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