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