first batch : remove ',' at end of enums as they could confuse older gcc, foreign...
[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 {
40   GstElement element;
41
42   /* pads */
43   GstPad *sinkpad, *srcpad;
44
45   /* the timestamp of the next frame */
46   guint64 next_time;
47   gint16 datain[512];
48
49   /* video state */
50   gdouble fps;
51   gint width;
52   gint height;
53   gboolean first_buffer;
54
55   /* visualisation state */
56   struct monoscope_state *visstate;
57 };
58
59 struct _GstMonoscopeClass
60 {
61   GstElementClass parent_class;
62 };
63
64 GType gst_monoscope_get_type (void);
65
66
67 /* elementfactory information */
68 static GstElementDetails gst_monoscope_details = {
69   "Monoscope",
70   "Visualization",
71   "Displays a highly stabilised waveform of audio input",
72   "Richard Boulton <richard@tartarus.org>"
73 };
74
75 /* signals and args */
76 enum
77 {
78   /* FILL ME */
79   LAST_SIGNAL
80 };
81
82 enum
83 {
84   ARG_0
85       /* FILL ME */
86 };
87
88 #if G_BYTE_ORDER == G_BIG_ENDIAN
89 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
90     GST_PAD_SRC,
91     GST_PAD_ALWAYS,
92     GST_STATIC_CAPS ("video/x-raw-rgb, "
93         "bpp = (int) 32, "
94         "depth = (int) 24, "
95         "endianness = (int) BIG_ENDIAN, "
96         "red_mask = (int) " GST_VIDEO_BYTE2_MASK_32 ", "
97         "green_mask = (int) " GST_VIDEO_BYTE3_MASK_32 ", "
98         "blue_mask = (int) " GST_VIDEO_BYTE4_MASK_32 ", "
99         "width = (int)256, "
100         "height = (int)128, " "framerate = " GST_VIDEO_FPS_RANGE)
101     );
102 #else
103 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
104     GST_PAD_SRC,
105     GST_PAD_ALWAYS,
106     GST_STATIC_CAPS ("video/x-raw-rgb, "
107         "bpp = (int) 32, "
108         "depth = (int) 24, "
109         "endianness = (int) BIG_ENDIAN, "
110         "red_mask = (int) " GST_VIDEO_BYTE3_MASK_32 ", "
111         "green_mask = (int) " GST_VIDEO_BYTE2_MASK_32 ", "
112         "blue_mask = (int) " GST_VIDEO_BYTE1_MASK_32 ", "
113         "width = (int)256, "
114         "height = (int)128, " "framerate = " GST_VIDEO_FPS_RANGE)
115     );
116 #endif
117
118 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
119     GST_PAD_SINK,
120     GST_PAD_ALWAYS,
121     GST_STATIC_CAPS (GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_CAPS)
122     );
123
124
125 static void gst_monoscope_class_init (GstMonoscopeClass * klass);
126 static void gst_monoscope_base_init (GstMonoscopeClass * klass);
127 static void gst_monoscope_init (GstMonoscope * monoscope);
128
129 static void gst_monoscope_chain (GstPad * pad, GstData * _data);
130
131 static GstPadLinkReturn
132 gst_monoscope_srcconnect (GstPad * pad, const GstCaps * caps);
133
134 static GstElementClass *parent_class = NULL;
135
136 GType
137 gst_monoscope_get_type (void)
138 {
139   static GType type = 0;
140
141   if (!type) {
142     static const GTypeInfo info = {
143       sizeof (GstMonoscopeClass),
144       (GBaseInitFunc) gst_monoscope_base_init,
145       NULL,
146       (GClassInitFunc) gst_monoscope_class_init,
147       NULL,
148       NULL,
149       sizeof (GstMonoscope),
150       0,
151       (GInstanceInitFunc) gst_monoscope_init,
152     };
153
154     type = g_type_register_static (GST_TYPE_ELEMENT, "GstMonoscope", &info, 0);
155   }
156   return type;
157 }
158
159 static void
160 gst_monoscope_base_init (GstMonoscopeClass * klass)
161 {
162   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
163
164   gst_element_class_add_pad_template (element_class,
165       gst_static_pad_template_get (&src_template));
166   gst_element_class_add_pad_template (element_class,
167       gst_static_pad_template_get (&sink_template));
168   gst_element_class_set_details (element_class, &gst_monoscope_details);
169 }
170
171 static void
172 gst_monoscope_class_init (GstMonoscopeClass * klass)
173 {
174   GObjectClass *gobject_class;
175   GstElementClass *gstelement_class;
176
177   gobject_class = (GObjectClass *) klass;
178   gstelement_class = (GstElementClass *) klass;
179
180   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
181 }
182
183 static void
184 gst_monoscope_init (GstMonoscope * monoscope)
185 {
186   /* create the sink and src pads */
187   monoscope->sinkpad =
188       gst_pad_new_from_template (gst_static_pad_template_get (&sink_template),
189       "sink");
190   monoscope->srcpad =
191       gst_pad_new_from_template (gst_static_pad_template_get (&src_template),
192       "src");
193   gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
194   gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
195
196   gst_pad_set_chain_function (monoscope->sinkpad, gst_monoscope_chain);
197   gst_pad_set_link_function (monoscope->srcpad, gst_monoscope_srcconnect);
198
199   monoscope->next_time = 0;
200
201   /* reset the initial video state */
202   monoscope->first_buffer = TRUE;
203   monoscope->width = 256;
204   monoscope->height = 128;
205   monoscope->fps = 25.;         /* desired frame rate */
206 }
207
208 static GstPadLinkReturn
209 gst_monoscope_srcconnect (GstPad * pad, const GstCaps * caps)
210 {
211   GstMonoscope *monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
212   GstStructure *structure;
213
214   structure = gst_caps_get_structure (caps, 0);
215
216   gst_structure_get_int (structure, "width", &monoscope->width);
217   gst_structure_get_int (structure, "height", &monoscope->height);
218   gst_structure_get_double (structure, "framerate", &monoscope->fps);
219
220   return GST_PAD_LINK_OK;
221 }
222
223 static void
224 gst_monoscope_chain (GstPad * pad, GstData * _data)
225 {
226   GstBuffer *bufin = GST_BUFFER (_data);
227   GstMonoscope *monoscope;
228   GstBuffer *bufout;
229   guint32 samples_in;
230   gint16 *data;
231   gint i;
232
233   monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
234
235   GST_DEBUG ("Monoscope: chainfunc called");
236
237   samples_in = GST_BUFFER_SIZE (bufin) / sizeof (gint16);
238
239   GST_DEBUG ("input buffer has %d samples", samples_in);
240
241   /* FIXME: should really select the first 1024 samples after the timestamp. */
242   if (GST_BUFFER_TIMESTAMP (bufin) < monoscope->next_time || samples_in < 1024) {
243     GST_DEBUG ("timestamp is %" G_GUINT64_FORMAT ": want >= %" G_GUINT64_FORMAT,
244         GST_BUFFER_TIMESTAMP (bufin), monoscope->next_time);
245     gst_buffer_unref (bufin);
246     return;
247   }
248
249   data = (gint16 *) GST_BUFFER_DATA (bufin);
250   /* FIXME: Select samples in a better way. */
251   for (i = 0; i < 512; i++) {
252     monoscope->datain[i] = *data++;
253   }
254
255   if (monoscope->first_buffer) {
256     monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
257     g_assert (monoscope->visstate != 0);
258     GST_DEBUG ("making new pad");
259     if (!gst_pad_is_negotiated (monoscope->srcpad)) {
260       if (gst_pad_renegotiate (monoscope->srcpad) <= 0) {
261         GST_ELEMENT_ERROR (monoscope, CORE, NEGOTIATION, (NULL), (NULL));
262         return;
263       }
264     }
265     monoscope->first_buffer = FALSE;
266   }
267
268   bufout = gst_buffer_new ();
269   GST_BUFFER_SIZE (bufout) = monoscope->width * monoscope->height * 4;
270   GST_BUFFER_DATA (bufout) =
271       (guchar *) monoscope_update (monoscope->visstate, monoscope->datain);
272   GST_BUFFER_TIMESTAMP (bufout) = monoscope->next_time;
273   GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
274
275   monoscope->next_time += GST_SECOND / monoscope->fps;
276
277   gst_pad_push (monoscope->srcpad, GST_DATA (bufout));
278
279   gst_buffer_unref (bufin);
280
281   GST_DEBUG ("Monoscope: exiting chainfunc");
282
283 }
284
285 static gboolean
286 plugin_init (GstPlugin * plugin)
287 {
288   return gst_element_register (plugin, "monoscope",
289       GST_RANK_NONE, GST_TYPE_MONOSCOPE);
290 }
291
292 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
293     GST_VERSION_MINOR,
294     "monoscope",
295     "Monoscope visualization",
296     plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)