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