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