gst/auparse/gstauparse.c: Don't push buffers into the adapter that we are going to...
[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 GstElementDetails gst_au_parse_details =
37 GST_ELEMENT_DETAILS (".au parser",
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_template (gst_static_pad_template_get
116       (&gst_au_parse_sink_template), "sink");
117   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
118   gst_pad_set_chain_function (auparse->sinkpad, gst_au_parse_chain);
119
120   auparse->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
121       (&gst_au_parse_src_template), "src");
122   gst_pad_use_fixed_caps (auparse->srcpad);
123   gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
124
125   auparse->offset = 0;
126   auparse->buffer_offset = 0;
127   auparse->adapter = gst_adapter_new ();
128   auparse->size = 0;
129   auparse->encoding = 0;
130   auparse->frequency = 0;
131   auparse->channels = 0;
132 }
133
134 static void
135 gst_au_parse_dispose (GObject * object)
136 {
137   GstAuParse *au = GST_AU_PARSE (object);
138
139   if (au->adapter != NULL) {
140     g_object_unref (au->adapter);
141     au->adapter = NULL;
142   }
143   G_OBJECT_CLASS (parent_class)->dispose (object);
144 }
145
146 static GstFlowReturn
147 gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
148 {
149   GstFlowReturn ret;
150   GstAuParse *auparse;
151   guchar *data;
152   glong size;
153   GstCaps *tempcaps;
154   gint law = 0, depth = 0, ieee = 0;
155   gchar layout[7];
156   GstBuffer *databuf;
157   GstEvent *event;
158
159   layout[0] = 0;
160
161   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
162
163   data = GST_BUFFER_DATA (buf);
164   size = GST_BUFFER_SIZE (buf);
165
166   GST_DEBUG_OBJECT (auparse, "got buffer of size %ld", size);
167
168   /* if we haven't seen any data yet... */
169   if (auparse->size == 0) {
170     guint32 *head = (guint32 *) data;
171
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       GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
207       gst_buffer_unref (buf);
208       gst_object_unref (auparse);
209       return GST_FLOW_ERROR;
210     }
211
212     GST_DEBUG
213         ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
214         auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
215         auparse->channels);
216
217 /*
218 Docs :
219         http://www.opengroup.org/public/pubs/external/auformat.html
220         http://astronomy.swin.edu.au/~pbourke/dataformats/au/
221         Solaris headers : /usr/include/audio/au.h
222         libsndfile : src/au.c
223 Samples :
224         http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
225 */
226
227     switch (auparse->encoding) {
228
229       case 1:                  /* 8-bit ISDN mu-law G.711 */
230         law = 1;
231         depth = 8;
232         break;
233       case 27:                 /* 8-bit ISDN  A-law G.711 */
234         law = 2;
235         depth = 8;
236         break;
237
238       case 2:                  /*  8-bit linear PCM */
239         depth = 8;
240         break;
241       case 3:                  /* 16-bit linear PCM */
242         depth = 16;
243         break;
244       case 4:                  /* 24-bit linear PCM */
245         depth = 24;
246         break;
247       case 5:                  /* 32-bit linear PCM */
248         depth = 32;
249         break;
250
251       case 6:                  /* 32-bit IEEE floating point */
252         ieee = 1;
253         depth = 32;
254         break;
255       case 7:                  /* 64-bit IEEE floating point */
256         ieee = 1;
257         depth = 64;
258         break;
259
260       case 23:                 /* 4-bit CCITT G.721   ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
261         strcpy (layout, "g721");
262         break;
263       case 24:                 /* 8-bit CCITT G.722   ADPCM        -> rtp */
264         strcpy (layout, "g722");
265         break;
266       case 25:                 /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
267         strcpy (layout, "g723_3");
268         break;
269       case 26:                 /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
270         strcpy (layout, "g723_5");
271         break;
272
273       case 8:                  /* Fragmented sample data */
274       case 9:                  /* AU_ENCODING_NESTED */
275
276       case 10:                 /* DSP program */
277       case 11:                 /* DSP  8-bit fixed point */
278       case 12:                 /* DSP 16-bit fixed point */
279       case 13:                 /* DSP 24-bit fixed point */
280       case 14:                 /* DSP 32-bit fixed point */
281
282       case 16:                 /* AU_ENCODING_DISPLAY : non-audio display data */
283       case 17:                 /* AU_ENCODING_MULAW_SQUELCH */
284
285       case 18:                 /* 16-bit linear with emphasis */
286       case 19:                 /* 16-bit linear compressed (NeXT) */
287       case 20:                 /* 16-bit linear with emphasis and compression */
288
289       case 21:                 /* Music kit DSP commands */
290       case 22:                 /* Music kit DSP commands samples */
291
292       default:
293         GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
294         gst_buffer_unref (buf);
295         gst_object_unref (auparse);
296         return GST_FLOW_ERROR;
297     }
298
299     if (law) {
300       tempcaps =
301           gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
302           "rate", G_TYPE_INT, auparse->frequency,
303           "channels", G_TYPE_INT, auparse->channels, NULL);
304       auparse->sample_size = auparse->channels;
305     } else if (ieee) {
306       tempcaps = gst_caps_new_simple ("audio/x-raw-float",
307           "rate", G_TYPE_INT, auparse->frequency,
308           "channels", G_TYPE_INT, auparse->channels,
309           "endianness", G_TYPE_INT,
310           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN,
311           "width", G_TYPE_INT, depth, NULL);
312       auparse->sample_size = auparse->channels * depth / 8;
313     } else if (layout[0]) {
314       tempcaps = gst_caps_new_simple ("audio/x-adpcm",
315           "layout", G_TYPE_STRING, layout, NULL);
316       auparse->sample_size = 0;
317     } else {
318       tempcaps = gst_caps_new_simple ("audio/x-raw-int",
319           "rate", G_TYPE_INT, auparse->frequency,
320           "channels", G_TYPE_INT, auparse->channels,
321           "endianness", G_TYPE_INT,
322           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "depth", G_TYPE_INT,
323           depth, "width", G_TYPE_INT, depth, "signed", G_TYPE_BOOLEAN, TRUE,
324           NULL);
325       auparse->sample_size = auparse->channels * depth / 8;
326     }
327
328     gst_pad_use_fixed_caps (auparse->srcpad);
329     gst_pad_set_caps (auparse->srcpad, tempcaps);
330     gst_pad_set_active (auparse->srcpad, TRUE);
331
332     event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_DEFAULT,
333         0, GST_CLOCK_TIME_NONE, 0);
334
335     gst_pad_push_event (auparse->srcpad, event);
336
337     databuf = gst_buffer_create_sub (buf, auparse->offset,
338         size - auparse->offset);
339
340     gst_buffer_unref (buf);
341     buf = NULL;
342   } else {
343     databuf = buf;
344     buf = NULL;
345   }
346
347   if (auparse->sample_size) {
348     int avail;
349
350     gst_adapter_push (auparse->adapter, databuf);
351
352     /* Ensure we push a buffer that's a multiple of the frame size downstream */
353     avail = gst_adapter_available (auparse->adapter);
354     avail -= avail % auparse->sample_size;
355
356     if (avail > 0) {
357       const guint8 *data = gst_adapter_peek (auparse->adapter, avail);
358       GstBuffer *newbuf;
359
360       if ((ret =
361               gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
362                   auparse->buffer_offset, avail, GST_PAD_CAPS (auparse->srcpad),
363                   &newbuf)) == GST_FLOW_OK) {
364
365         memcpy (GST_BUFFER_DATA (newbuf), data, avail);
366         gst_adapter_flush (auparse->adapter, avail);
367
368         auparse->buffer_offset += avail;
369
370         ret = gst_pad_push (auparse->srcpad, newbuf);
371       }
372     } else {
373       ret = GST_FLOW_OK;
374     }
375   } else {
376     /* It's something non-trivial (such as ADPCM), we don't understand it, so
377      * just push downstream and assume this will know what to do with it */
378     gst_buffer_set_caps (databuf, GST_PAD_CAPS (auparse->srcpad));
379     ret = gst_pad_push (auparse->srcpad, databuf);
380   }
381
382   gst_object_unref (auparse);
383
384   return ret;
385 }
386
387 static GstStateChangeReturn
388 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
389 {
390   GstAuParse *auparse = GST_AU_PARSE (element);
391   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
392
393   ret = parent_class->change_state (element, transition);
394   if (ret == GST_STATE_CHANGE_FAILURE)
395     return ret;
396
397   switch (transition) {
398     case GST_STATE_CHANGE_READY_TO_NULL:
399       gst_adapter_clear (auparse->adapter);
400       auparse->buffer_offset = 0;
401       auparse->offset = 0;
402       auparse->size = 0;
403       auparse->encoding = 0;
404       auparse->frequency = 0;
405       auparse->channels = 0;
406     default:
407       break;
408   }
409
410   return ret;
411 }
412
413 static gboolean
414 plugin_init (GstPlugin * plugin)
415 {
416   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
417           GST_TYPE_AU_PARSE)) {
418     return FALSE;
419   }
420
421   return TRUE;
422 }
423
424 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
425     GST_VERSION_MINOR,
426     "auparse",
427     "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
428     GST_PACKAGE_ORIGIN)