Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / audiovisualizers / gstspectrascope.c
1 /* GStreamer
2  * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstspectrascope.c: frequency spectrum scope
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 /**
21  * SECTION:element-spectrascope
22  * @see_also: goom
23  *
24  * Spectrascope is a simple spectrum visualisation element. It renders the
25  * frequency spectrum as a series of bars.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
30  * gst-launch audiotestsrc ! audioconvert ! spectrascope ! ximagesink
31  * ]|
32  * </refsect2>
33  */
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 #include <stdlib.h>
38
39 #include "gstspectrascope.h"
40
41 static GstStaticPadTemplate gst_spectra_scope_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
43     GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
46     );
47
48 static GstStaticPadTemplate gst_spectra_scope_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS (GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_CAPS)
53     );
54
55
56 GST_DEBUG_CATEGORY_STATIC (spectra_scope_debug);
57 #define GST_CAT_DEFAULT spectra_scope_debug
58
59 static void gst_spectra_scope_finalize (GObject * object);
60
61 static gboolean gst_spectra_scope_setup (GstBaseAudioVisualizer * scope);
62 static gboolean gst_spectra_scope_render (GstBaseAudioVisualizer * scope,
63     GstBuffer * audio, GstBuffer * video);
64
65
66 GST_BOILERPLATE (GstSpectraScope, gst_spectra_scope, GstBaseAudioVisualizer,
67     GST_TYPE_BASE_AUDIO_VISUALIZER);
68
69 static void
70 gst_spectra_scope_base_init (gpointer g_class)
71 {
72   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
73
74   gst_element_class_set_details_simple (element_class,
75       "Frequency spectrum scope", "Visualization",
76       "Simple frequency spectrum scope", "Stefan Kost <ensonic@users.sf.net>");
77
78   gst_element_class_add_static_pad_template (element_class,
79       &gst_spectra_scope_src_template);
80   gst_element_class_add_static_pad_template (element_class,
81       &gst_spectra_scope_sink_template);
82 }
83
84 static void
85 gst_spectra_scope_class_init (GstSpectraScopeClass * g_class)
86 {
87   GObjectClass *gobject_class = (GObjectClass *) g_class;
88   GstBaseAudioVisualizerClass *scope_class =
89       (GstBaseAudioVisualizerClass *) g_class;
90
91   gobject_class->finalize = gst_spectra_scope_finalize;
92
93   scope_class->setup = GST_DEBUG_FUNCPTR (gst_spectra_scope_setup);
94   scope_class->render = GST_DEBUG_FUNCPTR (gst_spectra_scope_render);
95 }
96
97 static void
98 gst_spectra_scope_init (GstSpectraScope * scope, GstSpectraScopeClass * g_class)
99 {
100   /* do nothing */
101 }
102
103 static void
104 gst_spectra_scope_finalize (GObject * object)
105 {
106   GstSpectraScope *scope = GST_SPECTRA_SCOPE (object);
107
108   if (scope->fft_ctx) {
109     gst_fft_s16_free (scope->fft_ctx);
110     scope->fft_ctx = NULL;
111   }
112   if (scope->freq_data) {
113     g_free (scope->freq_data);
114     scope->freq_data = NULL;
115   }
116
117   G_OBJECT_CLASS (parent_class)->finalize (object);
118 }
119
120 static gboolean
121 gst_spectra_scope_setup (GstBaseAudioVisualizer * bscope)
122 {
123   GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
124   guint num_freq = bscope->width + 1;
125
126   if (scope->fft_ctx)
127     gst_fft_s16_free (scope->fft_ctx);
128   g_free (scope->freq_data);
129
130   /* we'd need this amount of samples per render() call */
131   bscope->req_spf = num_freq * 2 - 2;
132   scope->fft_ctx = gst_fft_s16_new (bscope->req_spf, FALSE);
133   scope->freq_data = g_new (GstFFTS16Complex, num_freq);
134
135   return TRUE;
136 }
137
138 static inline void
139 add_pixel (guint32 * _p, guint32 _c)
140 {
141   guint8 *p = (guint8 *) _p;
142   guint8 *c = (guint8 *) & _c;
143
144   if (p[0] < 255 - c[0])
145     p[0] += c[0];
146   else
147     p[0] = 255;
148   if (p[1] < 255 - c[1])
149     p[1] += c[1];
150   else
151     p[1] = 255;
152   if (p[2] < 255 - c[2])
153     p[2] += c[2];
154   else
155     p[2] = 255;
156   if (p[3] < 255 - c[3])
157     p[3] += c[3];
158   else
159     p[3] = 255;
160 }
161
162 static gboolean
163 gst_spectra_scope_render (GstBaseAudioVisualizer * bscope, GstBuffer * audio,
164     GstBuffer * video)
165 {
166   GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
167   guint32 *vdata = (guint32 *) GST_BUFFER_DATA (video);
168   gint16 *adata = (gint16 *) g_memdup (GST_BUFFER_DATA (audio),
169       GST_BUFFER_SIZE (audio));
170   GstFFTS16Complex *fdata = scope->freq_data;
171   guint x, y, off;
172   guint l, h = bscope->height - 1;
173   gfloat fr, fi;
174   guint w = bscope->width;
175
176   if (bscope->channels > 1) {
177     guint ch = bscope->channels;
178     guint num_samples = GST_BUFFER_SIZE (audio) / (ch * sizeof (gint16));
179     guint i, c, v, s = 0;
180
181     /* deinterleave and mixdown adata */
182     for (i = 0; i < num_samples; i++) {
183       v = 0;
184       for (c = 0; c < ch; c++) {
185         v += adata[s++];
186       }
187       adata[i] = v / ch;
188     }
189   }
190
191   /* run fft */
192   gst_fft_s16_window (scope->fft_ctx, adata, GST_FFT_WINDOW_HAMMING);
193   gst_fft_s16_fft (scope->fft_ctx, adata, fdata);
194   g_free (adata);
195
196   /* draw lines */
197   for (x = 0; x < bscope->width; x++) {
198     /* figure out the range so that we don't need to clip,
199      * or even better do a log mapping? */
200     fr = (gfloat) fdata[1 + x].r / 512.0;
201     fi = (gfloat) fdata[1 + x].i / 512.0;
202     y = (guint) (h * fabs (fr * fr + fi * fi));
203     if (y > h)
204       y = h;
205     y = h - y;
206     off = (y * w) + x;
207     vdata[off] = 0x00FFFFFF;
208     for (l = y + 1; l <= h; l++) {
209       off += w;
210       add_pixel (&vdata[off], 0x007F7F7F);
211     }
212   }
213   return TRUE;
214 }
215
216 gboolean
217 gst_spectra_scope_plugin_init (GstPlugin * plugin)
218 {
219   GST_DEBUG_CATEGORY_INIT (spectra_scope_debug, "spectrascope", 0,
220       "spectrascope");
221
222   return gst_element_register (plugin, "spectrascope", GST_RANK_NONE,
223       GST_TYPE_SPECTRA_SCOPE);
224 }