a7a3715561bca4e584493203783fbfecf7ba0b62
[platform/upstream/gstreamer.git] / gst / scopes / gstspectrascope.c
1 /* GStreamer
2  * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstspectrascope.c: simple oscilloscope
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  * Wavescope is a simple audio visualisation element. It renders the waveforms
25  * like on an oscilloscope.
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 (GstBaseScope * scope);
62 static gboolean gst_spectra_scope_render (GstBaseScope * scope,
63     GstBuffer * audio, GstBuffer * video);
64
65
66 GST_BOILERPLATE (GstSpectraScope, gst_spectra_scope, GstBaseScope,
67     GST_TYPE_BASE_SCOPE);
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, "Waveform oscilloscope",
75       "Visualization",
76       "Simple waveform oscilloscope", "Stefan Kost <ensonic@users.sf.net>");
77
78   gst_element_class_add_pad_template (element_class,
79       gst_static_pad_template_get (&gst_spectra_scope_src_template));
80   gst_element_class_add_pad_template (element_class,
81       gst_static_pad_template_get (&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   GstBaseScopeClass *scope_class = (GstBaseScopeClass *) g_class;
89
90   gobject_class->finalize = gst_spectra_scope_finalize;
91
92   scope_class->setup = GST_DEBUG_FUNCPTR (gst_spectra_scope_setup);
93   scope_class->render = GST_DEBUG_FUNCPTR (gst_spectra_scope_render);
94 }
95
96 static void
97 gst_spectra_scope_init (GstSpectraScope * scope, GstSpectraScopeClass * g_class)
98 {
99   /* do nothing */
100 }
101
102 static void
103 gst_spectra_scope_finalize (GObject * object)
104 {
105   GstSpectraScope *scope = GST_SPECTRA_SCOPE (object);
106
107   if (scope->fft_ctx) {
108     gst_fft_s16_free (scope->fft_ctx);
109     scope->fft_ctx = NULL;
110   }
111   if (scope->freq_data) {
112     g_free (scope->freq_data);
113     scope->freq_data = NULL;
114   }
115
116   G_OBJECT_CLASS (parent_class)->finalize (object);
117 }
118
119 static gboolean
120 gst_spectra_scope_setup (GstBaseScope * bscope)
121 {
122   GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
123
124   if (scope->fft_ctx)
125     gst_fft_s16_free (scope->fft_ctx);
126   if (scope->freq_data)
127     g_free (scope->freq_data);
128   scope->fft_ctx = gst_fft_s16_new (bscope->width * 2 - 2, FALSE);
129   scope->freq_data = g_new (GstFFTS16Complex, bscope->width);
130   return TRUE;
131 }
132
133 static gboolean
134 gst_spectra_scope_render (GstBaseScope * bscope, GstBuffer * audio,
135     GstBuffer * video)
136 {
137   GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
138   guint8 *vdata = GST_BUFFER_DATA (video);
139   gint16 *adata = (gint16 *) GST_BUFFER_DATA (audio);
140   GstFFTS16Complex *fdata = scope->freq_data;
141   guint x, y, off;
142   guint l, h = bscope->height - 1;
143   gfloat fr, fi;
144   guint bpp = gst_video_format_get_pixel_stride (bscope->video_format, 0);
145   guint bpl = bpp * bscope->width;
146
147   if (bscope->channels > 1) {
148     gint ch = bscope->channels;
149     gint num_samples = GST_BUFFER_SIZE (audio) / (ch * sizeof (gint16));
150     gint i, c, v, s = 0;
151
152     /* deinterleave and mixdown adata */
153     for (i = 0; i < num_samples; i++) {
154       v = 0;
155       for (c = 0; c < ch; c++) {
156         v += adata[s++];
157       }
158       adata[i] = v / ch;
159     }
160   }
161
162   /* run fft */
163   gst_fft_s16_window (scope->fft_ctx, adata, GST_FFT_WINDOW_HAMMING);
164   gst_fft_s16_fft (scope->fft_ctx, adata, fdata);
165
166   /* draw lines */
167   for (x = 0; x < bscope->width; x++) {
168     /* figure out the range so that we don't need to clip,
169      * or even better do a log mapping? */
170     fr = (gfloat) fdata[x].r / 2048.0;
171     fi = (gfloat) fdata[x].i / 2048.0;
172     y = (guint) (h * fabs (fr * fr + fi * fi));
173     if (y > h)
174       y = h;
175     y = h - y;
176     off = (y * bpl) + (x * bpp);
177     vdata[off + 0] = 0xFF;
178     vdata[off + 1] = 0xFF;
179     vdata[off + 2] = 0xFF;
180     for (l = y + 1; l <= h; l++) {
181       off += bpl;
182       vdata[off + 0] = 0x7F;
183       vdata[off + 1] = 0x7F;
184       vdata[off + 2] = 0x7F;
185     }
186   }
187   return TRUE;
188 }
189
190 gboolean
191 gst_spectra_scope_plugin_init (GstPlugin * plugin)
192 {
193   GST_DEBUG_CATEGORY_INIT (spectra_scope_debug, "spectrascope", 0,
194       "spectrascope");
195
196   return gst_element_register (plugin, "spectrascope", GST_RANK_NONE,
197       GST_TYPE_SPECTRA_SCOPE);
198 }