1 /* GStreamer
2 * Copyright (C) 2004 Benjamin Otte <otte@gnome.org>
3 * 2012 Stefan Sauer <ensonic@users.sf.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "visual.h"
26
27 GST_DEBUG_CATEGORY_EXTERN (libvisual_debug);
28 #define GST_CAT_DEFAULT (libvisual_debug)
29
30 /* amounf of samples before we can feed libvisual */
31 #define VISUAL_SAMPLES 512
32
33 #if G_BYTE_ORDER == G_BIG_ENDIAN
34 #define RGB_ORDER_CAPS "xRGB, RGB"
35 #else
36 #define RGB_ORDER_CAPS "BGRx, BGR"
37 #endif
38
39 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
40 GST_PAD_SRC,
41 GST_PAD_ALWAYS,
42 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (" { " RGB_ORDER_CAPS ", RGB16 } "))
43 );
44
45 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_PAD_SINK,
47 GST_PAD_ALWAYS,
48 GST_STATIC_CAPS ("audio/x-raw, "
49 "format = (string) " GST_AUDIO_NE (S16) ", "
50 "layout = (string) interleaved, " "channels = (int) { 1, 2 }, "
51 "rate = (int) { 8000, 11250, 22500, 32000, 44100, 48000, 96000 }")
52 );
53
54
55 static void gst_visual_init (GstVisual * visual);
56 static void gst_visual_finalize (GObject * object);
57
58 static gboolean gst_visual_setup (GstAudioVisualizer * bscope);
59 static gboolean gst_visual_render (GstAudioVisualizer * bscope,
60 GstBuffer * audio, GstVideoFrame * video);
61
62 static GstElementClass *parent_class = NULL;
63
64 GType
gst_visual_get_type(void)65 gst_visual_get_type (void)
66 {
67 static GType type = 0;
68
69 if (G_UNLIKELY (type == 0)) {
70 static const GTypeInfo info = {
71 sizeof (GstVisualClass),
72 NULL,
73 NULL,
74 gst_visual_class_init,
75 NULL,
76 NULL,
77 sizeof (GstVisual),
78 0,
79 (GInstanceInitFunc) gst_visual_init,
80 };
81
82 type =
83 g_type_register_static (GST_TYPE_AUDIO_VISUALIZER, "GstVisual",
84 &info, 0);
85 }
86 return type;
87 }
88
89 void
gst_visual_class_init(gpointer g_class,gpointer class_data)90 gst_visual_class_init (gpointer g_class, gpointer class_data)
91 {
92 GObjectClass *gobject_class = (GObjectClass *) g_class;
93 GstElementClass *element_class = (GstElementClass *) g_class;
94 GstAudioVisualizerClass *scope_class = (GstAudioVisualizerClass *) g_class;
95 GstVisualClass *klass = (GstVisualClass *) g_class;
96
97 klass->plugin = class_data;
98
99 if (class_data == NULL) {
100 parent_class = g_type_class_peek_parent (g_class);
101 } else {
102 gchar *longname = g_strdup_printf ("libvisual %s plugin v.%s",
103 klass->plugin->info->name, klass->plugin->info->version);
104
105 /* FIXME: improve to only register what plugin supports? */
106 gst_element_class_add_static_pad_template (element_class, &src_template);
107 gst_element_class_add_static_pad_template (element_class, &sink_template);
108
109 gst_element_class_set_metadata (element_class,
110 longname, "Visualization",
111 klass->plugin->info->about, "Benjamin Otte <otte@gnome.org>");
112
113 g_free (longname);
114 }
115
116 gobject_class->finalize = gst_visual_finalize;
117
118 scope_class->setup = GST_DEBUG_FUNCPTR (gst_visual_setup);
119 scope_class->render = GST_DEBUG_FUNCPTR (gst_visual_render);
120
121 gst_type_mark_as_plugin_api (GST_TYPE_VISUAL, 0);
122 }
123
124 static void
gst_visual_init(GstVisual * visual)125 gst_visual_init (GstVisual * visual)
126 {
127 /* do nothing */
128 }
129
130 static void
gst_visual_clear_actors(GstVisual * visual)131 gst_visual_clear_actors (GstVisual * visual)
132 {
133 if (visual->actor) {
134 visual_object_unref (VISUAL_OBJECT (visual->actor));
135 visual->actor = NULL;
136 }
137 if (visual->video) {
138 visual_object_unref (VISUAL_OBJECT (visual->video));
139 visual->video = NULL;
140 }
141 if (visual->audio) {
142 visual_object_unref (VISUAL_OBJECT (visual->audio));
143 visual->audio = NULL;
144 }
145 }
146
147 static void
gst_visual_finalize(GObject * object)148 gst_visual_finalize (GObject * object)
149 {
150 GstVisual *visual = GST_VISUAL (object);
151
152 gst_visual_clear_actors (visual);
153
154 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
155 }
156
157 static gboolean
gst_visual_setup(GstAudioVisualizer * bscope)158 gst_visual_setup (GstAudioVisualizer * bscope)
159 {
160 GstVisual *visual = GST_VISUAL (bscope);
161 gint depth;
162
163 gst_visual_clear_actors (visual);
164
165 /* FIXME: we need to know how many bits we actually have in memory */
166 depth = bscope->vinfo.finfo->pixel_stride[0];
167 if (bscope->vinfo.finfo->bits >= 8) {
168 depth *= 8;
169 }
170
171 visual->actor =
172 visual_actor_new (GST_VISUAL_GET_CLASS (visual)->plugin->info->plugname);
173 visual->video = visual_video_new ();
174 visual->audio = visual_audio_new ();
175 /* can't have a play without actors */
176 if (!visual->actor || !visual->video)
177 goto no_actors;
178
179 if (visual_actor_realize (visual->actor) != 0)
180 goto no_realize;
181
182 visual_actor_set_video (visual->actor, visual->video);
183
184 visual_video_set_depth (visual->video,
185 visual_video_depth_enum_from_value (depth));
186 visual_video_set_dimension (visual->video,
187 GST_VIDEO_INFO_WIDTH (&bscope->vinfo),
188 GST_VIDEO_INFO_HEIGHT (&bscope->vinfo));
189 visual_actor_video_negotiate (visual->actor, 0, FALSE, FALSE);
190
191 GST_DEBUG_OBJECT (visual, "WxH: %dx%d, bpp: %d, depth: %d",
192 GST_VIDEO_INFO_WIDTH (&bscope->vinfo),
193 GST_VIDEO_INFO_HEIGHT (&bscope->vinfo), visual->video->bpp, depth);
194
195 return TRUE;
196 /* ERRORS */
197 no_actors:
198 {
199 GST_ELEMENT_ERROR (visual, LIBRARY, INIT, (NULL),
200 ("could not create actors"));
201 gst_visual_clear_actors (visual);
202 return FALSE;
203 }
204 no_realize:
205 {
206 GST_ELEMENT_ERROR (visual, LIBRARY, INIT, (NULL),
207 ("could not realize actor"));
208 gst_visual_clear_actors (visual);
209 return FALSE;
210 }
211 }
212
213 static gboolean
gst_visual_render(GstAudioVisualizer * bscope,GstBuffer * audio,GstVideoFrame * video)214 gst_visual_render (GstAudioVisualizer * bscope, GstBuffer * audio,
215 GstVideoFrame * video)
216 {
217 GstVisual *visual = GST_VISUAL (bscope);
218 GstMapInfo amap;
219 const guint16 *adata;
220 gint i, channels;
221 gboolean res = TRUE;
222 VisBuffer *lbuf, *rbuf;
223 guint16 ldata[VISUAL_SAMPLES], rdata[VISUAL_SAMPLES];
224 VisAudioSampleRateType vrate;
225
226 visual_video_set_buffer (visual->video, GST_VIDEO_FRAME_PLANE_DATA (video,
227 0));
228 visual_video_set_pitch (visual->video, GST_VIDEO_FRAME_PLANE_STRIDE (video,
229 0));
230
231 channels = GST_AUDIO_INFO_CHANNELS (&bscope->ainfo);
232
233 gst_buffer_map (audio, &amap, GST_MAP_READ);
234 adata = (const guint16 *) amap.data;
235
236 lbuf = visual_buffer_new_with_buffer (ldata, sizeof (ldata), NULL);
237 rbuf = visual_buffer_new_with_buffer (rdata, sizeof (rdata), NULL);
238
239 if (channels == 2) {
240 for (i = 0; i < VISUAL_SAMPLES; i++) {
241 ldata[i] = *adata++;
242 rdata[i] = *adata++;
243 }
244 } else {
245 for (i = 0; i < VISUAL_SAMPLES; i++) {
246 ldata[i] = *adata;
247 rdata[i] = *adata++;
248 }
249 }
250
251 /* TODO(ensonic): move to setup */
252 switch (bscope->ainfo.rate) {
253 case 8000:
254 vrate = VISUAL_AUDIO_SAMPLE_RATE_8000;
255 break;
256 case 11250:
257 vrate = VISUAL_AUDIO_SAMPLE_RATE_11250;
258 break;
259 case 22500:
260 vrate = VISUAL_AUDIO_SAMPLE_RATE_22500;
261 break;
262 case 32000:
263 vrate = VISUAL_AUDIO_SAMPLE_RATE_32000;
264 break;
265 case 44100:
266 vrate = VISUAL_AUDIO_SAMPLE_RATE_44100;
267 break;
268 case 48000:
269 vrate = VISUAL_AUDIO_SAMPLE_RATE_48000;
270 break;
271 case 96000:
272 vrate = VISUAL_AUDIO_SAMPLE_RATE_96000;
273 break;
274 default:
275 visual_object_unref (VISUAL_OBJECT (lbuf));
276 visual_object_unref (VISUAL_OBJECT (rbuf));
277 GST_ERROR_OBJECT (visual, "unsupported rate %d", bscope->ainfo.rate);
278 res = FALSE;
279 goto done;
280 }
281
282 visual_audio_samplepool_input_channel (visual->audio->samplepool,
283 lbuf,
284 vrate, VISUAL_AUDIO_SAMPLE_FORMAT_S16,
285 (char *) VISUAL_AUDIO_CHANNEL_LEFT);
286 visual_audio_samplepool_input_channel (visual->audio->samplepool, rbuf,
287 vrate, VISUAL_AUDIO_SAMPLE_FORMAT_S16,
288 (char *) VISUAL_AUDIO_CHANNEL_RIGHT);
289
290 visual_object_unref (VISUAL_OBJECT (lbuf));
291 visual_object_unref (VISUAL_OBJECT (rbuf));
292
293 visual_audio_analyze (visual->audio);
294 visual_actor_run (visual->actor, visual->audio);
295 visual_video_set_buffer (visual->video, NULL);
296
297 GST_DEBUG_OBJECT (visual, "rendered one frame");
298 done:
299 gst_buffer_unmap (audio, &amap);
300
301 return res;
302 }
303