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