Define GstElementDetails as const and also static (when defined as global)
[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
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "gstauparse.h"
33 #include <gst/audio/audio.h>
34
35 /* elementfactory information */
36 static const GstElementDetails gst_au_parse_details =
37 GST_ELEMENT_DETAILS ("AU audio demuxer",
38     "Codec/Demuxer/Audio",
39     "Parse an .au file into raw audio",
40     "Erik Walthinsen <omega@cse.ogi.edu>");
41
42 static GstStaticPadTemplate gst_au_parse_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("audio/x-au")
47     );
48
49 #define GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS \
50     "audio/x-alaw, "                        \
51     "rate = (int) [ 8000, 192000 ], "       \
52     "channels = (int) [ 1, 2 ]"
53
54 #define GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS \
55     "audio/x-mulaw, "                        \
56     "rate = (int) [ 8000, 192000 ], "        \
57     "channels = (int) [ 1, 2 ]"
58
59 /* Nothing to decode those ADPCM streams for now */
60 #define GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS \
61     "audio/x-adpcm, "                        \
62     "layout = (string) { g721, g722, g723_3, g723_5 }"
63
64 static GstStaticPadTemplate gst_au_parse_src_template =
65     GST_STATIC_PAD_TEMPLATE ("src",
66     GST_PAD_SRC,
67     GST_PAD_SOMETIMES,
68     GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
69         GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS ";"
70         GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS ";"
71         GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS ";"
72         GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS));
73
74
75 static void gst_au_parse_dispose (GObject * object);
76 static GstFlowReturn gst_au_parse_chain (GstPad * pad, GstBuffer * buf);
77 static GstStateChangeReturn gst_au_parse_change_state (GstElement * element,
78     GstStateChange transition);
79
80 GST_BOILERPLATE (GstAuParse, gst_au_parse, GstElement, GST_TYPE_ELEMENT)
81
82      static void gst_au_parse_base_init (gpointer g_class)
83 {
84   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
85
86   gst_element_class_add_pad_template (element_class,
87       gst_static_pad_template_get (&gst_au_parse_sink_template));
88   gst_element_class_add_pad_template (element_class,
89       gst_static_pad_template_get (&gst_au_parse_src_template));
90   gst_element_class_set_details (element_class, &gst_au_parse_details);
91
92 }
93
94 static void
95 gst_au_parse_class_init (GstAuParseClass * klass)
96 {
97   GObjectClass *gobject_class;
98   GstElementClass *gstelement_class;
99
100   gobject_class = (GObjectClass *) klass;
101   gstelement_class = (GstElementClass *) klass;
102
103   parent_class = g_type_class_peek_parent (klass);
104
105   gobject_class->dispose = gst_au_parse_dispose;
106
107   gstelement_class->change_state =
108       GST_DEBUG_FUNCPTR (gst_au_parse_change_state);
109 }
110
111 static void
112 gst_au_parse_init (GstAuParse * auparse, GstAuParseClass * klass)
113 {
114   auparse->sinkpad =
115       gst_pad_new_from_static_template (&gst_au_parse_sink_template, "sink");
116   gst_pad_set_chain_function (auparse->sinkpad, gst_au_parse_chain);
117   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
118
119   auparse->srcpad =
120       gst_pad_new_from_static_template (&gst_au_parse_src_template, "src");
121   gst_pad_use_fixed_caps (auparse->srcpad);
122   gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
123
124   auparse->offset = 0;
125   auparse->buffer_offset = 0;
126   auparse->adapter = gst_adapter_new ();
127   auparse->size = 0;
128   auparse->encoding = 0;
129   auparse->frequency = 0;
130   auparse->channels = 0;
131 }
132
133 static void
134 gst_au_parse_dispose (GObject * object)
135 {
136   GstAuParse *au = GST_AU_PARSE (object);
137
138   if (au->adapter != NULL) {
139     g_object_unref (au->adapter);
140     au->adapter = NULL;
141   }
142   G_OBJECT_CLASS (parent_class)->dispose (object);
143 }
144
145 static GstFlowReturn
146 gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
147 {
148   GstFlowReturn ret;
149   GstAuParse *auparse;
150   guchar *data;
151   glong size;
152   GstCaps *tempcaps;
153   gint law = 0, depth = 0, ieee = 0;
154   gchar layout[7];
155   GstBuffer *databuf;
156
157   layout[0] = 0;
158
159   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
160
161   data = GST_BUFFER_DATA (buf);
162   size = GST_BUFFER_SIZE (buf);
163
164   GST_DEBUG_OBJECT (auparse, "got buffer of size %ld", size);
165
166   /* if we haven't seen any data yet... */
167   if (auparse->size == 0) {
168     guint32 *head = (guint32 *) data;
169
170     /* FIXME, check if we have enough data (with adapter?) instead of
171      * crashing. */
172     /* normal format is big endian (au is a Sparc format) */
173     if (GST_READ_UINT32_BE (head) == 0x2e736e64) {      /* ".snd" */
174       head++;
175       auparse->le = 0;
176       auparse->offset = GST_READ_UINT32_BE (head);
177       head++;
178       /* Do not trust size, could be set to -1 : unknown */
179       auparse->size = GST_READ_UINT32_BE (head);
180       head++;
181       auparse->encoding = GST_READ_UINT32_BE (head);
182       head++;
183       auparse->frequency = GST_READ_UINT32_BE (head);
184       head++;
185       auparse->channels = GST_READ_UINT32_BE (head);
186       head++;
187
188       /* and of course, someone had to invent a little endian
189        * version.  Used by DEC systems. */
190     } else if (GST_READ_UINT32_LE (head) == 0x0064732E) {       /* other source say it is "dns." */
191       head++;
192       auparse->le = 1;
193       auparse->offset = GST_READ_UINT32_LE (head);
194       head++;
195       /* Do not trust size, could be set to -1 : unknown */
196       auparse->size = GST_READ_UINT32_LE (head);
197       head++;
198       auparse->encoding = GST_READ_UINT32_LE (head);
199       head++;
200       auparse->frequency = GST_READ_UINT32_LE (head);
201       head++;
202       auparse->channels = GST_READ_UINT32_LE (head);
203       head++;
204
205     } else {
206       goto unknown_header;
207     }
208
209     GST_DEBUG
210         ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
211         auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
212         auparse->channels);
213
214 /*
215 Docs :
216         http://www.opengroup.org/public/pubs/external/auformat.html
217         http://astronomy.swin.edu.au/~pbourke/dataformats/au/
218         Solaris headers : /usr/include/audio/au.h
219         libsndfile : src/au.c
220 Samples :
221         http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
222 */
223
224     switch (auparse->encoding) {
225
226       case 1:                  /* 8-bit ISDN mu-law G.711 */
227         law = 1;
228         depth = 8;
229         break;
230       case 27:                 /* 8-bit ISDN  A-law G.711 */
231         law = 2;
232         depth = 8;
233         break;
234
235       case 2:                  /*  8-bit linear PCM */
236         depth = 8;
237         break;
238       case 3:                  /* 16-bit linear PCM */
239         depth = 16;
240         break;
241       case 4:                  /* 24-bit linear PCM */
242         depth = 24;
243         break;
244       case 5:                  /* 32-bit linear PCM */
245         depth = 32;
246         break;
247
248       case 6:                  /* 32-bit IEEE floating point */
249         ieee = 1;
250         depth = 32;
251         break;
252       case 7:                  /* 64-bit IEEE floating point */
253         ieee = 1;
254         depth = 64;
255         break;
256
257       case 23:                 /* 4-bit CCITT G.721   ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
258         strcpy (layout, "g721");
259         break;
260       case 24:                 /* 8-bit CCITT G.722   ADPCM        -> rtp */
261         strcpy (layout, "g722");
262         break;
263       case 25:                 /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
264         strcpy (layout, "g723_3");
265         break;
266       case 26:                 /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
267         strcpy (layout, "g723_5");
268         break;
269
270       case 8:                  /* Fragmented sample data */
271       case 9:                  /* AU_ENCODING_NESTED */
272
273       case 10:                 /* DSP program */
274       case 11:                 /* DSP  8-bit fixed point */
275       case 12:                 /* DSP 16-bit fixed point */
276       case 13:                 /* DSP 24-bit fixed point */
277       case 14:                 /* DSP 32-bit fixed point */
278
279       case 16:                 /* AU_ENCODING_DISPLAY : non-audio display data */
280       case 17:                 /* AU_ENCODING_MULAW_SQUELCH */
281
282       case 18:                 /* 16-bit linear with emphasis */
283       case 19:                 /* 16-bit linear compressed (NeXT) */
284       case 20:                 /* 16-bit linear with emphasis and compression */
285
286       case 21:                 /* Music kit DSP commands */
287       case 22:                 /* Music kit DSP commands samples */
288
289       default:
290         goto unknown_format;
291     }
292
293     if (law) {
294       tempcaps =
295           gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
296           "rate", G_TYPE_INT, auparse->frequency,
297           "channels", G_TYPE_INT, auparse->channels, NULL);
298       auparse->sample_size = auparse->channels;
299     } else if (ieee) {
300       tempcaps = gst_caps_new_simple ("audio/x-raw-float",
301           "rate", G_TYPE_INT, auparse->frequency,
302           "channels", G_TYPE_INT, auparse->channels,
303           "endianness", G_TYPE_INT,
304           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN,
305           "width", G_TYPE_INT, depth, NULL);
306       auparse->sample_size = auparse->channels * depth / 8;
307     } else if (layout[0]) {
308       tempcaps = gst_caps_new_simple ("audio/x-adpcm",
309           "layout", G_TYPE_STRING, layout, NULL);
310       auparse->sample_size = 0;
311     } else {
312       tempcaps = gst_caps_new_simple ("audio/x-raw-int",
313           "rate", G_TYPE_INT, auparse->frequency,
314           "channels", G_TYPE_INT, auparse->channels,
315           "endianness", G_TYPE_INT,
316           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "depth", G_TYPE_INT,
317           depth, "width", G_TYPE_INT, depth, "signed", G_TYPE_BOOLEAN, TRUE,
318           NULL);
319       auparse->sample_size = auparse->channels * depth / 8;
320     }
321
322     gst_pad_set_caps (auparse->srcpad, tempcaps);
323
324 #if 0
325     {
326       GstEvent *event;
327
328       event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_DEFAULT,
329           0, GST_CLOCK_TIME_NONE, 0);
330
331       gst_pad_push_event (auparse->srcpad, event);
332     }
333 #endif
334
335     databuf = gst_buffer_create_sub (buf, auparse->offset,
336         size - auparse->offset);
337
338     gst_buffer_unref (buf);
339     buf = NULL;
340   } else {
341     databuf = buf;
342     buf = NULL;
343   }
344
345   if (auparse->sample_size) {
346     int avail;
347
348     gst_adapter_push (auparse->adapter, databuf);
349
350     /* Ensure we push a buffer that's a multiple of the frame size downstream */
351     avail = gst_adapter_available (auparse->adapter);
352     avail -= avail % auparse->sample_size;
353
354     if (avail > 0) {
355       const guint8 *data = gst_adapter_peek (auparse->adapter, avail);
356       GstBuffer *newbuf;
357
358       if ((ret =
359               gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
360                   auparse->buffer_offset, avail, GST_PAD_CAPS (auparse->srcpad),
361                   &newbuf)) == GST_FLOW_OK) {
362
363         memcpy (GST_BUFFER_DATA (newbuf), data, avail);
364         gst_adapter_flush (auparse->adapter, avail);
365
366         auparse->buffer_offset += avail;
367
368         ret = gst_pad_push (auparse->srcpad, newbuf);
369       }
370     } else {
371       ret = GST_FLOW_OK;
372     }
373   } else {
374     /* It's something non-trivial (such as ADPCM), we don't understand it, so
375      * just push downstream and assume this will know what to do with it */
376     gst_buffer_set_caps (databuf, GST_PAD_CAPS (auparse->srcpad));
377     ret = gst_pad_push (auparse->srcpad, databuf);
378   }
379
380   gst_object_unref (auparse);
381
382   return ret;
383
384   /* ERRORS */
385 unknown_header:
386   {
387     GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
388     gst_buffer_unref (buf);
389     gst_object_unref (auparse);
390     return GST_FLOW_ERROR;
391   }
392 unknown_format:
393   {
394     GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
395     gst_buffer_unref (buf);
396     gst_object_unref (auparse);
397     return GST_FLOW_ERROR;
398   }
399 }
400
401 static GstStateChangeReturn
402 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
403 {
404   GstAuParse *auparse = GST_AU_PARSE (element);
405   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
406
407   ret = parent_class->change_state (element, transition);
408   if (ret == GST_STATE_CHANGE_FAILURE)
409     return ret;
410
411   switch (transition) {
412     case GST_STATE_CHANGE_READY_TO_NULL:
413       gst_adapter_clear (auparse->adapter);
414       auparse->buffer_offset = 0;
415       auparse->offset = 0;
416       auparse->size = 0;
417       auparse->encoding = 0;
418       auparse->frequency = 0;
419       auparse->channels = 0;
420     default:
421       break;
422   }
423
424   return ret;
425 }
426
427 static gboolean
428 plugin_init (GstPlugin * plugin)
429 {
430   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
431           GST_TYPE_AU_PARSE)) {
432     return FALSE;
433   }
434
435   return TRUE;
436 }
437
438 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
439     GST_VERSION_MINOR,
440     "auparse",
441     "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
442     GST_PACKAGE_ORIGIN)