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., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25 /**
26 * SECTION:element-volume
27 * @title: volume
28 *
29 * The volume element changes the volume of the audio data.
30 *
31 * ## Example launch line
32 * |[
33 * gst-launch-1.0 -v -m audiotestsrc ! volume volume=0.5 ! level ! fakesink silent=TRUE
34 * ]|
35 * This pipeline shows that the level of audiotestsrc has been halved
36 * (peak values are around -6 dB and RMS around -9 dB) compared to
37 * the same pipeline without the volume element.
38 *
39 */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <string.h>
46 #include <gst/gst.h>
47 #include <gst/base/gstbasetransform.h>
48 #include <gst/audio/audio.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 #define GST_CAT_DEFAULT gst_volume_debug
83 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
84
85 /* Filter signals and args */
86 enum
87 {
88 /* FILL ME */
89 LAST_SIGNAL
90 };
91
92 #define DEFAULT_PROP_MUTE FALSE
93 #define DEFAULT_PROP_VOLUME 1.0
94
95 enum
96 {
97 PROP_0,
98 PROP_MUTE,
99 PROP_VOLUME
100 };
101
102 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
103 #define ALLOWED_CAPS \
104 GST_AUDIO_CAPS_MAKE ("{ F32LE, F64LE, S8, S16LE, S24LE, S32LE }") \
105 ", layout = (string) interleaved"
106 #else
107 #define ALLOWED_CAPS \
108 GST_AUDIO_CAPS_MAKE ("{ F32BE, F64BE, S8, S16BE, S24BE, S32BE }") \
109 ", layout = (string) { interleaved, non-interleaved }"
110 #endif
111
112 #define gst_volume_parent_class parent_class
113 G_DEFINE_TYPE_WITH_CODE (GstVolume, gst_volume,
114 GST_TYPE_AUDIO_FILTER,
115 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
116 GST_ELEMENT_REGISTER_DEFINE (volume, "volume", GST_RANK_NONE, GST_TYPE_VOLUME);
117
118 static void volume_set_property (GObject * object, guint prop_id,
119 const GValue * value, GParamSpec * pspec);
120 static void volume_get_property (GObject * object, guint prop_id,
121 GValue * value, GParamSpec * pspec);
122
123 static void volume_before_transform (GstBaseTransform * base,
124 GstBuffer * buffer);
125 static GstFlowReturn volume_transform_ip (GstBaseTransform * base,
126 GstBuffer * outbuf);
127 static gboolean volume_stop (GstBaseTransform * base);
128 static gboolean volume_setup (GstAudioFilter * filter,
129 const GstAudioInfo * info);
130
131 static void volume_process_double (GstVolume * self, gpointer bytes,
132 guint n_bytes);
133 static void volume_process_controlled_double (GstVolume * self, gpointer bytes,
134 gdouble * volume, guint channels, guint n_bytes);
135 static void volume_process_float (GstVolume * self, gpointer bytes,
136 guint n_bytes);
137 static void volume_process_controlled_float (GstVolume * self, gpointer bytes,
138 gdouble * volume, guint channels, guint n_bytes);
139 static void volume_process_int32 (GstVolume * self, gpointer bytes,
140 guint n_bytes);
141 static void volume_process_int32_clamp (GstVolume * self, gpointer bytes,
142 guint n_bytes);
143 static void volume_process_controlled_int32_clamp (GstVolume * self,
144 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
145 static void volume_process_int24 (GstVolume * self, gpointer bytes,
146 guint n_bytes);
147 static void volume_process_int24_clamp (GstVolume * self, gpointer bytes,
148 guint n_bytes);
149 static void volume_process_controlled_int24_clamp (GstVolume * self,
150 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
151 static void volume_process_int16 (GstVolume * self, gpointer bytes,
152 guint n_bytes);
153 static void volume_process_int16_clamp (GstVolume * self, gpointer bytes,
154 guint n_bytes);
155 static void volume_process_controlled_int16_clamp (GstVolume * self,
156 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
157 static void volume_process_int8 (GstVolume * self, gpointer bytes,
158 guint n_bytes);
159 static void volume_process_int8_clamp (GstVolume * self, gpointer bytes,
160 guint n_bytes);
161 static void volume_process_controlled_int8_clamp (GstVolume * self,
162 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
163
164
165 /* helper functions */
166
167 static gboolean
volume_choose_func(GstVolume * self,const GstAudioInfo * info)168 volume_choose_func (GstVolume * self, const GstAudioInfo * info)
169 {
170 GstAudioFormat format;
171
172 self->process = NULL;
173 self->process_controlled = NULL;
174
175 format = GST_AUDIO_INFO_FORMAT (info);
176
177 if (format == GST_AUDIO_FORMAT_UNKNOWN)
178 return FALSE;
179
180 switch (format) {
181 case GST_AUDIO_FORMAT_S32:
182 /* only clamp if the gain is greater than 1.0 */
183 if (self->current_vol_i32 > VOLUME_UNITY_INT32) {
184 self->process = volume_process_int32_clamp;
185 } else {
186 self->process = volume_process_int32;
187 }
188 self->process_controlled = volume_process_controlled_int32_clamp;
189 break;
190 case GST_AUDIO_FORMAT_S24:
191 /* only clamp if the gain is greater than 1.0 */
192 if (self->current_vol_i24 > VOLUME_UNITY_INT24) {
193 self->process = volume_process_int24_clamp;
194 } else {
195 self->process = volume_process_int24;
196 }
197 self->process_controlled = volume_process_controlled_int24_clamp;
198 break;
199 case GST_AUDIO_FORMAT_S16:
200 /* only clamp if the gain is greater than 1.0 */
201 if (self->current_vol_i16 > VOLUME_UNITY_INT16) {
202 self->process = volume_process_int16_clamp;
203 } else {
204 self->process = volume_process_int16;
205 }
206 self->process_controlled = volume_process_controlled_int16_clamp;
207 break;
208 case GST_AUDIO_FORMAT_S8:
209 /* only clamp if the gain is greater than 1.0 */
210 if (self->current_vol_i8 > VOLUME_UNITY_INT8) {
211 self->process = volume_process_int8_clamp;
212 } else {
213 self->process = volume_process_int8;
214 }
215 self->process_controlled = volume_process_controlled_int8_clamp;
216 break;
217 case GST_AUDIO_FORMAT_F32:
218 self->process = volume_process_float;
219 self->process_controlled = volume_process_controlled_float;
220 break;
221 case GST_AUDIO_FORMAT_F64:
222 self->process = volume_process_double;
223 self->process_controlled = volume_process_controlled_double;
224 break;
225 default:
226 break;
227 }
228
229 return (self->process != NULL);
230 }
231
232 static gboolean
volume_update_volume(GstVolume * self,const GstAudioInfo * info,gdouble volume,gboolean mute)233 volume_update_volume (GstVolume * self, const GstAudioInfo * info,
234 gdouble volume, gboolean mute)
235 {
236 gboolean passthrough;
237 gboolean res;
238
239 GST_DEBUG_OBJECT (self, "configure mute %d, volume %f", mute, volume);
240
241 if (mute) {
242 self->current_mute = TRUE;
243 self->current_volume = 0.0;
244
245 self->current_vol_i8 = 0;
246 self->current_vol_i16 = 0;
247 self->current_vol_i24 = 0;
248 self->current_vol_i32 = 0;
249
250 passthrough = FALSE;
251 } else {
252 self->current_mute = FALSE;
253 self->current_volume = volume;
254
255 self->current_vol_i8 =
256 (gint) ((gdouble) volume * (gdouble) VOLUME_UNITY_INT8);
257 self->current_vol_i16 =
258 (gint) ((gdouble) volume * (gdouble) VOLUME_UNITY_INT16);
259 self->current_vol_i24 =
260 (gint) ((gdouble) volume * (gdouble) VOLUME_UNITY_INT24);
261 self->current_vol_i32 =
262 (gint) ((gdouble) volume * (gdouble) VOLUME_UNITY_INT32);
263
264 passthrough = (self->current_vol_i16 == VOLUME_UNITY_INT16);
265 }
266
267 /* If a controller is used, never use passthrough mode
268 * because the property can change from 1.0 to something
269 * else in the middle of a buffer.
270 */
271 passthrough &= !gst_object_has_active_control_bindings (GST_OBJECT (self));
272
273 GST_DEBUG_OBJECT (self, "set passthrough %d", passthrough);
274
275 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (self), passthrough);
276
277 res = self->negotiated = volume_choose_func (self, info);
278
279 return res;
280 }
281
282 /* Element class */
283
284 static void
gst_volume_dispose(GObject * object)285 gst_volume_dispose (GObject * object)
286 {
287 GstVolume *volume = GST_VOLUME (object);
288
289 if (volume->tracklist) {
290 if (volume->tracklist->data)
291 g_object_unref (volume->tracklist->data);
292 g_list_free (volume->tracklist);
293 volume->tracklist = NULL;
294 }
295
296 G_OBJECT_CLASS (parent_class)->dispose (object);
297 }
298
299 static void
gst_volume_class_init(GstVolumeClass * klass)300 gst_volume_class_init (GstVolumeClass * klass)
301 {
302 GObjectClass *gobject_class;
303 GstElementClass *element_class;
304 GstBaseTransformClass *trans_class;
305 GstAudioFilterClass *filter_class;
306 GstCaps *caps;
307
308 gobject_class = (GObjectClass *) klass;
309 element_class = (GstElementClass *) klass;
310 trans_class = (GstBaseTransformClass *) klass;
311 filter_class = (GstAudioFilterClass *) (klass);
312
313 gobject_class->set_property = volume_set_property;
314 gobject_class->get_property = volume_get_property;
315 gobject_class->dispose = gst_volume_dispose;
316
317 g_object_class_install_property (gobject_class, PROP_MUTE,
318 g_param_spec_boolean ("mute", "Mute", "mute channel",
319 DEFAULT_PROP_MUTE,
320 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
321
322 g_object_class_install_property (gobject_class, PROP_VOLUME,
323 g_param_spec_double ("volume", "Volume", "volume factor, 1.0=100%",
324 0.0, VOLUME_MAX_DOUBLE, DEFAULT_PROP_VOLUME,
325 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
326
327 gst_element_class_set_static_metadata (element_class, "Volume",
328 "Filter/Effect/Audio",
329 "Set volume on audio/raw streams", "Andy Wingo <wingo@pobox.com>");
330
331 caps = gst_caps_from_string (ALLOWED_CAPS);
332 gst_audio_filter_class_add_pad_templates (filter_class, caps);
333 gst_caps_unref (caps);
334
335 trans_class->before_transform = GST_DEBUG_FUNCPTR (volume_before_transform);
336 trans_class->transform_ip = GST_DEBUG_FUNCPTR (volume_transform_ip);
337 trans_class->stop = GST_DEBUG_FUNCPTR (volume_stop);
338 trans_class->transform_ip_on_passthrough = FALSE;
339
340 filter_class->setup = GST_DEBUG_FUNCPTR (volume_setup);
341 }
342
343 static void
gst_volume_init(GstVolume * self)344 gst_volume_init (GstVolume * self)
345 {
346 self->mute = DEFAULT_PROP_MUTE;
347 self->volume = DEFAULT_PROP_VOLUME;
348
349 self->tracklist = NULL;
350 self->negotiated = FALSE;
351
352 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (self), TRUE);
353 }
354
355 static void
volume_process_double(GstVolume * self,gpointer bytes,guint n_bytes)356 volume_process_double (GstVolume * self, gpointer bytes, guint n_bytes)
357 {
358 gdouble *data = (gdouble *) bytes;
359 guint num_samples = n_bytes / sizeof (gdouble);
360
361 volume_orc_scalarmultiply_f64_ns (data, self->current_volume, num_samples);
362 }
363
364 static void
volume_process_controlled_double(GstVolume * self,gpointer bytes,gdouble * volume,guint channels,guint n_bytes)365 volume_process_controlled_double (GstVolume * self, gpointer bytes,
366 gdouble * volume, guint channels, guint n_bytes)
367 {
368 gdouble *data = (gdouble *) bytes;
369 guint num_samples = n_bytes / (sizeof (gdouble) * channels);
370 guint i, j;
371 gdouble vol;
372
373 if (channels == 1) {
374 volume_orc_process_controlled_f64_1ch (data, volume, num_samples);
375 } else {
376 for (i = 0; i < num_samples; i++) {
377 vol = *volume++;
378 for (j = 0; j < channels; j++) {
379 *data++ *= vol;
380 }
381 }
382 }
383 }
384
385 static void
volume_process_float(GstVolume * self,gpointer bytes,guint n_bytes)386 volume_process_float (GstVolume * self, gpointer bytes, guint n_bytes)
387 {
388 gfloat *data = (gfloat *) bytes;
389 guint num_samples = n_bytes / sizeof (gfloat);
390
391 volume_orc_scalarmultiply_f32_ns (data, self->current_volume, num_samples);
392 }
393
394 static void
volume_process_controlled_float(GstVolume * self,gpointer bytes,gdouble * volume,guint channels,guint n_bytes)395 volume_process_controlled_float (GstVolume * self, gpointer bytes,
396 gdouble * volume, guint channels, guint n_bytes)
397 {
398 gfloat *data = (gfloat *) bytes;
399 guint num_samples = n_bytes / (sizeof (gfloat) * channels);
400 guint i, j;
401 gdouble vol;
402
403 if (channels == 1) {
404 volume_orc_process_controlled_f32_1ch (data, volume, num_samples);
405 } else if (channels == 2) {
406 volume_orc_process_controlled_f32_2ch (data, volume, num_samples);
407 } else {
408 for (i = 0; i < num_samples; i++) {
409 vol = *volume++;
410 for (j = 0; j < channels; j++) {
411 *data++ *= vol;
412 }
413 }
414 }
415 }
416
417 static void
volume_process_int32(GstVolume * self,gpointer bytes,guint n_bytes)418 volume_process_int32 (GstVolume * self, gpointer bytes, guint n_bytes)
419 {
420 gint32 *data = (gint32 *) bytes;
421 guint num_samples = n_bytes / sizeof (gint);
422
423 /* hard coded in volume.orc */
424 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
425 volume_orc_process_int32 (data, self->current_vol_i32, num_samples);
426 }
427
428 static void
volume_process_int32_clamp(GstVolume * self,gpointer bytes,guint n_bytes)429 volume_process_int32_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
430 {
431 gint32 *data = (gint32 *) bytes;
432 guint num_samples = n_bytes / sizeof (gint);
433
434 /* hard coded in volume.orc */
435 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
436
437 volume_orc_process_int32_clamp (data, self->current_vol_i32, num_samples);
438 }
439
440 static void
volume_process_controlled_int32_clamp(GstVolume * self,gpointer bytes,gdouble * volume,guint channels,guint n_bytes)441 volume_process_controlled_int32_clamp (GstVolume * self, gpointer bytes,
442 gdouble * volume, guint channels, guint n_bytes)
443 {
444 gint32 *data = (gint32 *) bytes;
445 guint i, j;
446 guint num_samples = n_bytes / (sizeof (gint32) * channels);
447 gdouble vol, val;
448
449 if (channels == 1) {
450 volume_orc_process_controlled_int32_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 val = *data * vol;
456 *data++ = (gint32) CLAMP (val, VOLUME_MIN_INT32, VOLUME_MAX_INT32);
457 }
458 }
459 }
460 }
461
462 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
463 #define get_unaligned_i24(_x) ( (((guint8*)_x)[0]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[2]) << 16) )
464
465 #define write_unaligned_u24(_x,samp) \
466 G_STMT_START { \
467 *(_x)++ = samp & 0xFF; \
468 *(_x)++ = (samp >> 8) & 0xFF; \
469 *(_x)++ = (samp >> 16) & 0xFF; \
470 } G_STMT_END
471
472 #else /* BIG ENDIAN */
473 #define get_unaligned_i24(_x) ( (((guint8*)_x)[2]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[0]) << 16) )
474 #define write_unaligned_u24(_x,samp) \
475 G_STMT_START { \
476 *(_x)++ = (samp >> 16) & 0xFF; \
477 *(_x)++ = (samp >> 8) & 0xFF; \
478 *(_x)++ = samp & 0xFF; \
479 } G_STMT_END
480 #endif
481
482 static void
volume_process_int24(GstVolume * self,gpointer bytes,guint n_bytes)483 volume_process_int24 (GstVolume * self, gpointer bytes, guint n_bytes)
484 {
485 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
486 guint i, num_samples;
487 guint32 samp;
488 gint64 val;
489
490 num_samples = n_bytes / (sizeof (gint8) * 3);
491 for (i = 0; i < num_samples; i++) {
492 samp = get_unaligned_i24 (data);
493
494 val = (gint32) samp;
495 val =
496 (((gint64) self->current_vol_i24 *
497 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
498 samp = (guint32) val;
499
500 /* write the value back into the stream */
501 write_unaligned_u24 (data, samp);
502 }
503 }
504
505 static void
volume_process_int24_clamp(GstVolume * self,gpointer bytes,guint n_bytes)506 volume_process_int24_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
507 {
508 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
509 guint i, num_samples;
510 guint32 samp;
511 gint64 val;
512
513 num_samples = n_bytes / (sizeof (gint8) * 3);
514 for (i = 0; i < num_samples; i++) {
515 samp = get_unaligned_i24 (data);
516
517 val = (gint32) samp;
518 val =
519 (((gint64) self->current_vol_i24 *
520 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
521 samp = (guint32) CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
522
523 /* write the value back into the stream */
524 write_unaligned_u24 (data, samp);
525 }
526 }
527
528 static void
volume_process_controlled_int24_clamp(GstVolume * self,gpointer bytes,gdouble * volume,guint channels,guint n_bytes)529 volume_process_controlled_int24_clamp (GstVolume * self, gpointer bytes,
530 gdouble * volume, guint channels, guint n_bytes)
531 {
532 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
533 guint i, j;
534 guint num_samples = n_bytes / (sizeof (gint8) * 3 * channels);
535 gdouble vol, val;
536
537 for (i = 0; i < num_samples; i++) {
538 vol = *volume++;
539 for (j = 0; j < channels; j++) {
540 val = get_unaligned_i24 (data) * vol;
541 val = CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
542 write_unaligned_u24 (data, (gint32) val);
543 }
544 }
545 }
546
547 static void
volume_process_int16(GstVolume * self,gpointer bytes,guint n_bytes)548 volume_process_int16 (GstVolume * self, gpointer bytes, guint n_bytes)
549 {
550 gint16 *data = (gint16 *) bytes;
551 guint num_samples = n_bytes / sizeof (gint16);
552
553 /* hard coded in volume.orc */
554 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
555
556 volume_orc_process_int16 (data, self->current_vol_i16, num_samples);
557 }
558
559 static void
volume_process_int16_clamp(GstVolume * self,gpointer bytes,guint n_bytes)560 volume_process_int16_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
561 {
562 gint16 *data = (gint16 *) bytes;
563 guint num_samples = n_bytes / sizeof (gint16);
564
565 /* hard coded in volume.orc */
566 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
567
568 volume_orc_process_int16_clamp (data, self->current_vol_i16, num_samples);
569 }
570
571 static void
volume_process_controlled_int16_clamp(GstVolume * self,gpointer bytes,gdouble * volume,guint channels,guint n_bytes)572 volume_process_controlled_int16_clamp (GstVolume * self, gpointer bytes,
573 gdouble * volume, guint channels, guint n_bytes)
574 {
575 gint16 *data = (gint16 *) bytes;
576 guint i, j;
577 guint num_samples = n_bytes / (sizeof (gint16) * channels);
578 gdouble vol, val;
579
580 if (channels == 1) {
581 volume_orc_process_controlled_int16_1ch (data, volume, num_samples);
582 } else if (channels == 2) {
583 volume_orc_process_controlled_int16_2ch (data, volume, num_samples);
584 } else {
585 for (i = 0; i < num_samples; i++) {
586 vol = *volume++;
587 for (j = 0; j < channels; j++) {
588 val = *data * vol;
589 *data++ = (gint16) CLAMP (val, VOLUME_MIN_INT16, VOLUME_MAX_INT16);
590 }
591 }
592 }
593 }
594
595 static void
volume_process_int8(GstVolume * self,gpointer bytes,guint n_bytes)596 volume_process_int8 (GstVolume * self, gpointer bytes, guint n_bytes)
597 {
598 gint8 *data = (gint8 *) bytes;
599 guint num_samples = n_bytes / sizeof (gint8);
600
601 /* hard coded in volume.orc */
602 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
603
604 volume_orc_process_int8 (data, self->current_vol_i8, num_samples);
605 }
606
607 static void
volume_process_int8_clamp(GstVolume * self,gpointer bytes,guint n_bytes)608 volume_process_int8_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
609 {
610 gint8 *data = (gint8 *) bytes;
611 guint num_samples = n_bytes / sizeof (gint8);
612
613 /* hard coded in volume.orc */
614 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
615
616 volume_orc_process_int8_clamp (data, self->current_vol_i8, num_samples);
617 }
618
619 static void
volume_process_controlled_int8_clamp(GstVolume * self,gpointer bytes,gdouble * volume,guint channels,guint n_bytes)620 volume_process_controlled_int8_clamp (GstVolume * self, gpointer bytes,
621 gdouble * volume, guint channels, guint n_bytes)
622 {
623 gint8 *data = (gint8 *) bytes;
624 guint i, j;
625 guint num_samples = n_bytes / (sizeof (gint8) * channels);
626 gdouble val, vol;
627
628 if (channels == 1) {
629 volume_orc_process_controlled_int8_1ch (data, volume, num_samples);
630 } else if (channels == 2) {
631 volume_orc_process_controlled_int8_2ch (data, volume, num_samples);
632 } else {
633 for (i = 0; i < num_samples; i++) {
634 vol = *volume++;
635 for (j = 0; j < channels; j++) {
636 val = *data * vol;
637 *data++ = (gint8) CLAMP (val, VOLUME_MIN_INT8, VOLUME_MAX_INT8);
638 }
639 }
640 }
641 }
642
643 /* GstBaseTransform vmethod implementations */
644
645 /* get notified of caps and plug in the correct process function */
646 static gboolean
volume_setup(GstAudioFilter * filter,const GstAudioInfo * info)647 volume_setup (GstAudioFilter * filter, const GstAudioInfo * info)
648 {
649 gboolean res;
650 GstVolume *self = GST_VOLUME (filter);
651 gdouble volume;
652 gboolean mute;
653
654 GST_OBJECT_LOCK (self);
655 volume = self->volume;
656 mute = self->mute;
657 GST_OBJECT_UNLOCK (self);
658
659 res = volume_update_volume (self, info, volume, mute);
660 if (!res) {
661 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
662 ("Invalid incoming format"), (NULL));
663 }
664 self->negotiated = res;
665
666 return res;
667 }
668
669 static gboolean
volume_stop(GstBaseTransform * base)670 volume_stop (GstBaseTransform * base)
671 {
672 GstVolume *self = GST_VOLUME (base);
673
674 g_free (self->volumes);
675 self->volumes = NULL;
676 self->volumes_count = 0;
677
678 g_free (self->mutes);
679 self->mutes = NULL;
680 self->mutes_count = 0;
681
682 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, stop, (base),
683 TRUE);
684 }
685
686 static void
volume_before_transform(GstBaseTransform * base,GstBuffer * buffer)687 volume_before_transform (GstBaseTransform * base, GstBuffer * buffer)
688 {
689 GstClockTime timestamp;
690 GstVolume *self = GST_VOLUME (base);
691 gdouble volume;
692 gboolean mute;
693
694 timestamp = GST_BUFFER_TIMESTAMP (buffer);
695 timestamp =
696 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
697
698 GST_DEBUG_OBJECT (base, "sync to %" GST_TIME_FORMAT,
699 GST_TIME_ARGS (timestamp));
700
701 if (GST_CLOCK_TIME_IS_VALID (timestamp))
702 gst_object_sync_values (GST_OBJECT (self), timestamp);
703
704 /* get latest values */
705 GST_OBJECT_LOCK (self);
706 volume = self->volume;
707 mute = self->mute;
708 GST_OBJECT_UNLOCK (self);
709
710 if ((volume != self->current_volume) || (mute != self->current_mute)) {
711 /* the volume or mute was updated, update our internal state before
712 * we continue processing. */
713 volume_update_volume (self, GST_AUDIO_FILTER_INFO (self), volume, mute);
714 }
715 }
716
717 /* call the plugged-in process function for this instance
718 * needs to be done with this indirection since volume_transform is
719 * a class-global method
720 */
721 static GstFlowReturn
volume_transform_ip(GstBaseTransform * base,GstBuffer * outbuf)722 volume_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
723 {
724 GstAudioFilter *filter = GST_AUDIO_FILTER_CAST (base);
725 GstVolume *self = GST_VOLUME (base);
726 GstMapInfo map;
727 GstClockTime ts;
728
729 if (G_UNLIKELY (!self->negotiated))
730 goto not_negotiated;
731
732 /* don't process data with GAP */
733 if (GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP))
734 return GST_FLOW_OK;
735
736 gst_buffer_map (outbuf, &map, GST_MAP_READWRITE);
737 ts = GST_BUFFER_TIMESTAMP (outbuf);
738 ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, ts);
739
740 if (GST_CLOCK_TIME_IS_VALID (ts)) {
741 GstControlBinding *mute_cb, *volume_cb;
742
743 mute_cb = gst_object_get_control_binding (GST_OBJECT (self), "mute");
744 volume_cb = gst_object_get_control_binding (GST_OBJECT (self), "volume");
745
746 if (mute_cb || (volume_cb && !self->current_mute)) {
747 gint rate = GST_AUDIO_INFO_RATE (&filter->info);
748 gint width = GST_AUDIO_FORMAT_INFO_WIDTH (filter->info.finfo) / 8;
749 gint channels = GST_AUDIO_INFO_CHANNELS (&filter->info);
750 guint nsamples = map.size / (width * channels);
751 GstClockTime interval = gst_util_uint64_scale_int (1, GST_SECOND, rate);
752 gboolean have_mutes = FALSE;
753 gboolean have_volumes = FALSE;
754
755 if (self->mutes_count < nsamples && mute_cb) {
756 self->mutes = g_realloc (self->mutes, sizeof (gboolean) * nsamples);
757 self->mutes_count = nsamples;
758 }
759
760 if (self->volumes_count < nsamples) {
761 self->volumes = g_realloc (self->volumes, sizeof (gdouble) * nsamples);
762 self->volumes_count = nsamples;
763 }
764
765 if (volume_cb && self->volumes) {
766 have_volumes =
767 gst_control_binding_get_value_array (volume_cb, ts, interval,
768 nsamples, (gpointer) self->volumes);
769 gst_object_replace ((GstObject **) & volume_cb, NULL);
770 }
771 if (!have_volumes) {
772 volume_orc_memset_f64 (self->volumes, self->current_volume, nsamples);
773 }
774
775 if (mute_cb && self->mutes) {
776 have_mutes = gst_control_binding_get_value_array (mute_cb, ts, interval,
777 nsamples, (gpointer) self->mutes);
778 gst_object_replace ((GstObject **) & mute_cb, NULL);
779 }
780 if (have_mutes) {
781 volume_orc_prepare_volumes (self->volumes, self->mutes, nsamples);
782 } else {
783 g_free (self->mutes);
784 self->mutes = NULL;
785 self->mutes_count = 0;
786 }
787
788 self->process_controlled (self, map.data, self->volumes, channels,
789 map.size);
790
791 goto done;
792 } else if (volume_cb) {
793 gst_object_unref (volume_cb);
794 }
795 }
796
797 if (self->current_volume == 0.0 || self->current_mute) {
798 orc_memset (map.data, 0, map.size);
799 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
800 } else if (self->current_volume != 1.0) {
801 self->process (self, map.data, map.size);
802 }
803
804 done:
805 gst_buffer_unmap (outbuf, &map);
806
807 return GST_FLOW_OK;
808
809 /* ERRORS */
810 not_negotiated:
811 {
812 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
813 ("No format was negotiated"), (NULL));
814 return GST_FLOW_NOT_NEGOTIATED;
815 }
816 }
817
818 static void
volume_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)819 volume_set_property (GObject * object, guint prop_id, const GValue * value,
820 GParamSpec * pspec)
821 {
822 GstVolume *self = GST_VOLUME (object);
823
824 switch (prop_id) {
825 case PROP_MUTE:
826 GST_OBJECT_LOCK (self);
827 self->mute = g_value_get_boolean (value);
828 GST_OBJECT_UNLOCK (self);
829 break;
830 case PROP_VOLUME:
831 GST_OBJECT_LOCK (self);
832 self->volume = g_value_get_double (value);
833 GST_OBJECT_UNLOCK (self);
834 break;
835 default:
836 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
837 break;
838 }
839 }
840
841 static void
volume_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)842 volume_get_property (GObject * object, guint prop_id, GValue * value,
843 GParamSpec * pspec)
844 {
845 GstVolume *self = GST_VOLUME (object);
846
847 switch (prop_id) {
848 case PROP_MUTE:
849 GST_OBJECT_LOCK (self);
850 g_value_set_boolean (value, self->mute);
851 GST_OBJECT_UNLOCK (self);
852 break;
853 case PROP_VOLUME:
854 GST_OBJECT_LOCK (self);
855 g_value_set_double (value, self->volume);
856 GST_OBJECT_UNLOCK (self);
857 break;
858 default:
859 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
860 break;
861 }
862 }
863
864 static gboolean
plugin_init(GstPlugin * plugin)865 plugin_init (GstPlugin * plugin)
866 {
867 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "volume", 0, "Volume gain");
868
869 return GST_ELEMENT_REGISTER (volume, plugin);
870 }
871
872 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
873 GST_VERSION_MINOR,
874 volume,
875 "plugin for controlling audio volume",
876 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
877