Fix memleak with gst_static_pad_template_get().
[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_static_template (&gst_au_parse_sink_template, "sink");
116   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
117   gst_pad_set_chain_function (auparse->sinkpad, gst_au_parse_chain);
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   GstEvent *event;
157
158   layout[0] = 0;
159
160   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
161
162   data = GST_BUFFER_DATA (buf);
163   size = GST_BUFFER_SIZE (buf);
164
165   GST_DEBUG_OBJECT (auparse, "got buffer of size %ld", size);
166
167   /* if we haven't seen any data yet... */
168   if (auparse->size == 0) {
169     guint32 *head = (guint32 *) data;
170
171     /* normal format is big endian (au is a Sparc format) */
172     if (GST_READ_UINT32_BE (head) == 0x2e736e64) {      /* ".snd" */
173       head++;
174       auparse->le = 0;
175       auparse->offset = GST_READ_UINT32_BE (head);
176       head++;
177       /* Do not trust size, could be set to -1 : unknown */
178       auparse->size = GST_READ_UINT32_BE (head);
179       head++;
180       auparse->encoding = GST_READ_UINT32_BE (head);
181       head++;
182       auparse->frequency = GST_READ_UINT32_BE (head);
183       head++;
184       auparse->channels = GST_READ_UINT32_BE (head);
185       head++;
186
187       /* and of course, someone had to invent a little endian
188        * version.  Used by DEC systems. */
189     } else if (GST_READ_UINT32_LE (head) == 0x0064732E) {       /* other source say it is "dns." */
190       head++;
191       auparse->le = 1;
192       auparse->offset = GST_READ_UINT32_LE (head);
193       head++;
194       /* Do not trust size, could be set to -1 : unknown */
195       auparse->size = GST_READ_UINT32_LE (head);
196       head++;
197       auparse->encoding = GST_READ_UINT32_LE (head);
198       head++;
199       auparse->frequency = GST_READ_UINT32_LE (head);
200       head++;
201       auparse->channels = GST_READ_UINT32_LE (head);
202       head++;
203
204     } else {
205       GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
206       gst_buffer_unref (buf);
207       gst_object_unref (auparse);
208       return GST_FLOW_ERROR;
209     }
210
211     GST_DEBUG
212         ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
213         auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
214         auparse->channels);
215
216 /*
217 Docs :
218         http://www.opengroup.org/public/pubs/external/auformat.html
219         http://astronomy.swin.edu.au/~pbourke/dataformats/au/
220         Solaris headers : /usr/include/audio/au.h
221         libsndfile : src/au.c
222 Samples :
223         http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
224 */
225
226     switch (auparse->encoding) {
227
228       case 1:                  /* 8-bit ISDN mu-law G.711 */
229         law = 1;
230         depth = 8;
231         break;
232       case 27:                 /* 8-bit ISDN  A-law G.711 */
233         law = 2;
234         depth = 8;
235         break;
236
237       case 2:                  /*  8-bit linear PCM */
238         depth = 8;
239         break;
240       case 3:                  /* 16-bit linear PCM */
241         depth = 16;
242         break;
243       case 4:                  /* 24-bit linear PCM */
244         depth = 24;
245         break;
246       case 5:                  /* 32-bit linear PCM */
247         depth = 32;
248         break;
249
250       case 6:                  /* 32-bit IEEE floating point */
251         ieee = 1;
252         depth = 32;
253         break;
254       case 7:                  /* 64-bit IEEE floating point */
255         ieee = 1;
256         depth = 64;
257         break;
258
259       case 23:                 /* 4-bit CCITT G.721   ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
260         strcpy (layout, "g721");
261         break;
262       case 24:                 /* 8-bit CCITT G.722   ADPCM        -> rtp */
263         strcpy (layout, "g722");
264         break;
265       case 25:                 /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
266         strcpy (layout, "g723_3");
267         break;
268       case 26:                 /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
269         strcpy (layout, "g723_5");
270         break;
271
272       case 8:                  /* Fragmented sample data */
273       case 9:                  /* AU_ENCODING_NESTED */
274
275       case 10:                 /* DSP program */
276       case 11:                 /* DSP  8-bit fixed point */
277       case 12:                 /* DSP 16-bit fixed point */
278       case 13:                 /* DSP 24-bit fixed point */
279       case 14:                 /* DSP 32-bit fixed point */
280
281       case 16:                 /* AU_ENCODING_DISPLAY : non-audio display data */
282       case 17:                 /* AU_ENCODING_MULAW_SQUELCH */
283
284       case 18:                 /* 16-bit linear with emphasis */
285       case 19:                 /* 16-bit linear compressed (NeXT) */
286       case 20:                 /* 16-bit linear with emphasis and compression */
287
288       case 21:                 /* Music kit DSP commands */
289       case 22:                 /* Music kit DSP commands samples */
290
291       default:
292         GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
293         gst_buffer_unref (buf);
294         gst_object_unref (auparse);
295         return GST_FLOW_ERROR;
296     }
297
298     if (law) {
299       tempcaps =
300           gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
301           "rate", G_TYPE_INT, auparse->frequency,
302           "channels", G_TYPE_INT, auparse->channels, NULL);
303       auparse->sample_size = auparse->channels;
304     } else if (ieee) {
305       tempcaps = gst_caps_new_simple ("audio/x-raw-float",
306           "rate", G_TYPE_INT, auparse->frequency,
307           "channels", G_TYPE_INT, auparse->channels,
308           "endianness", G_TYPE_INT,
309           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN,
310           "width", G_TYPE_INT, depth, NULL);
311       auparse->sample_size = auparse->channels * depth / 8;
312     } else if (layout[0]) {
313       tempcaps = gst_caps_new_simple ("audio/x-adpcm",
314           "layout", G_TYPE_STRING, layout, NULL);
315       auparse->sample_size = 0;
316     } else {
317       tempcaps = gst_caps_new_simple ("audio/x-raw-int",
318           "rate", G_TYPE_INT, auparse->frequency,
319           "channels", G_TYPE_INT, auparse->channels,
320           "endianness", G_TYPE_INT,
321           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "depth", G_TYPE_INT,
322           depth, "width", G_TYPE_INT, depth, "signed", G_TYPE_BOOLEAN, TRUE,
323           NULL);
324       auparse->sample_size = auparse->channels * depth / 8;
325     }
326
327     gst_pad_use_fixed_caps (auparse->srcpad);
328     gst_pad_set_caps (auparse->srcpad, tempcaps);
329     gst_pad_set_active (auparse->srcpad, TRUE);
330
331     event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_DEFAULT,
332         0, GST_CLOCK_TIME_NONE, 0);
333
334     gst_pad_push_event (auparse->srcpad, event);
335
336     databuf = gst_buffer_create_sub (buf, auparse->offset,
337         size - auparse->offset);
338
339     gst_buffer_unref (buf);
340     buf = NULL;
341   } else {
342     databuf = buf;
343     buf = NULL;
344   }
345
346   if (auparse->sample_size) {
347     int avail;
348
349     gst_adapter_push (auparse->adapter, databuf);
350
351     /* Ensure we push a buffer that's a multiple of the frame size downstream */
352     avail = gst_adapter_available (auparse->adapter);
353     avail -= avail % auparse->sample_size;
354
355     if (avail > 0) {
356       const guint8 *data = gst_adapter_peek (auparse->adapter, avail);
357       GstBuffer *newbuf;
358
359       if ((ret =
360               gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
361                   auparse->buffer_offset, avail, GST_PAD_CAPS (auparse->srcpad),
362                   &newbuf)) == GST_FLOW_OK) {
363
364         memcpy (GST_BUFFER_DATA (newbuf), data, avail);
365         gst_adapter_flush (auparse->adapter, avail);
366
367         auparse->buffer_offset += avail;
368
369         ret = gst_pad_push (auparse->srcpad, newbuf);
370       }
371     } else {
372       ret = GST_FLOW_OK;
373     }
374   } else {
375     /* It's something non-trivial (such as ADPCM), we don't understand it, so
376      * just push downstream and assume this will know what to do with it */
377     gst_buffer_set_caps (databuf, GST_PAD_CAPS (auparse->srcpad));
378     ret = gst_pad_push (auparse->srcpad, databuf);
379   }
380
381   gst_object_unref (auparse);
382
383   return ret;
384 }
385
386 static GstStateChangeReturn
387 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
388 {
389   GstAuParse *auparse = GST_AU_PARSE (element);
390   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
391
392   ret = parent_class->change_state (element, transition);
393   if (ret == GST_STATE_CHANGE_FAILURE)
394     return ret;
395
396   switch (transition) {
397     case GST_STATE_CHANGE_READY_TO_NULL:
398       gst_adapter_clear (auparse->adapter);
399       auparse->buffer_offset = 0;
400       auparse->offset = 0;
401       auparse->size = 0;
402       auparse->encoding = 0;
403       auparse->frequency = 0;
404       auparse->channels = 0;
405     default:
406       break;
407   }
408
409   return ret;
410 }
411
412 static gboolean
413 plugin_init (GstPlugin * plugin)
414 {
415   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
416           GST_TYPE_AU_PARSE)) {
417     return FALSE;
418   }
419
420   return TRUE;
421 }
422
423 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
424     GST_VERSION_MINOR,
425     "auparse",
426     "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
427     GST_PACKAGE_ORIGIN)