1 /* gstgoom.c: implementation of goom drawing element
2  * Copyright (C) <2001> Richard Boulton <richard@tartarus.org>
3  *           (C) <2006> Wim Taymans <wim at fluendo dot com>
4  *           (C) <2011> Wim Taymans <wim.taymans at gmail dot com>
5  *           (C) <2015> Luis de Bethencourt <luis@debethencourt.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 /**
24  * SECTION:element-goom
25  * @see_also: synaesthesia
26  *
27  * Goom is an audio visualisation element. It creates warping structures
28  * based on the incoming audio signal.
29  *
30  * <refsect2>
31  * <title>Example launch line</title>
32  * |[
33  * gst-launch-1.0 -v audiotestsrc ! goom ! videoconvert ! xvimagesink
34  * ]|
35  * </refsect2>
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 
42 #include <string.h>
43 #include "gstgoom.h"
44 #include "goom.h"
45 
46 #if HAVE_ORC
47 #include <orc/orc.h>
48 #endif
49 
50 GST_DEBUG_CATEGORY (goom_debug);
51 #define GST_CAT_DEFAULT goom_debug
52 
53 #define DEFAULT_WIDTH  320
54 #define DEFAULT_HEIGHT 240
55 
56 /* signals and args */
57 enum
58 {
59   /* FILL ME */
60   LAST_SIGNAL
61 };
62 
63 enum
64 {
65   ARG_0
66       /* FILL ME */
67 };
68 
69 #if G_BYTE_ORDER == G_BIG_ENDIAN
70 #define RGB_ORDER "xRGB"
71 #else
72 #define RGB_ORDER "BGRx"
73 #endif
74 
75 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
76     GST_PAD_SRC,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (RGB_ORDER))
79     );
80 
81 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",    /* the name of the pads */
82     GST_PAD_SINK,               /* type of the pad */
83     GST_PAD_ALWAYS,             /* ALWAYS/SOMETIMES */
84     GST_STATIC_CAPS ("audio/x-raw, "
85         "format = (string) " GST_AUDIO_NE (S16) ", "
86         "rate = (int) [ 8000, 96000 ], "
87         "channels = (int) 1, "
88         "layout = (string) interleaved; "
89         "audio/x-raw, "
90         "format = (string) " GST_AUDIO_NE (S16) ", "
91         "rate = (int) [ 8000, 96000 ], "
92         "channels = (int) 2, "
93         "channel-mask = (bitmask) 0x3, " "layout = (string) interleaved")
94     );
95 
96 
97 static void gst_goom_finalize (GObject * object);
98 
99 static gboolean gst_goom_setup (GstAudioVisualizer * base);
100 static gboolean gst_goom_render (GstAudioVisualizer * base, GstBuffer * audio,
101     GstVideoFrame * video);
102 
103 G_DEFINE_TYPE (GstGoom, gst_goom, GST_TYPE_AUDIO_VISUALIZER);
104 
105 static void
gst_goom_class_init(GstGoomClass * klass)106 gst_goom_class_init (GstGoomClass * klass)
107 {
108   GObjectClass *gobject_class;
109   GstElementClass *gstelement_class;
110   GstAudioVisualizerClass *visualizer_class;
111 
112   gobject_class = (GObjectClass *) klass;
113   gstelement_class = (GstElementClass *) klass;
114   visualizer_class = (GstAudioVisualizerClass *) klass;
115 
116   gobject_class->finalize = gst_goom_finalize;
117 
118   gst_element_class_set_static_metadata (gstelement_class, "GOOM: what a GOOM!",
119       "Visualization",
120       "Takes frames of data and outputs video frames using the GOOM filter",
121       "Wim Taymans <wim@fluendo.com>");
122   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
123   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
124 
125   visualizer_class->setup = GST_DEBUG_FUNCPTR (gst_goom_setup);
126   visualizer_class->render = GST_DEBUG_FUNCPTR (gst_goom_render);
127 }
128 
129 static void
gst_goom_init(GstGoom * goom)130 gst_goom_init (GstGoom * goom)
131 {
132   goom->width = DEFAULT_WIDTH;
133   goom->height = DEFAULT_HEIGHT;
134   goom->channels = 0;
135 
136   goom->plugin = goom_init (goom->width, goom->height);
137 }
138 
139 static void
gst_goom_finalize(GObject * object)140 gst_goom_finalize (GObject * object)
141 {
142   GstGoom *goom = GST_GOOM (object);
143 
144   goom_close (goom->plugin);
145   goom->plugin = NULL;
146 
147   G_OBJECT_CLASS (gst_goom_parent_class)->finalize (object);
148 }
149 
150 static gboolean
gst_goom_setup(GstAudioVisualizer * base)151 gst_goom_setup (GstAudioVisualizer * base)
152 {
153   GstGoom *goom = GST_GOOM (base);
154 
155   goom->width = GST_VIDEO_INFO_WIDTH (&base->vinfo);
156   goom->height = GST_VIDEO_INFO_HEIGHT (&base->vinfo);
157   goom_set_resolution (goom->plugin, goom->width, goom->height);
158 
159   return TRUE;
160 }
161 
162 static gboolean
gst_goom_render(GstAudioVisualizer * base,GstBuffer * audio,GstVideoFrame * video)163 gst_goom_render (GstAudioVisualizer * base, GstBuffer * audio,
164     GstVideoFrame * video)
165 {
166   GstGoom *goom = GST_GOOM (base);
167   GstMapInfo amap;
168   gint16 datain[2][GOOM_SAMPLES];
169   gint16 *adata;
170   gint i;
171 
172   /* get next GOOM_SAMPLES, we have at least this amount of samples */
173   gst_buffer_map (audio, &amap, GST_MAP_READ);
174   adata = (gint16 *) amap.data;
175 
176   if (goom->channels == 2) {
177     for (i = 0; i < GOOM_SAMPLES; i++) {
178       datain[0][i] = *adata++;
179       datain[1][i] = *adata++;
180     }
181   } else {
182     for (i = 0; i < GOOM_SAMPLES; i++) {
183       datain[0][i] = *adata;
184       datain[1][i] = *adata++;
185     }
186   }
187 
188   video->data[0] = goom_update (goom->plugin, datain, 0, 0);
189   gst_buffer_unmap (audio, &amap);
190 
191   return TRUE;
192 }
193 
194 static gboolean
plugin_init(GstPlugin * plugin)195 plugin_init (GstPlugin * plugin)
196 {
197   GST_DEBUG_CATEGORY_INIT (goom_debug, "goom", 0, "goom visualisation element");
198 
199 #if HAVE_ORC
200   orc_init ();
201 #endif
202 
203   return gst_element_register (plugin, "goom", GST_RANK_NONE, GST_TYPE_GOOM);
204 }
205 
206 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
207     GST_VERSION_MINOR,
208     goom,
209     "GOOM visualization filter",
210     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
211