0d7212cbccae0be568ff93dd729c0c0d19bb2dc9
[platform/upstream/gst-plugins-good.git] / gst / monoscope / gstmonoscope.c
1 /* gstmonoscope.c: implementation of monoscope drawing element
2  * Copyright (C) <2002> Richard Boulton <richard@tartarus.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25 #include <gst/video/video.h>
26 #include <gst/audio/audio.h>
27 #include "monoscope.h"
28
29 #define GST_TYPE_MONOSCOPE (gst_monoscope_get_type())
30 #define GST_MONOSCOPE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MONOSCOPE,GstMonoscope))
31 #define GST_MONOSCOPE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MONOSCOPE,GstMonoscope))
32 #define GST_IS_MONOSCOPE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MONOSCOPE))
33 #define GST_IS_MONOSCOPE_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MONOSCOPE))
34
35 typedef struct _GstMonoscope GstMonoscope;
36 typedef struct _GstMonoscopeClass GstMonoscopeClass;
37
38 struct _GstMonoscope {
39   GstElement element;
40
41   /* pads */
42   GstPad *sinkpad,*srcpad;
43
44   /* the timestamp of the next frame */
45   guint64 next_time;
46   gint16 datain[512];
47
48   /* video state */
49   gdouble fps;
50   gint width;
51   gint height;
52   gboolean first_buffer;
53
54   /* visualisation state */
55   struct monoscope_state * visstate;
56 };
57
58 struct _GstMonoscopeClass {
59   GstElementClass parent_class;
60 };
61
62 GType gst_monoscope_get_type(void);
63
64
65 /* elementfactory information */
66 static GstElementDetails gst_monoscope_details = {
67   "Monoscope",
68   "Visualization",
69   "Displays a highly stabilised waveform of audio input",
70   "Richard Boulton <richard@tartarus.org>"
71 };
72
73 /* signals and args */
74 enum {
75   /* FILL ME */
76   LAST_SIGNAL
77 };
78
79 enum {
80   ARG_0,
81   /* FILL ME */
82 };
83
84 static GstStaticPadTemplate src_template =
85 GST_STATIC_PAD_TEMPLATE (
86   "src",
87   GST_PAD_SRC,
88   GST_PAD_ALWAYS,
89   GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
90 );
91
92 static GstStaticPadTemplate sink_template =
93 GST_STATIC_PAD_TEMPLATE (
94   "sink",
95   GST_PAD_SINK,
96   GST_PAD_ALWAYS,
97   GST_STATIC_CAPS (GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_CAPS)
98 );
99
100
101 static void     gst_monoscope_class_init        (GstMonoscopeClass *klass);
102 static void     gst_monoscope_base_init         (GstMonoscopeClass *klass);
103 static void     gst_monoscope_init              (GstMonoscope *monoscope);
104
105 static void     gst_monoscope_chain             (GstPad *pad, GstData *_data);
106
107 static GstPadLinkReturn
108                 gst_monoscope_srcconnect        (GstPad *pad, const GstCaps *caps);
109
110 static GstElementClass *parent_class = NULL;
111
112 GType
113 gst_monoscope_get_type (void)
114 {
115   static GType type = 0;
116
117   if (!type) {
118     static const GTypeInfo info = {
119       sizeof (GstMonoscopeClass),      
120       (GBaseInitFunc) gst_monoscope_base_init,      
121       NULL,      
122       (GClassInitFunc) gst_monoscope_class_init,
123       NULL,
124       NULL,
125       sizeof (GstMonoscope),
126       0,
127       (GInstanceInitFunc) gst_monoscope_init,
128     };
129     type = g_type_register_static (GST_TYPE_ELEMENT, "GstMonoscope", &info, 0);
130   }
131   return type;
132 }
133
134 static void
135 gst_monoscope_base_init (GstMonoscopeClass *klass)
136 {
137   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
138
139   gst_element_class_add_pad_template (element_class,
140                 gst_static_pad_template_get (&src_template));
141   gst_element_class_add_pad_template (element_class,
142                 gst_static_pad_template_get (&sink_template));
143   gst_element_class_set_details (element_class, &gst_monoscope_details);
144 }
145
146 static void
147 gst_monoscope_class_init(GstMonoscopeClass *klass)
148 {
149   GObjectClass *gobject_class;
150   GstElementClass *gstelement_class;
151
152   gobject_class = (GObjectClass*) klass;
153   gstelement_class = (GstElementClass*) klass;
154
155   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
156 }
157
158 static void
159 gst_monoscope_init (GstMonoscope *monoscope)
160 {
161   /* create the sink and src pads */
162   monoscope->sinkpad = gst_pad_new_from_template (
163                   gst_static_pad_template_get (&sink_template ), "sink");
164   monoscope->srcpad = gst_pad_new_from_template (
165                   gst_static_pad_template_get (&src_template ), "src");
166   gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
167   gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
168
169   gst_pad_set_chain_function (monoscope->sinkpad, gst_monoscope_chain);
170   gst_pad_set_link_function (monoscope->srcpad, gst_monoscope_srcconnect);
171
172   monoscope->next_time = 0;
173
174   /* reset the initial video state */
175   monoscope->first_buffer = TRUE;
176   monoscope->width = 256;
177   monoscope->height = 128;
178   monoscope->fps = 25.; /* desired frame rate */
179 }
180
181 static GstPadLinkReturn
182 gst_monoscope_srcconnect (GstPad *pad, const GstCaps *caps)
183 {
184   GstMonoscope *monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
185   GstStructure *structure;
186
187   structure = gst_caps_get_structure (caps, 0);
188
189   gst_structure_get_int (structure, "width", &monoscope->width);
190   gst_structure_get_int (structure, "height", &monoscope->height);
191   gst_structure_get_double (structure, "framerate", &monoscope->fps);
192
193   return GST_PAD_LINK_OK;
194 }
195
196 static void
197 gst_monoscope_chain (GstPad *pad, GstData *_data)
198 {
199   GstBuffer *bufin = GST_BUFFER (_data);
200   GstMonoscope *monoscope;
201   GstBuffer *bufout;
202   guint32 samples_in;
203   gint16 *data;
204   gint i;
205
206   monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
207
208   GST_DEBUG ("Monoscope: chainfunc called");
209
210   samples_in = GST_BUFFER_SIZE (bufin) / sizeof (gint16);
211
212   GST_DEBUG ("input buffer has %d samples", samples_in);
213
214   /* FIXME: should really select the first 1024 samples after the timestamp. */
215   if (GST_BUFFER_TIMESTAMP (bufin) < monoscope->next_time || samples_in < 1024) {
216     GST_DEBUG ("timestamp is %" G_GUINT64_FORMAT ": want >= %" G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (bufin), monoscope->next_time);
217     gst_buffer_unref (bufin);
218     return;
219   }
220
221   data = (gint16 *) GST_BUFFER_DATA (bufin);
222   /* FIXME: Select samples in a better way. */
223   for (i=0; i < 512; i++) {
224     monoscope->datain[i] = *data++;
225   }
226
227   if (monoscope->first_buffer) {
228     monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
229     g_assert(monoscope->visstate != 0);
230     GST_DEBUG ("making new pad");
231     if (!gst_pad_is_negotiated (monoscope->srcpad)) {
232       if (gst_pad_renegotiate (monoscope->srcpad) <= 0) {
233         GST_ELEMENT_ERROR (monoscope, CORE, NEGOTIATION, NULL, NULL);
234         return;
235       }
236     }
237     monoscope->first_buffer = FALSE;
238   }
239
240   bufout = gst_buffer_new ();
241   GST_BUFFER_SIZE (bufout) = monoscope->width * monoscope->height * 4;
242   GST_BUFFER_DATA (bufout) = (guchar *) monoscope_update (monoscope->visstate, monoscope->datain);
243   GST_BUFFER_TIMESTAMP (bufout) = monoscope->next_time;
244   GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
245
246   monoscope->next_time += GST_SECOND / monoscope->fps;
247
248   gst_pad_push (monoscope->srcpad, GST_DATA (bufout));
249
250   gst_buffer_unref (bufin);
251
252   GST_DEBUG ("Monoscope: exiting chainfunc");
253
254 }
255
256 static gboolean
257 plugin_init (GstPlugin *plugin)
258 {
259   return gst_element_register(plugin, "monoscope",
260                               GST_RANK_NONE, GST_TYPE_MONOSCOPE);
261 }
262
263 GST_PLUGIN_DEFINE (
264   GST_VERSION_MAJOR,
265   GST_VERSION_MINOR,
266   "monoscope",
267   "Monoscope visualization",
268   plugin_init,
269   VERSION,
270   "LGPL",
271   GST_PACKAGE,
272   GST_ORIGIN
273 )