gst/auparse/gstauparse.c: eee (32, 64) only unsupported formats are ADPCM/CCITT G.72x
[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 "; "       /* does not support 24bit without patch */
53         GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS "; "
54         "audio/x-alaw, "
55         "rate = (int) [ 8000, 48000 ], "
56         "channels = (int) [ 1, 2 ]; "
57         "audio/x-mulaw, "
58         "rate = (int) [ 8000, 48000 ], " "channels = (int) [ 1, 2 ]")
59     );
60
61 /* AuParse signals and args */
62 enum
63 {
64   /* FILL ME */
65   LAST_SIGNAL
66 };
67
68 enum
69 {
70   ARG_0,
71   /* FILL ME */
72 };
73
74 static void gst_auparse_base_init (gpointer g_class);
75 static void gst_auparse_class_init (GstAuParseClass * klass);
76 static void gst_auparse_init (GstAuParse * auparse);
77
78 static void gst_auparse_chain (GstPad * pad, GstData * _data);
79
80 static GstElementClass *parent_class = NULL;
81
82 /*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
83
84 GType
85 gst_auparse_get_type (void)
86 {
87   static GType auparse_type = 0;
88
89   if (!auparse_type) {
90     static const GTypeInfo auparse_info = {
91       sizeof (GstAuParseClass),
92       gst_auparse_base_init,
93       NULL,
94       (GClassInitFunc) gst_auparse_class_init,
95       NULL,
96       NULL,
97       sizeof (GstAuParse),
98       0,
99       (GInstanceInitFunc) gst_auparse_init,
100     };
101
102     auparse_type =
103         g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info,
104         0);
105   }
106   return auparse_type;
107 }
108
109 static void
110 gst_auparse_base_init (gpointer g_class)
111 {
112   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
113
114   gst_element_class_add_pad_template (element_class,
115       gst_static_pad_template_get (&gst_auparse_sink_template));
116   gst_element_class_add_pad_template (element_class,
117       gst_static_pad_template_get (&gst_auparse_src_template));
118   gst_element_class_set_details (element_class, &gst_auparse_details);
119
120 }
121
122 static void
123 gst_auparse_class_init (GstAuParseClass * klass)
124 {
125   GstElementClass *gstelement_class;
126
127   gstelement_class = (GstElementClass *) klass;
128
129   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
130 }
131
132 static void
133 gst_auparse_init (GstAuParse * auparse)
134 {
135   auparse->sinkpad =
136       gst_pad_new_from_template (gst_static_pad_template_get
137       (&gst_auparse_sink_template), "sink");
138   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
139   gst_pad_set_chain_function (auparse->sinkpad, gst_auparse_chain);
140
141   auparse->srcpad =
142       gst_pad_new_from_template (gst_static_pad_template_get
143       (&gst_auparse_src_template), "src");
144   gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
145   gst_pad_use_explicit_caps (auparse->srcpad);
146
147   auparse->offset = 0;
148   auparse->size = 0;
149   auparse->encoding = 0;
150   auparse->frequency = 0;
151   auparse->channels = 0;
152 }
153
154 static void
155 gst_auparse_chain (GstPad * pad, GstData * _data)
156 {
157   GstBuffer *buf = GST_BUFFER (_data);
158   GstAuParse *auparse;
159   gchar *data;
160   glong size;
161   GstCaps *tempcaps;
162   gint law, depth, ieee;
163
164   g_return_if_fail (pad != NULL);
165   g_return_if_fail (GST_IS_PAD (pad));
166   g_return_if_fail (buf != NULL);
167
168   auparse = GST_AUPARSE (gst_pad_get_parent (pad));
169
170   GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
171       gst_element_get_name (GST_ELEMENT (auparse)));
172
173   data = GST_BUFFER_DATA (buf);
174   size = GST_BUFFER_SIZE (buf);
175
176   /* if we haven't seen any data yet... */
177   if (auparse->size == 0) {
178     GstBuffer *newbuf;
179     guint32 *head = (guint32 *) data;
180
181     /* normal format is big endian (au is a Sparc format) */
182     if (GUINT32_FROM_BE (*head) == 0x2e736e64) {
183       head++;
184       auparse->le = 0;
185       auparse->offset = GUINT32_FROM_BE (*head);
186       head++;
187       auparse->size = GUINT32_FROM_BE (*head);
188       head++;
189       auparse->encoding = GUINT32_FROM_BE (*head);
190       head++;
191       auparse->frequency = GUINT32_FROM_BE (*head);
192       head++;
193       auparse->channels = GUINT32_FROM_BE (*head);
194       head++;
195
196       /* and of course, someone had to invent a little endian
197        * version.  Used by DEC systems. */
198     } else if (GUINT32_FROM_LE (*head) == 0x0064732E) {
199       head++;
200       auparse->le = 1;
201       auparse->offset = GUINT32_FROM_LE (*head);
202       head++;
203       auparse->size = GUINT32_FROM_LE (*head);
204       head++;
205       auparse->encoding = GUINT32_FROM_LE (*head);
206       head++;
207       auparse->frequency = GUINT32_FROM_LE (*head);
208       head++;
209       auparse->channels = GUINT32_FROM_LE (*head);
210       head++;
211
212     } else {
213       g_warning ("help, dunno what I'm looking at!\n");
214       gst_buffer_unref (buf);
215       return;
216     }
217
218     g_print
219         ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
220         auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
221         auparse->channels);
222     GST_DEBUG
223         ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld",
224         auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
225         auparse->channels);
226
227 /*
228 Docs :
229         http://www.opengroup.org/public/pubs/external/auformat.html
230         http://astronomy.swin.edu.au/~pbourke/dataformats/au/
231 Samples :
232         http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
233 */
234
235     switch (auparse->encoding) {
236
237       case 1:                  /* 8-bit ISDN mu-law */
238         law = 1;
239         depth = 8;
240         break;
241
242       case 2:                  /* 8-bit linear PCM */
243         law = 0;
244         depth = 8;
245         break;
246       case 3:                  /* 16-bit linear PCM */
247         law = 0;
248         depth = 16;
249         break;
250       case 4:                  /* 24-bit linear PCM */
251         law = 0;
252         depth = 24;
253         break;
254       case 5:                  /* 32-bit linear PCM */
255         law = 0;
256         depth = 32;
257         break;
258
259       case 27:                 /* 8-bit ISDN a-law */
260         law = 2;
261         depth = 8;
262         break;
263
264       case 6:                  /* 32 bit IEEE floating point */
265         ieee = 1;
266         depth = 32;
267         break;
268       case 7:                  /* 64 bit IEEE floating point */
269         ieee = 1;
270         depth = 64;
271         break;
272
273       case 23:                 /* 4 bit CCITT G721 ADPCM */
274       case 24:                 /* CCITT G722 ADPCM */
275       case 25:                 /* CCITT G723 ADPCM */
276       case 26:                 /* 5 bit CCITT G723 ADPCM */
277
278       default:
279         g_warning ("help!, dont know how to deal with this format yet\n");
280         return;
281     }
282
283     if (law) {
284       tempcaps =
285           gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
286           "rate", G_TYPE_INT, auparse->frequency, "channels", G_TYPE_INT,
287           auparse->channels, NULL);
288     } else if (ieee) {
289       tempcaps = gst_caps_new_simple ("audio/x-raw-float",
290           "width", G_TYPE_INT, depth,
291           "endianness", G_TYPE_INT,
292           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, NULL);
293     } else {
294       tempcaps = gst_caps_new_simple ("audio/x-raw-int",
295           "endianness", G_TYPE_INT,
296           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "rate", G_TYPE_INT,
297           auparse->frequency, "channels", G_TYPE_INT, auparse->channels,
298           "depth", G_TYPE_INT, depth, "width", G_TYPE_INT, depth, "signed",
299           G_TYPE_BOOLEAN, TRUE, NULL);
300     }
301
302     if (!gst_pad_set_explicit_caps (auparse->srcpad, tempcaps)) {
303       gst_buffer_unref (buf);
304       return;
305     }
306
307     newbuf = gst_buffer_new ();
308     GST_BUFFER_DATA (newbuf) = (gpointer) malloc (size - (auparse->offset));
309     memcpy (GST_BUFFER_DATA (newbuf), data + 24, size - (auparse->offset));
310     GST_BUFFER_SIZE (newbuf) = size - (auparse->offset);
311
312     gst_buffer_unref (buf);
313
314     gst_pad_push (auparse->srcpad, GST_DATA (newbuf));
315     return;
316   }
317
318   gst_pad_push (auparse->srcpad, GST_DATA (buf));
319 }
320
321
322 static gboolean
323 plugin_init (GstPlugin * plugin)
324 {
325   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
326           GST_TYPE_AUPARSE)) {
327     return FALSE;
328   }
329
330   return TRUE;
331 }
332
333 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
334     GST_VERSION_MINOR,
335     "auparse",
336     "parses au streams", plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN)