1 /*
2  * GStreamer
3  * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
4  * Copyright (C) 2006 Sebastian Dröge <slomo@circular-chaos.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 /**
23  * SECTION:element-audiopanorama
24  *
25  * Stereo panorama effect with controllable pan position. One can choose between the default psychoacoustic panning method,
26  * which keeps the same perceived loudness, and a simple panning method that just controls the volume on one channel.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch-1.0 audiotestsrc wave=saw ! audiopanorama panorama=-1.00 ! alsasink
32  * gst-launch-1.0 filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiopanorama panorama=-1.00 ! alsasink
33  * gst-launch-1.0 audiotestsrc wave=saw ! audioconvert ! audiopanorama panorama=-1.00 ! audioconvert ! alsasink
34  * gst-launch-1.0 audiotestsrc wave=saw ! audioconvert ! audiopanorama method=simple panorama=-0.50 ! audioconvert ! alsasink
35  * ]|
36  * </refsect2>
37  */
38 
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42 
43 #include <string.h>
44 
45 #include <gst/gst.h>
46 #include <gst/base/gstbasetransform.h>
47 
48 #ifdef HAVE_ORC
49 #include <orc/orcfunctions.h>
50 #else
51 #define orc_memset memset
52 #endif
53 
54 #include "audiopanorama.h"
55 #include "audiopanoramaorc.h"
56 
57 #define GST_CAT_DEFAULT gst_audio_panorama_debug
58 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
59 
60 /* Filter signals and args */
61 enum
62 {
63   PROP_0,
64   PROP_PANORAMA,
65   PROP_METHOD
66 };
67 
68 #define GST_TYPE_AUDIO_PANORAMA_METHOD (gst_audio_panorama_method_get_type ())
69 static GType
gst_audio_panorama_method_get_type(void)70 gst_audio_panorama_method_get_type (void)
71 {
72   static GType gtype = 0;
73 
74   if (gtype == 0) {
75     static const GEnumValue values[] = {
76       {METHOD_PSYCHOACOUSTIC, "Psychoacoustic Panning (default)",
77           "psychoacoustic"},
78       {METHOD_SIMPLE, "Simple Panning", "simple"},
79       {0, NULL, NULL}
80     };
81 
82     gtype = g_enum_register_static ("GstAudioPanoramaMethod", values);
83   }
84   return gtype;
85 }
86 
87 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
88     GST_PAD_SINK,
89     GST_PAD_ALWAYS,
90     GST_STATIC_CAPS ("audio/x-raw, "
91         "format = (string) { " GST_AUDIO_NE (F32) ", " GST_AUDIO_NE (S16) "}, "
92         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ], "
93         "layout = (string) interleaved")
94     );
95 
96 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
97     GST_PAD_SRC,
98     GST_PAD_ALWAYS,
99     GST_STATIC_CAPS ("audio/x-raw, "
100         "format = (string) { " GST_AUDIO_NE (F32) ", " GST_AUDIO_NE (S16) "}, "
101         "rate = (int) [ 1, MAX ], " "channels = (int) 2, "
102         "layout = (string) interleaved")
103     );
104 
105 G_DEFINE_TYPE (GstAudioPanorama, gst_audio_panorama, GST_TYPE_BASE_TRANSFORM);
106 
107 static void gst_audio_panorama_set_property (GObject * object, guint prop_id,
108     const GValue * value, GParamSpec * pspec);
109 static void gst_audio_panorama_get_property (GObject * object, guint prop_id,
110     GValue * value, GParamSpec * pspec);
111 
112 static gboolean gst_audio_panorama_get_unit_size (GstBaseTransform * base,
113     GstCaps * caps, gsize * size);
114 static GstCaps *gst_audio_panorama_transform_caps (GstBaseTransform * base,
115     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
116 static gboolean gst_audio_panorama_set_caps (GstBaseTransform * base,
117     GstCaps * incaps, GstCaps * outcaps);
118 
119 static void gst_audio_panorama_m2s_int (gfloat pan,
120     gint16 * idata, gint16 * odata, guint num_samples);
121 static void gst_audio_panorama_s2s_int (gfloat pan,
122     gint16 * idata, gint16 * odata, guint num_samples);
123 static void gst_audio_panorama_m2s_float (gfloat pan,
124     gfloat * idata, gfloat * odata, guint num_samples);
125 static void gst_audio_panorama_s2s_float (gfloat pan,
126     gfloat * idata, gfloat * odata, guint num_samples);
127 
128 static void gst_audio_panorama_m2s_int_simple (gfloat pan,
129     gint16 * idata, gint16 * odata, guint num_samples);
130 static void gst_audio_panorama_s2s_int_simple (gfloat pan,
131     gint16 * idata, gint16 * odata, guint num_samples);
132 static void gst_audio_panorama_m2s_float_simple (gfloat pan,
133     gfloat * idata, gfloat * odata, guint num_samples);
134 static void gst_audio_panorama_s2s_float_simple (gfloat pan,
135     gfloat * idata, gfloat * odata, guint num_samples);
136 
137 static GstFlowReturn gst_audio_panorama_transform (GstBaseTransform * base,
138     GstBuffer * inbuf, GstBuffer * outbuf);
139 
140 
141 /* Table with processing functions: [channels][format][method] */
142 static const GstAudioPanoramaProcessFunc panorama_process_functions[2][2][2] = {
143   {
144         {
145               (GstAudioPanoramaProcessFunc) gst_audio_panorama_m2s_int,
146             (GstAudioPanoramaProcessFunc) gst_audio_panorama_m2s_int_simple},
147         {
148               (GstAudioPanoramaProcessFunc) gst_audio_panorama_m2s_float,
149             (GstAudioPanoramaProcessFunc) gst_audio_panorama_m2s_float_simple}
150       },
151   {
152         {
153               (GstAudioPanoramaProcessFunc) gst_audio_panorama_s2s_int,
154             (GstAudioPanoramaProcessFunc) gst_audio_panorama_s2s_int_simple},
155         {
156               (GstAudioPanoramaProcessFunc) gst_audio_panorama_s2s_float,
157             (GstAudioPanoramaProcessFunc) gst_audio_panorama_s2s_float_simple}
158       }
159 };
160 
161 /* GObject vmethod implementations */
162 
163 static void
gst_audio_panorama_class_init(GstAudioPanoramaClass * klass)164 gst_audio_panorama_class_init (GstAudioPanoramaClass * klass)
165 {
166   GObjectClass *gobject_class;
167   GstElementClass *gstelement_class;
168 
169   GST_DEBUG_CATEGORY_INIT (gst_audio_panorama_debug, "audiopanorama", 0,
170       "audiopanorama element");
171 
172   gobject_class = (GObjectClass *) klass;
173   gstelement_class = (GstElementClass *) klass;
174 
175   gobject_class->set_property = gst_audio_panorama_set_property;
176   gobject_class->get_property = gst_audio_panorama_get_property;
177 
178   g_object_class_install_property (gobject_class, PROP_PANORAMA,
179       g_param_spec_float ("panorama", "Panorama",
180           "Position in stereo panorama (-1.0 left -> 1.0 right)", -1.0, 1.0,
181           0.0,
182           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
183   /**
184    * GstAudioPanorama:method:
185    *
186    * Panning method: psychoacoustic mode keeps the same perceived loudness,
187    * while simple mode just controls the volume of one channel. It's merely
188    * a matter of taste which method should be chosen.
189    */
190   g_object_class_install_property (gobject_class, PROP_METHOD,
191       g_param_spec_enum ("method", "Panning method",
192           "Psychoacoustic mode keeps same perceived loudness, "
193           "simple mode just controls volume of one channel.",
194           GST_TYPE_AUDIO_PANORAMA_METHOD, METHOD_PSYCHOACOUSTIC,
195           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196 
197   gst_element_class_set_static_metadata (gstelement_class, "Stereo positioning",
198       "Filter/Effect/Audio",
199       "Positions audio streams in the stereo panorama",
200       "Stefan Kost <ensonic@users.sf.net>");
201 
202   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
203   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
204 
205   GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
206       GST_DEBUG_FUNCPTR (gst_audio_panorama_get_unit_size);
207   GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
208       GST_DEBUG_FUNCPTR (gst_audio_panorama_transform_caps);
209   GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
210       GST_DEBUG_FUNCPTR (gst_audio_panorama_set_caps);
211   GST_BASE_TRANSFORM_CLASS (klass)->transform =
212       GST_DEBUG_FUNCPTR (gst_audio_panorama_transform);
213 }
214 
215 static void
gst_audio_panorama_init(GstAudioPanorama * filter)216 gst_audio_panorama_init (GstAudioPanorama * filter)
217 {
218 
219   filter->panorama = 0;
220   filter->method = METHOD_PSYCHOACOUSTIC;
221   gst_audio_info_init (&filter->info);
222   filter->process = NULL;
223 
224   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
225 }
226 
227 static gboolean
gst_audio_panorama_set_process_function(GstAudioPanorama * filter,GstAudioInfo * info)228 gst_audio_panorama_set_process_function (GstAudioPanorama * filter,
229     GstAudioInfo * info)
230 {
231   gint channel_index, format_index, method_index;
232   const GstAudioFormatInfo *finfo = info->finfo;
233 
234   /* set processing function */
235   channel_index = GST_AUDIO_INFO_CHANNELS (info) - 1;
236   if (channel_index > 1 || channel_index < 0) {
237     filter->process = NULL;
238     return FALSE;
239   }
240 
241   format_index = GST_AUDIO_FORMAT_INFO_IS_FLOAT (finfo) ? 1 : 0;
242   method_index = filter->method;
243 
244   filter->process =
245       panorama_process_functions[channel_index][format_index][method_index];
246   return TRUE;
247 }
248 
249 static void
gst_audio_panorama_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)250 gst_audio_panorama_set_property (GObject * object, guint prop_id,
251     const GValue * value, GParamSpec * pspec)
252 {
253   GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
254 
255   switch (prop_id) {
256     case PROP_PANORAMA:
257       filter->panorama = g_value_get_float (value);
258       break;
259     case PROP_METHOD:
260       filter->method = g_value_get_enum (value);
261       gst_audio_panorama_set_process_function (filter, &filter->info);
262       break;
263     default:
264       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265       break;
266   }
267 }
268 
269 static void
gst_audio_panorama_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)270 gst_audio_panorama_get_property (GObject * object, guint prop_id,
271     GValue * value, GParamSpec * pspec)
272 {
273   GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
274 
275   switch (prop_id) {
276     case PROP_PANORAMA:
277       g_value_set_float (value, filter->panorama);
278       break;
279     case PROP_METHOD:
280       g_value_set_enum (value, filter->method);
281       break;
282     default:
283       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284       break;
285   }
286 }
287 
288 /* GstBaseTransform vmethod implementations */
289 
290 static gboolean
gst_audio_panorama_get_unit_size(GstBaseTransform * base,GstCaps * caps,gsize * size)291 gst_audio_panorama_get_unit_size (GstBaseTransform * base, GstCaps * caps,
292     gsize * size)
293 {
294   GstAudioInfo info;
295 
296   g_assert (size);
297 
298   if (!gst_audio_info_from_caps (&info, caps))
299     return FALSE;
300 
301   *size = GST_AUDIO_INFO_BPF (&info);
302 
303   return TRUE;
304 }
305 
306 static GstCaps *
gst_audio_panorama_transform_caps(GstBaseTransform * base,GstPadDirection direction,GstCaps * caps,GstCaps * filter)307 gst_audio_panorama_transform_caps (GstBaseTransform * base,
308     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
309 {
310   GstCaps *res;
311   GstStructure *structure;
312   gint i;
313 
314   /* replace the channel property with our range. */
315   res = gst_caps_copy (caps);
316   for (i = 0; i < gst_caps_get_size (res); i++) {
317     structure = gst_caps_get_structure (res, i);
318     if (direction == GST_PAD_SRC) {
319       GST_INFO_OBJECT (base, "[%d] allow 1-2 channels", i);
320       gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
321     } else {
322       GST_INFO_OBJECT (base, "[%d] allow 2 channels", i);
323       gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
324     }
325     gst_structure_remove_field (structure, "channel-mask");
326   }
327   GST_DEBUG_OBJECT (base, "transformed %" GST_PTR_FORMAT, res);
328 
329   if (filter) {
330     GstCaps *intersection;
331 
332     GST_DEBUG_OBJECT (base, "Using filter caps %" GST_PTR_FORMAT, filter);
333     intersection =
334         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
335     gst_caps_unref (res);
336     res = intersection;
337     GST_DEBUG_OBJECT (base, "Intersection %" GST_PTR_FORMAT, res);
338   }
339 
340   return res;
341 }
342 
343 static gboolean
gst_audio_panorama_set_caps(GstBaseTransform * base,GstCaps * incaps,GstCaps * outcaps)344 gst_audio_panorama_set_caps (GstBaseTransform * base, GstCaps * incaps,
345     GstCaps * outcaps)
346 {
347   GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
348   GstAudioInfo info;
349 
350   /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
351   if (!gst_audio_info_from_caps (&info, incaps))
352     goto no_format;
353 
354   GST_DEBUG ("try to process %d input with %d channels",
355       GST_AUDIO_INFO_FORMAT (&info), GST_AUDIO_INFO_CHANNELS (&info));
356 
357   if (!gst_audio_panorama_set_process_function (filter, &info))
358     goto no_format;
359 
360   filter->info = info;
361 
362   return TRUE;
363 
364 no_format:
365   {
366     GST_DEBUG ("invalid caps");
367     return FALSE;
368   }
369 }
370 
371 /* psychoacoustic processing functions */
372 
373 /* mono to stereo panning
374  * pan: -1.0  0.0  1.0
375  * l:    1.0  0.5  0.0
376  * r:    0.0  0.5  1.0
377  *
378  * FIXME: we should use -3db (1/sqtr(2)) for 50:50
379  */
380 static void
gst_audio_panorama_m2s_int(gfloat pan,gint16 * idata,gint16 * odata,guint n)381 gst_audio_panorama_m2s_int (gfloat pan, gint16 * idata, gint16 * odata, guint n)
382 {
383   gfloat r = (pan + 1.0) / 2.0;
384   audiopanoramam_orc_process_s16_ch1_psy (odata, idata, 1.0 - r, r, n);
385 }
386 
387 static void
gst_audio_panorama_m2s_float(gfloat pan,gfloat * idata,gfloat * odata,guint n)388 gst_audio_panorama_m2s_float (gfloat pan, gfloat * idata,
389     gfloat * odata, guint n)
390 {
391   gfloat r = (pan + 1.0) / 2.0;
392   audiopanoramam_orc_process_f32_ch1_psy (odata, idata, 1.0 - r, r, n);
393 }
394 
395 /* stereo balance
396  * pan: -1.0  0.0  1.0
397  * ll:   1.0  1.0  0.0
398  * lr:   1.0  0.0  0.0
399  * rr:   0.0  1.0  1.0
400  * rl:   0.0  0.0  1.0
401  */
402 static void
gst_audio_panorama_s2s_int(gfloat pan,gint16 * idata,gint16 * odata,guint n)403 gst_audio_panorama_s2s_int (gfloat pan, gint16 * idata, gint16 * odata, guint n)
404 {
405   if (pan == 0.0) {
406     audiopanoramam_orc_process_s16_ch2_none (odata, idata, n);
407   } else if (pan > 0.0) {
408     gfloat rl = pan;
409     gfloat ll = 1.0 - rl;
410     audiopanoramam_orc_process_s16_ch2_psy_right (odata, idata, ll, rl, n);
411   } else {
412     gfloat rr = 1.0 + pan;
413     gfloat lr = 1.0 - rr;
414     audiopanoramam_orc_process_s16_ch2_psy_left (odata, idata, lr, rr, n);
415   }
416 }
417 
418 static void
gst_audio_panorama_s2s_float(gfloat pan,gfloat * idata,gfloat * odata,guint n)419 gst_audio_panorama_s2s_float (gfloat pan, gfloat * idata,
420     gfloat * odata, guint n)
421 {
422   if (pan == 0.0) {
423     audiopanoramam_orc_process_f32_ch2_none (odata, idata, n);
424   } else if (pan > 0.0) {
425     gfloat rl = pan;
426     gfloat ll = 1.0 - rl;
427     audiopanoramam_orc_process_f32_ch2_psy_right (odata, idata, ll, rl, n);
428   } else {
429     gfloat rr = 1.0 + pan;
430     gfloat lr = 1.0 - rr;
431     audiopanoramam_orc_process_f32_ch2_psy_left (odata, idata, lr, rr, n);
432   }
433 }
434 
435 /* simple processing functions */
436 
437 static void
gst_audio_panorama_m2s_int_simple(gfloat pan,gint16 * idata,gint16 * odata,guint n)438 gst_audio_panorama_m2s_int_simple (gfloat pan, gint16 * idata,
439     gint16 * odata, guint n)
440 {
441   if (pan == 0.0) {
442     audiopanoramam_orc_process_s16_ch1_none (odata, idata, n);
443   } else if (pan > 0.0) {
444     gfloat lpan = 1.0 - pan;
445     audiopanoramam_orc_process_s16_ch1_sim_left (odata, idata, lpan, n);
446   } else {
447     gfloat rpan = 1.0 + pan;
448     audiopanoramam_orc_process_s16_ch1_sim_right (odata, idata, rpan, n);
449   }
450 }
451 
452 static void
gst_audio_panorama_s2s_int_simple(gfloat pan,gint16 * idata,gint16 * odata,guint n)453 gst_audio_panorama_s2s_int_simple (gfloat pan, gint16 * idata,
454     gint16 * odata, guint n)
455 {
456   if (pan == 0.0) {
457     audiopanoramam_orc_process_s16_ch2_none (odata, idata, n);
458   } else if (pan > 0.0) {
459     gfloat lpan = 1.0 - pan;
460     audiopanoramam_orc_process_s16_ch2_sim_left (odata, idata, lpan, n);
461   } else {
462     gfloat rpan = 1.0 + pan;
463     audiopanoramam_orc_process_s16_ch2_sim_right (odata, idata, rpan, n);
464   }
465 }
466 
467 static void
gst_audio_panorama_m2s_float_simple(gfloat pan,gfloat * idata,gfloat * odata,guint n)468 gst_audio_panorama_m2s_float_simple (gfloat pan, gfloat * idata,
469     gfloat * odata, guint n)
470 {
471   if (pan == 0.0) {
472     audiopanoramam_orc_process_f32_ch1_none (odata, idata, n);
473   } else if (pan > 0.0) {
474     gfloat lpan = 1.0 - pan;
475     audiopanoramam_orc_process_f32_ch1_sim_left (odata, idata, lpan, n);
476   } else {
477     gfloat rpan = 1.0 + pan;
478     audiopanoramam_orc_process_f32_ch1_sim_right (odata, idata, rpan, n);
479   }
480 }
481 
482 static void
gst_audio_panorama_s2s_float_simple(gfloat pan,gfloat * idata,gfloat * odata,guint n)483 gst_audio_panorama_s2s_float_simple (gfloat pan, gfloat * idata,
484     gfloat * odata, guint n)
485 {
486   if (pan == 0.0) {
487     audiopanoramam_orc_process_f32_ch2_none (odata, idata, n);
488   } else if (pan > 0.0) {
489     gfloat lpan = 1.0 - pan;
490     audiopanoramam_orc_process_f32_ch2_sim_left (odata, idata, lpan, n);
491   } else {
492     gfloat rpan = 1.0 + pan;
493     audiopanoramam_orc_process_f32_ch2_sim_right (odata, idata, rpan, n);
494   }
495 }
496 
497 /* this function does the actual processing
498  */
499 static GstFlowReturn
gst_audio_panorama_transform(GstBaseTransform * base,GstBuffer * inbuf,GstBuffer * outbuf)500 gst_audio_panorama_transform (GstBaseTransform * base, GstBuffer * inbuf,
501     GstBuffer * outbuf)
502 {
503   GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
504   GstClockTime ts;
505   GstMapInfo inmap, outmap;
506 
507   ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME,
508       GST_BUFFER_TIMESTAMP (inbuf));
509 
510   if (GST_CLOCK_TIME_IS_VALID (ts)) {
511     GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
512     gst_object_sync_values (GST_OBJECT (filter), ts);
513   }
514 
515   gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
516 
517   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))) {
518     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
519     orc_memset (outmap.data, 0, outmap.size);
520   } else {
521     /* output is always stereo, input is mono or stereo,
522      * and info describes input format */
523     guint num_samples = outmap.size / (2 * GST_AUDIO_INFO_BPS (&filter->info));
524 
525     gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
526     filter->process (filter->panorama, inmap.data, outmap.data, num_samples);
527     gst_buffer_unmap (inbuf, &inmap);
528   }
529 
530   gst_buffer_unmap (outbuf, &outmap);
531 
532   return GST_FLOW_OK;
533 }
534