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_static_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
122 static void
gst_visual_init(GstVisual * visual)123 gst_visual_init (GstVisual * visual)
124 {
125 /* do nothing */
126 }
127
128 static void
gst_visual_clear_actors(GstVisual * visual)129 gst_visual_clear_actors (GstVisual * visual)
130 {
131 if (visual->actor) {
132 visual_object_unref (VISUAL_OBJECT (visual->actor));
133 visual->actor = NULL;
134 }
135 if (visual->video) {
136 visual_object_unref (VISUAL_OBJECT (visual->video));
137 visual->video = NULL;
138 }
139 if (visual->audio) {
140 visual_object_unref (VISUAL_OBJECT (visual->audio));
141 visual->audio = NULL;
142 }
143 }
144
145 static void
gst_visual_finalize(GObject * object)146 gst_visual_finalize (GObject * object)
147 {
148 GstVisual *visual = GST_VISUAL (object);
149
150 gst_visual_clear_actors (visual);
151
152 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
153 }
154
155 static gboolean
gst_visual_setup(GstAudioVisualizer * bscope)156 gst_visual_setup (GstAudioVisualizer * bscope)
157 {
158 GstVisual *visual = GST_VISUAL (bscope);
159 gint depth;
160
161 gst_visual_clear_actors (visual);
162
163 /* FIXME: we need to know how many bits we actually have in memory */
164 depth = bscope->vinfo.finfo->pixel_stride[0];
165 if (bscope->vinfo.finfo->bits >= 8) {
166 depth *= 8;
167 }
168
169 visual->actor =
170 visual_actor_new (GST_VISUAL_GET_CLASS (visual)->plugin->info->plugname);
171 visual->video = visual_video_new ();
172 visual->audio = visual_audio_new ();
173 /* can't have a play without actors */
174 if (!visual->actor || !visual->video)
175 goto no_actors;
176
177 if (visual_actor_realize (visual->actor) != 0)
178 goto no_realize;
179
180 visual_actor_set_video (visual->actor, visual->video);
181
182 visual_video_set_depth (visual->video,
183 visual_video_depth_enum_from_value (depth));
184 visual_video_set_dimension (visual->video,
185 GST_VIDEO_INFO_WIDTH (&bscope->vinfo),
186 GST_VIDEO_INFO_HEIGHT (&bscope->vinfo));
187 visual_actor_video_negotiate (visual->actor, 0, FALSE, FALSE);
188
189 GST_DEBUG_OBJECT (visual, "WxH: %dx%d, bpp: %d, depth: %d",
190 GST_VIDEO_INFO_WIDTH (&bscope->vinfo),
191 GST_VIDEO_INFO_HEIGHT (&bscope->vinfo), visual->video->bpp, depth);
192
193 return TRUE;
194 /* ERRORS */
195 no_actors:
196 {
197 GST_ELEMENT_ERROR (visual, LIBRARY, INIT, (NULL),
198 ("could not create actors"));
199 gst_visual_clear_actors (visual);
200 return FALSE;
201 }
202 no_realize:
203 {
204 GST_ELEMENT_ERROR (visual, LIBRARY, INIT, (NULL),
205 ("could not realize actor"));
206 gst_visual_clear_actors (visual);
207 return FALSE;
208 }
209 }
210
211 static gboolean
gst_visual_render(GstAudioVisualizer * bscope,GstBuffer * audio,GstVideoFrame * video)212 gst_visual_render (GstAudioVisualizer * bscope, GstBuffer * audio,
213 GstVideoFrame * video)
214 {
215 GstVisual *visual = GST_VISUAL (bscope);
216 GstMapInfo amap;
217 const guint16 *adata;
218 gint i, channels;
219 gboolean res = TRUE;
220 VisBuffer *lbuf, *rbuf;
221 guint16 ldata[VISUAL_SAMPLES], rdata[VISUAL_SAMPLES];
222 VisAudioSampleRateType vrate;
223
224 visual_video_set_buffer (visual->video, GST_VIDEO_FRAME_PLANE_DATA (video,
225 0));
226 visual_video_set_pitch (visual->video, GST_VIDEO_FRAME_PLANE_STRIDE (video,
227 0));
228
229 channels = GST_AUDIO_INFO_CHANNELS (&bscope->ainfo);
230
231 gst_buffer_map (audio, &amap, GST_MAP_READ);
232 adata = (const guint16 *) amap.data;
233
234 lbuf = visual_buffer_new_with_buffer (ldata, sizeof (ldata), NULL);
235 rbuf = visual_buffer_new_with_buffer (rdata, sizeof (rdata), NULL);
236
237 if (channels == 2) {
238 for (i = 0; i < VISUAL_SAMPLES; i++) {
239 ldata[i] = *adata++;
240 rdata[i] = *adata++;
241 }
242 } else {
243 for (i = 0; i < VISUAL_SAMPLES; i++) {
244 ldata[i] = *adata;
245 rdata[i] = *adata++;
246 }
247 }
248
249 /* TODO(ensonic): move to setup */
250 switch (bscope->ainfo.rate) {
251 case 8000:
252 vrate = VISUAL_AUDIO_SAMPLE_RATE_8000;
253 break;
254 case 11250:
255 vrate = VISUAL_AUDIO_SAMPLE_RATE_11250;
256 break;
257 case 22500:
258 vrate = VISUAL_AUDIO_SAMPLE_RATE_22500;
259 break;
260 case 32000:
261 vrate = VISUAL_AUDIO_SAMPLE_RATE_32000;
262 break;
263 case 44100:
264 vrate = VISUAL_AUDIO_SAMPLE_RATE_44100;
265 break;
266 case 48000:
267 vrate = VISUAL_AUDIO_SAMPLE_RATE_48000;
268 break;
269 case 96000:
270 vrate = VISUAL_AUDIO_SAMPLE_RATE_96000;
271 break;
272 default:
273 visual_object_unref (VISUAL_OBJECT (lbuf));
274 visual_object_unref (VISUAL_OBJECT (rbuf));
275 GST_ERROR_OBJECT (visual, "unsupported rate %d", bscope->ainfo.rate);
276 res = FALSE;
277 goto done;
278 }
279
280 visual_audio_samplepool_input_channel (visual->audio->samplepool,
281 lbuf,
282 vrate, VISUAL_AUDIO_SAMPLE_FORMAT_S16,
283 (char *) VISUAL_AUDIO_CHANNEL_LEFT);
284 visual_audio_samplepool_input_channel (visual->audio->samplepool, rbuf,
285 vrate, VISUAL_AUDIO_SAMPLE_FORMAT_S16,
286 (char *) VISUAL_AUDIO_CHANNEL_RIGHT);
287
288 visual_object_unref (VISUAL_OBJECT (lbuf));
289 visual_object_unref (VISUAL_OBJECT (rbuf));
290
291 visual_audio_analyze (visual->audio);
292 visual_actor_run (visual->actor, visual->audio);
293 visual_video_set_buffer (visual->video, NULL);
294
295 GST_DEBUG_OBJECT (visual, "rendered one frame");
296 done:
297 gst_buffer_unmap (audio, &amap);
298
299 return res;
300 }
301