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