All plugins updated for element state changes.
[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 Abbas Merali <zaheerabbas at merali dot org>
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/Demuxer/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_SOMETIMES,          /* FIXME: spider */
52     GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
53         /* we don't use GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS
54            because of min buffer-frames which is 1, not 0 */
55         "audio/x-raw-float, "
56         "rate = (int) [ 1, MAX ], "
57         "channels = (int) [ 1, MAX ], "
58         "endianness = (int) { LITTLE_ENDIAN , BIG_ENDIAN }, "
59         "width = (int) { 32, 64 }, "
60         "buffer-frames = (int) [ 0, MAX]" "; "
61         "audio/x-alaw, "
62         "rate = (int) [ 8000, 192000 ], "
63         "channels = (int) [ 1, 2 ]" "; "
64         "audio/x-mulaw, "
65         "rate = (int) [ 8000, 192000 ], " "channels = (int) [ 1, 2 ]" "; "
66         /* Nothing to decode those ADPCM streams for now */
67         "audio/x-adpcm, " "layout = (string) { g721, g722, g723_3, g723_5 }")
68     );
69
70 /* AuParse signals and args */
71 enum
72 {
73   /* FILL ME */
74   LAST_SIGNAL
75 };
76
77 enum
78 {
79   ARG_0
80       /* FILL ME */
81 };
82
83 static void gst_auparse_base_init (gpointer g_class);
84 static void gst_auparse_class_init (GstAuParseClass * klass);
85 static void gst_auparse_init (GstAuParse * auparse);
86
87 static void gst_auparse_chain (GstPad * pad, GstData * _data);
88
89 static GstStateChangeReturn gst_auparse_change_state (GstElement * element,
90     GstStateChange transition);
91
92 static GstElementClass *parent_class = NULL;
93
94 /*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
95
96 GType
97 gst_auparse_get_type (void)
98 {
99   static GType auparse_type = 0;
100
101   if (!auparse_type) {
102     static const GTypeInfo auparse_info = {
103       sizeof (GstAuParseClass),
104       gst_auparse_base_init,
105       NULL,
106       (GClassInitFunc) gst_auparse_class_init,
107       NULL,
108       NULL,
109       sizeof (GstAuParse),
110       0,
111       (GInstanceInitFunc) gst_auparse_init,
112     };
113
114     auparse_type =
115         g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info,
116         0);
117   }
118   return auparse_type;
119 }
120
121 static void
122 gst_auparse_base_init (gpointer g_class)
123 {
124   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
125
126   gst_element_class_add_pad_template (element_class,
127       gst_static_pad_template_get (&gst_auparse_sink_template));
128   gst_element_class_add_pad_template (element_class,
129       gst_static_pad_template_get (&gst_auparse_src_template));
130   gst_element_class_set_details (element_class, &gst_auparse_details);
131
132 }
133
134 static void
135 gst_auparse_class_init (GstAuParseClass * klass)
136 {
137   GstElementClass *gstelement_class;
138
139   gstelement_class = (GstElementClass *) klass;
140
141   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
142
143   gstelement_class->change_state = gst_auparse_change_state;
144 }
145
146 static void
147 gst_auparse_init (GstAuParse * auparse)
148 {
149   auparse->sinkpad =
150       gst_pad_new_from_template (gst_static_pad_template_get
151       (&gst_auparse_sink_template), "sink");
152   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
153   gst_pad_set_chain_function (auparse->sinkpad, gst_auparse_chain);
154
155   auparse->srcpad = NULL;
156 #if 0                           /* FIXME: spider */
157   gst_pad_new_from_template (gst_static_pad_template_get
158       (&gst_auparse_src_template), "src");
159   gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
160   gst_pad_use_explicit_caps (auparse->srcpad);
161 #endif
162
163   auparse->offset = 0;
164   auparse->size = 0;
165   auparse->encoding = 0;
166   auparse->frequency = 0;
167   auparse->channels = 0;
168 }
169
170 static void
171 gst_auparse_chain (GstPad * pad, GstData * _data)
172 {
173   GstBuffer *buf = GST_BUFFER (_data);
174   GstAuParse *auparse;
175   gchar *data;
176   glong size;
177   GstCaps *tempcaps;
178   gint law = 0, depth = 0, ieee = 0;
179   gchar layout[7];
180
181   layout[0] = 0;
182
183   g_return_if_fail (pad != NULL);
184   g_return_if_fail (GST_IS_PAD (pad));
185   g_return_if_fail (buf != NULL);
186
187   auparse = GST_AUPARSE (gst_pad_get_parent (pad));
188
189   GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
190       gst_element_get_name (GST_ELEMENT (auparse)));
191
192   data = GST_BUFFER_DATA (buf);
193   size = GST_BUFFER_SIZE (buf);
194
195   /* if we haven't seen any data yet... */
196   if (auparse->size == 0) {
197     GstBuffer *newbuf;
198     guint32 *head = (guint32 *) data;
199
200     /* normal format is big endian (au is a Sparc format) */
201     if (GST_READ_UINT32_BE (head) == 0x2e736e64) {      /* ".snd" */
202       head++;
203       auparse->le = 0;
204       auparse->offset = GST_READ_UINT32_BE (head);
205       head++;
206       /* Do not trust size, could be set to -1 : unknown */
207       auparse->size = GST_READ_UINT32_BE (head);
208       head++;
209       auparse->encoding = GST_READ_UINT32_BE (head);
210       head++;
211       auparse->frequency = GST_READ_UINT32_BE (head);
212       head++;
213       auparse->channels = GST_READ_UINT32_BE (head);
214       head++;
215
216       /* and of course, someone had to invent a little endian
217        * version.  Used by DEC systems. */
218     } else if (GST_READ_UINT32_LE (head) == 0x0064732E) {       /* other source say it is "dns." */
219       head++;
220       auparse->le = 1;
221       auparse->offset = GST_READ_UINT32_LE (head);
222       head++;
223       /* Do not trust size, could be set to -1 : unknown */
224       auparse->size = GST_READ_UINT32_LE (head);
225       head++;
226       auparse->encoding = GST_READ_UINT32_LE (head);
227       head++;
228       auparse->frequency = GST_READ_UINT32_LE (head);
229       head++;
230       auparse->channels = GST_READ_UINT32_LE (head);
231       head++;
232
233     } else {
234       GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
235       gst_buffer_unref (buf);
236       return;
237     }
238
239     GST_DEBUG
240         ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld",
241         auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
242         auparse->channels);
243
244 /*
245 Docs :
246         http://www.opengroup.org/public/pubs/external/auformat.html
247         http://astronomy.swin.edu.au/~pbourke/dataformats/au/
248         Solaris headers : /usr/include/audio/au.h
249         libsndfile : src/au.c
250 Samples :
251         http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
252 */
253
254     switch (auparse->encoding) {
255
256       case 1:                  /* 8-bit ISDN mu-law G.711 */
257         law = 1;
258         depth = 8;
259         break;
260       case 27:                 /* 8-bit ISDN  A-law G.711 */
261         law = 2;
262         depth = 8;
263         break;
264
265       case 2:                  /*  8-bit linear PCM */
266         depth = 8;
267         break;
268       case 3:                  /* 16-bit linear PCM */
269         depth = 16;
270         break;
271       case 4:                  /* 24-bit linear PCM */
272         depth = 24;
273         break;
274       case 5:                  /* 32-bit linear PCM */
275         depth = 32;
276         break;
277
278       case 6:                  /* 32-bit IEEE floating point */
279         ieee = 1;
280         depth = 32;
281         break;
282       case 7:                  /* 64-bit IEEE floating point */
283         ieee = 1;
284         depth = 64;
285         break;
286
287       case 23:                 /* 4-bit CCITT G.721   ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
288         strcpy (layout, "g721");
289         break;
290       case 24:                 /* 8-bit CCITT G.722   ADPCM        -> rtp */
291         strcpy (layout, "g722");
292         break;
293       case 25:                 /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
294         strcpy (layout, "g723_3");
295         break;
296       case 26:                 /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
297         strcpy (layout, "g723_5");
298         break;
299
300       case 8:                  /* Fragmented sample data */
301       case 9:                  /* AU_ENCODING_NESTED */
302
303       case 10:                 /* DSP program */
304       case 11:                 /* DSP  8-bit fixed point */
305       case 12:                 /* DSP 16-bit fixed point */
306       case 13:                 /* DSP 24-bit fixed point */
307       case 14:                 /* DSP 32-bit fixed point */
308
309       case 16:                 /* AU_ENCODING_DISPLAY : non-audio display data */
310       case 17:                 /* AU_ENCODING_MULAW_SQUELCH */
311
312       case 18:                 /* 16-bit linear with emphasis */
313       case 19:                 /* 16-bit linear compressed (NeXT) */
314       case 20:                 /* 16-bit linear with emphasis and compression */
315
316       case 21:                 /* Music kit DSP commands */
317       case 22:                 /* Music kit DSP commands samples */
318
319       default:
320         GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
321         gst_buffer_unref (buf);
322         return;
323     }
324
325     auparse->srcpad =
326         gst_pad_new_from_template (gst_static_pad_template_get
327         (&gst_auparse_src_template), "src");
328     gst_pad_use_explicit_caps (auparse->srcpad);
329
330     if (law) {
331       tempcaps =
332           gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
333           "rate", G_TYPE_INT, auparse->frequency,
334           "channels", G_TYPE_INT, auparse->channels, NULL);
335     } else if (ieee) {
336       tempcaps = gst_caps_new_simple ("audio/x-raw-float",
337           "rate", G_TYPE_INT, auparse->frequency,
338           "channels", G_TYPE_INT, auparse->channels,
339           "endianness", G_TYPE_INT,
340           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "width", G_TYPE_INT,
341           depth, "buffer-frames", G_TYPE_INT, 0, NULL);
342     } else if (layout[0]) {
343       tempcaps = gst_caps_new_simple ("audio/x-adpcm",
344           "layout", G_TYPE_STRING, layout, NULL);
345     } else {
346       tempcaps = gst_caps_new_simple ("audio/x-raw-int",
347           "rate", G_TYPE_INT, auparse->frequency,
348           "channels", G_TYPE_INT, auparse->channels,
349           "endianness", G_TYPE_INT,
350           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "depth", G_TYPE_INT,
351           depth, "width", G_TYPE_INT, depth, "signed", G_TYPE_BOOLEAN, TRUE,
352           NULL);
353     }
354
355     if (!gst_pad_set_explicit_caps (auparse->srcpad, tempcaps)) {
356       GST_ELEMENT_ERROR (auparse, CORE, NEGOTIATION, (NULL), (NULL));
357       gst_buffer_unref (buf);
358       gst_object_unref (GST_OBJECT (auparse->srcpad));
359       auparse->srcpad = NULL;
360       return;
361     }
362
363     gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
364
365     newbuf = gst_buffer_new ();
366     GST_BUFFER_DATA (newbuf) = (gpointer) malloc (size - (auparse->offset));
367     memcpy (GST_BUFFER_DATA (newbuf), data + (auparse->offset),
368         size - (auparse->offset));
369     GST_BUFFER_SIZE (newbuf) = size - (auparse->offset);
370
371     gst_buffer_unref (buf);
372
373     gst_pad_push (auparse->srcpad, GST_DATA (newbuf));
374     return;
375   }
376
377   gst_pad_push (auparse->srcpad, GST_DATA (buf));
378 }
379
380 static GstStateChangeReturn
381 gst_auparse_change_state (GstElement * element, GstStateChange transition)
382 {
383   GstAuParse *auparse = GST_AUPARSE (element);
384
385   switch (transition) {
386     case GST_STATE_CHANGE_PAUSED_TO_READY:
387       if (auparse->srcpad) {
388         gst_element_remove_pad (element, auparse->srcpad);
389         auparse->srcpad = NULL;
390       }
391       break;
392     default:
393       break;
394   }
395
396   if (parent_class->change_state)
397     return parent_class->change_state (element, transition);
398
399   return GST_STATE_CHANGE_SUCCESS;
400 }
401
402 static gboolean
403 plugin_init (GstPlugin * plugin)
404 {
405   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
406           GST_TYPE_AUPARSE)) {
407     return FALSE;
408   }
409
410   return TRUE;
411 }
412
413 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
414     GST_VERSION_MINOR,
415     "auparse",
416     "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)