remove copyright field from plugins
[platform/upstream/gst-plugins-good.git] / gst / smoothwave / gstsmoothwave.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <string.h>
25
26 #include "gstsmoothwave.h"
27
28 static GstElementDetails gst_smoothwave_details = GST_ELEMENT_DETAILS (
29   "Smooth waveform",
30   "Visualization",
31   "Fading grayscale waveform display",
32   "Erik Walthinsen <omega@cse.ogi.edu>"
33 );
34
35
36 /* SmoothWave signals and args */
37 enum {
38   /* FILL ME */
39   LAST_SIGNAL
40 };
41
42 enum {
43   ARG_0,
44   ARG_WIDTH,
45   ARG_HEIGHT,
46   ARG_WIDGET,
47 };
48
49 static void     gst_smoothwave_base_init        (gpointer g_class);
50 static void     gst_smoothwave_class_init       (GstSmoothWaveClass *klass);
51 static void     gst_smoothwave_init             (GstSmoothWave *smoothwave);
52
53 static void     gst_smoothwave_set_property             (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
54 static void     gst_smoothwave_get_property             (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
55
56 static void     gst_smoothwave_chain            (GstPad *pad, GstData *_data);
57
58 static GstElementClass *parent_class = NULL;
59 /*static guint gst_smoothwave_signals[LAST_SIGNAL] = { 0 }; */
60
61
62 GType
63 gst_smoothwave_get_type (void)
64 {
65   static GType smoothwave_type = 0;
66
67   if (!smoothwave_type) {
68     static const GTypeInfo smoothwave_info = {
69       sizeof(GstSmoothWaveClass),
70       gst_smoothwave_base_init,
71       NULL,
72       (GClassInitFunc)gst_smoothwave_class_init,
73       NULL,
74       NULL,
75       sizeof(GstSmoothWave),
76       0,
77       (GInstanceInitFunc)gst_smoothwave_init,
78     };
79     smoothwave_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSmoothWave", &smoothwave_info, 0);
80   }
81   return smoothwave_type;
82 }
83
84 static void
85 gst_smoothwave_base_init (gpointer g_class)
86 {
87   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
88
89   gst_element_class_set_details (element_class, &gst_smoothwave_details);
90 }
91
92 static void
93 gst_smoothwave_class_init (GstSmoothWaveClass *klass)
94 {
95   GObjectClass *gobject_class;
96   GstElementClass *gstelement_class;
97
98   gobject_class = (GObjectClass*)klass;
99   gstelement_class = (GstElementClass*)klass;
100
101   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
102
103   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_WIDTH,
104     g_param_spec_int("width","width","width",
105                      G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
106   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_HEIGHT,
107     g_param_spec_int("height","height","height",
108                      G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
109   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_WIDGET,
110     g_param_spec_object("widget","widget","widget",
111                         GTK_TYPE_WIDGET,G_PARAM_READABLE)); /* CHECKME! */
112
113   gobject_class->set_property = gst_smoothwave_set_property;
114   gobject_class->get_property = gst_smoothwave_get_property;
115 }
116
117 static void
118 gst_smoothwave_init (GstSmoothWave *smoothwave)
119 {
120   int i;
121   guint32 palette[256];
122
123   smoothwave->sinkpad = gst_pad_new("sink",GST_PAD_SINK);
124   gst_element_add_pad(GST_ELEMENT(smoothwave),smoothwave->sinkpad);
125   gst_pad_set_chain_function(smoothwave->sinkpad,gst_smoothwave_chain);
126   smoothwave->srcpad = gst_pad_new("src",GST_PAD_SRC);
127   gst_element_add_pad(GST_ELEMENT(smoothwave),smoothwave->srcpad);
128
129 /*  smoothwave->meta = NULL; */
130   smoothwave->width = 512;
131   smoothwave->height = 256;
132
133   gdk_rgb_init();
134 /*  gtk_widget_set_default_colormap (gdk_rgb_get_cmap()); */
135 /*  gtk_widget_set_default_visual (gdk_rgb_get_visual()); */
136
137 /*  GST_DEBUG ("creating palette"); */
138   for (i=0;i<256;i++)
139     palette[i] = (i << 16) || (i << 8);
140 /*  GST_DEBUG ("creating cmap"); */
141   smoothwave->cmap = gdk_rgb_cmap_new(palette,256);
142 /*  GST_DEBUG ("created cmap"); */
143 /*  gtk_widget_set_default_colormap (smoothwave->cmap); */
144
145   smoothwave->image = gtk_drawing_area_new();
146   gtk_drawing_area_size(GTK_DRAWING_AREA(smoothwave->image),
147                         smoothwave->width,smoothwave->height);
148   gtk_widget_show(smoothwave->image);
149
150   smoothwave->imagebuffer = g_malloc(smoothwave->width*smoothwave->height);
151   memset(smoothwave->imagebuffer,0,smoothwave->width*smoothwave->height);
152 }
153
154 static void
155 gst_smoothwave_chain (GstPad *pad, GstData *_data)
156 {
157   GstBuffer *buf = GST_BUFFER (_data);
158   GstSmoothWave *smoothwave;
159   gint16 *samples;
160   gint samplecount,i;
161   register guint32 *ptr;
162   gint qheight;
163
164   g_return_if_fail(pad != NULL);
165   g_return_if_fail(GST_IS_PAD(pad));
166   g_return_if_fail(buf != NULL);
167 /*  g_return_if_fail(GST_IS_BUFFER(buf)); */
168
169   smoothwave = GST_SMOOTHWAVE(GST_OBJECT_PARENT (pad));
170
171   /* first deal with audio metadata */
172 #if 0
173   if (buf->meta) {
174     if (smoothwave->meta != NULL) {
175       /* FIXME: need to unref the old metadata so it goes away */
176     }
177     /* we just make a copy of the pointer */
178     smoothwave->meta = (MetaAudioRaw *)(buf->meta);
179     /* FIXME: now we have to ref the metadata so it doesn't go away */
180   }
181 #endif
182
183 /*  g_return_if_fail(smoothwave->meta != NULL); */
184
185   samples = (gint16 *)GST_BUFFER_DATA(buf);
186 /*  samplecount = buf->datasize / (smoothwave->meta->channels * sizeof(gint16)); */
187   samplecount = GST_BUFFER_SIZE(buf) / (2 * sizeof(gint16));
188
189   qheight = smoothwave->height/4;
190
191 /*  GST_DEBUG ("traversing %d",smoothwave->width); */
192   for (i=0;i<MAX(smoothwave->width,samplecount);i++) {
193     gint16 y1 = (gint32)(samples[i*2] * qheight) / 32768 +
194                 qheight;
195     gint16 y2 = (gint32)(samples[(i*2)+1] * qheight) / 32768 +
196                 (qheight*3);
197     smoothwave->imagebuffer[y1*smoothwave->width + i] = 0xff;
198     smoothwave->imagebuffer[y2*smoothwave->width + i] = 0xff;
199 /*    smoothwave->imagebuffer[i+(smoothwave->width*5)] = i; */
200   }
201
202   ptr = (guint32 *)smoothwave->imagebuffer;
203   for (i=0;i<(smoothwave->width*smoothwave->height)/4;i++) {
204     if (*ptr){
205       *ptr -= ((*ptr & 0xf0f0f0f0ul) >> 4) + ((*ptr & 0xe0e0e0e0ul) >> 5);
206       ptr++;
207     }else{
208       ptr++;
209     }
210   }
211
212 /*  GST_DEBUG ("drawing"); */
213 /*  GST_DEBUG ("gdk_draw_indexed_image(%p,%p,%d,%d,%d,%d,%s,%p,%d,%p);",
214         smoothwave->image->window,
215         smoothwave->image->style->fg_gc[GTK_STATE_NORMAL],
216         0,0,smoothwave->width,smoothwave->height,
217         "GDK_RGB_DITHER_NORMAL",
218         smoothwave->imagebuffer,smoothwave->width,
219         smoothwave->cmap);*/
220 /*  gdk_draw_indexed_image(smoothwave->image->window,
221         smoothwave->image->style->fg_gc[GTK_STATE_NORMAL],
222         0,0,smoothwave->width,smoothwave->height,
223         GDK_RGB_DITHER_NONE,
224         smoothwave->imagebuffer,smoothwave->width,
225         smoothwave->cmap);*/
226   gdk_draw_gray_image(smoothwave->image->window,
227         smoothwave->image->style->fg_gc[GTK_STATE_NORMAL],
228         0,0,smoothwave->width,smoothwave->height,
229         GDK_RGB_DITHER_NORMAL,
230         smoothwave->imagebuffer,smoothwave->width);
231
232 /*  gst_trace_add_entry(NULL,0,buf,"smoothwave: calculated smoothwave"); */
233
234   gst_buffer_unref(buf);
235 }
236
237 static void
238 gst_smoothwave_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
239 {
240   GstSmoothWave *smoothwave;
241
242   /* it's not null if we got it, but it might not be ours */
243   g_return_if_fail(GST_IS_SMOOTHWAVE(object));
244   smoothwave = GST_SMOOTHWAVE(object);
245
246   switch (prop_id) {
247     case ARG_WIDTH:
248       smoothwave->width = g_value_get_int (value);
249       gtk_drawing_area_size(GTK_DRAWING_AREA(smoothwave->image),
250                             smoothwave->width,smoothwave->height);
251       gtk_widget_set_usize(GTK_WIDGET(smoothwave->image),
252                            smoothwave->width,smoothwave->height);
253       break;
254     case ARG_HEIGHT:
255       smoothwave->height = g_value_get_int (value);
256       gtk_drawing_area_size(GTK_DRAWING_AREA(smoothwave->image),
257                             smoothwave->width,smoothwave->height);
258       gtk_widget_set_usize(GTK_WIDGET(smoothwave->image),
259                            smoothwave->width,smoothwave->height);
260       break;
261     default:
262       break;
263   }
264 }
265
266 static void
267 gst_smoothwave_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
268 {
269   GstSmoothWave *smoothwave;
270
271   /* it's not null if we got it, but it might not be ours */
272   smoothwave = GST_SMOOTHWAVE(object);
273
274   switch (prop_id) {
275     case ARG_WIDTH: {
276       g_value_set_int (value, smoothwave->width);
277       break;
278     }
279     case ARG_HEIGHT: {
280       g_value_set_int (value, smoothwave->height);
281       break;
282     }
283     case ARG_WIDGET: {
284       g_value_set_object (value, smoothwave->image);
285       break;
286     }
287     default: {
288       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289       break;
290     }
291   }
292 }
293
294
295
296 static gboolean
297 plugin_init (GstPlugin *plugin)
298 {
299   if (!gst_element_register (plugin, "smoothwave", GST_RANK_NONE, GST_TYPE_SMOOTHWAVE))
300     return FALSE;
301
302   return TRUE;
303 }
304
305 GST_PLUGIN_DEFINE (
306   GST_VERSION_MAJOR,
307   GST_VERSION_MINOR,
308   "smoothwave",
309   "Fading greyscale waveform display",
310   plugin_init,
311   VERSION,
312   "LGPL",
313   GST_PACKAGE,
314   GST_ORIGIN)