remove copyright field from plugins
[platform/upstream/gst-plugins-good.git] / gst / auparse / gstauparse.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 /* 2001/04/03 - Updated parseau to use caps nego
21  *              Zaheer Merali <zaheer@grid9.net
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gstauparse.h>
31
32 /* elementfactory information */
33 static GstElementDetails gst_auparse_details = GST_ELEMENT_DETAILS (
34   ".au parser",
35   "Codec/Parser/Audio",
36   "Parse an .au file into raw audio",
37   "Erik Walthinsen <omega@cse.ogi.edu>"
38 );
39
40 GST_PAD_TEMPLATE_FACTORY (sink_factory_templ,
41   "sink",
42   GST_PAD_SINK,
43   GST_PAD_ALWAYS,
44   GST_CAPS_NEW (
45     "auparse_sink",
46     "audio/x-au",
47     NULL
48   )
49 )
50
51 GST_PAD_TEMPLATE_FACTORY (src_factory_templ,
52   "src",
53   GST_PAD_SRC,
54   GST_PAD_ALWAYS,
55   GST_CAPS_NEW (
56     "auparse_src",
57     "audio/x-raw-int",
58       "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
59       "signed",     GST_PROPS_LIST(
60                       GST_PROPS_BOOLEAN (FALSE),
61                       GST_PROPS_BOOLEAN (TRUE)
62                     ),
63       "width",      GST_PROPS_LIST(
64                       GST_PROPS_INT (8),
65                       GST_PROPS_INT (16)
66                     ),
67       "depth",      GST_PROPS_LIST(
68                       GST_PROPS_INT (8),
69                       GST_PROPS_INT (16)
70                     ),
71       "rate",       GST_PROPS_INT_RANGE (8000,48000),
72       "channels",   GST_PROPS_INT_RANGE (1, 2)
73   ),
74   GST_CAPS_NEW (
75     "auparse_src_alaw",
76     "audio/x-alaw",
77       "rate",       GST_PROPS_INT_RANGE (8000,48000),
78       "channels",   GST_PROPS_INT_RANGE (1, 2)
79   )
80 )
81
82 /* AuParse signals and args */
83 enum {
84   /* FILL ME */
85   LAST_SIGNAL
86 };
87
88 enum {
89   ARG_0,
90   /* FILL ME */
91 };
92
93 static void     gst_auparse_base_init           (gpointer g_class);
94 static void     gst_auparse_class_init          (GstAuParseClass *klass);
95 static void     gst_auparse_init                (GstAuParse *auparse);
96
97 static void     gst_auparse_chain               (GstPad *pad,GstData *_data);
98
99 static GstElementClass *parent_class = NULL;
100 /*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
101
102 GType
103 gst_auparse_get_type (void) 
104 {
105   static GType auparse_type = 0;
106
107   if (!auparse_type) {
108     static const GTypeInfo auparse_info = {
109       sizeof(GstAuParseClass),
110       gst_auparse_base_init,
111       NULL,
112       (GClassInitFunc) gst_auparse_class_init,
113       NULL,
114       NULL,
115       sizeof(GstAuParse),
116       0,
117       (GInstanceInitFunc) gst_auparse_init,
118     };
119     auparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info, 0);
120   }
121   return auparse_type;
122 }
123
124 static void
125 gst_auparse_base_init (gpointer g_class)
126 {
127   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
128
129   gst_element_class_add_pad_template (element_class,
130       GST_PAD_TEMPLATE_GET (sink_factory_templ));
131   gst_element_class_add_pad_template (element_class,
132       GST_PAD_TEMPLATE_GET (src_factory_templ));
133   gst_element_class_set_details (element_class, &gst_auparse_details);
134
135 }
136
137 static void
138 gst_auparse_class_init (GstAuParseClass *klass) 
139 {
140   GstElementClass *gstelement_class;
141
142   gstelement_class = (GstElementClass*) klass;
143
144   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
145 }
146
147 static void 
148 gst_auparse_init (GstAuParse *auparse) 
149 {
150   auparse->sinkpad = gst_pad_new_from_template (
151                   GST_PAD_TEMPLATE_GET (sink_factory_templ), "sink");
152   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
153   gst_pad_set_chain_function (auparse->sinkpad, gst_auparse_chain);
154
155   auparse->srcpad = gst_pad_new_from_template (
156                   GST_PAD_TEMPLATE_GET (src_factory_templ), "src");
157   gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
158
159   auparse->offset = 0;
160   auparse->size = 0;
161   auparse->encoding = 0;
162   auparse->frequency = 0;
163   auparse->channels = 0;
164 }
165
166 static void 
167 gst_auparse_chain (GstPad *pad, GstData *_data) 
168 {
169   GstBuffer *buf = GST_BUFFER (_data);
170   GstAuParse *auparse;
171   gchar *data;
172   glong size;
173   GstCaps* tempcaps;
174   gint law, depth;
175   gboolean sign;
176
177   g_return_if_fail (pad != NULL);
178   g_return_if_fail (GST_IS_PAD (pad));
179   g_return_if_fail (buf != NULL);
180
181   auparse = GST_AUPARSE (gst_pad_get_parent (pad));
182   
183   GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
184           gst_element_get_name (GST_ELEMENT (auparse)));
185
186   data = GST_BUFFER_DATA (buf);
187   size = GST_BUFFER_SIZE (buf);
188
189   /* if we haven't seen any data yet... */
190   if (auparse->size == 0) {
191     GstBuffer *newbuf;
192     guint32 *head = (guint32 *)data;
193
194     /* normal format is big endian (au is a Sparc format) */
195     if (GUINT32_FROM_BE (*head) == 0x2e736e64) {
196       head++;
197       auparse->le = 0;
198       auparse->offset           = GUINT32_FROM_BE (*head);
199       head++;
200       auparse->size             = GUINT32_FROM_BE (*head);
201       head++;
202       auparse->encoding         = GUINT32_FROM_BE (*head);
203       head++;
204       auparse->frequency        = GUINT32_FROM_BE (*head);
205       head++;
206       auparse->channels         = GUINT32_FROM_BE (*head);
207       head++;
208
209     /* and of course, someone had to invent a little endian
210      * version.  Used by DEC systems. */
211     } else if (GUINT32_FROM_LE (*head) == 0x0064732E) {
212       auparse->le = 1;
213       head++;
214       auparse->le = 0;
215       auparse->offset           = GUINT32_FROM_LE (*head);
216       head++;
217       auparse->size             = GUINT32_FROM_LE (*head);
218       head++;
219       auparse->encoding         = GUINT32_FROM_LE (*head);
220       head++;
221       auparse->frequency        = GUINT32_FROM_LE (*head);
222       head++;
223       auparse->channels         = GUINT32_FROM_LE (*head);
224       head++;
225
226     } else {
227       g_warning ("help, dunno what I'm looking at!\n");
228       gst_buffer_unref(buf);
229       return;
230     }
231
232     g_print ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
233              auparse->offset,auparse->size,auparse->encoding,
234              auparse->frequency,auparse->channels);
235     GST_DEBUG ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld",
236              auparse->offset,auparse->size,auparse->encoding,
237              auparse->frequency,auparse->channels);
238     
239     switch (auparse->encoding) {
240       case 1:
241         law = 1;
242         depth = 8;
243         sign = FALSE;
244         break;
245       case 2:
246         law = 0;
247         depth = 8;
248         sign = FALSE;
249         break;
250       case 3:
251         law = 0;
252         depth = 16;
253         sign = TRUE;
254         break;
255       default:
256         g_warning ("help!, dont know how to deal with this format yet\n");
257         return;
258     }
259
260     if (law) {
261       tempcaps = GST_CAPS_NEW ("auparse_src",
262                                "audio/x-alaw",
263                                  "rate",     GST_PROPS_INT (auparse->frequency),
264                                  "channels", GST_PROPS_INT (auparse->channels));
265     } else {
266       tempcaps = GST_CAPS_NEW ("auparse_src",
267                                "audio/x-raw-int",
268                                  "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
269                                  "rate",       GST_PROPS_INT (auparse->frequency),
270                                  "channels",   GST_PROPS_INT (auparse->channels),
271                                  "depth",      GST_PROPS_INT (depth),
272                                  "width",      GST_PROPS_INT (depth),
273                                  "signed",     GST_PROPS_BOOLEAN (sign));
274     }
275
276     if (gst_pad_try_set_caps (auparse->srcpad, tempcaps) <= 0) {
277       gst_buffer_unref (buf);
278       gst_element_error (GST_ELEMENT (auparse), "could not set audio caps");
279       return;
280     }
281
282     newbuf = gst_buffer_new ();
283     GST_BUFFER_DATA (newbuf) = (gpointer) malloc (size-(auparse->offset));
284     memcpy (GST_BUFFER_DATA (newbuf), data+24, size-(auparse->offset));
285     GST_BUFFER_SIZE (newbuf) = size-(auparse->offset);
286
287     gst_buffer_unref (buf);
288
289     gst_pad_push (auparse->srcpad, GST_DATA (newbuf));
290     return;
291   }
292
293   gst_pad_push (auparse->srcpad, GST_DATA (buf));
294 }
295
296
297 static gboolean
298 plugin_init (GstPlugin *plugin)
299 {
300   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
301         GST_TYPE_AUPARSE)) {
302     return FALSE;
303   }
304
305   return TRUE;
306 }
307
308 GST_PLUGIN_DEFINE (
309   GST_VERSION_MAJOR,
310   GST_VERSION_MINOR,
311   "auparse",
312   "parses au streams",
313   plugin_init,
314   VERSION,
315   "GPL",
316   GST_PACKAGE,
317   GST_ORIGIN
318 )
319