gstauparse.c 8.88 KB
Newer Older
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1
/* GStreamer
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* 2001/04/03 - Updated parseau to use caps nego
 *              Zaheer Merali <zaheer@grid9.net
 */

24
25
26
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
27
28
29
#include <stdlib.h>
#include <string.h>

30
#include <gstauparse.h>
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
31
32
33


/* elementfactory information */
34
static GstElementDetails gst_auparse_details = {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
35
  ".au parser",
36
  "Codec/Parser",
37
  "LGPL",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
38
39
40
41
42
43
44
  "Parse an .au file into raw audio",
  VERSION,
  "Erik Walthinsen <omega@cse.ogi.edu>",
  "(C) 1999",
};

static GstCaps*
45
au_type_find (GstByteStream *bs, gpointer private)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
46
{
47
  GstBuffer *buf = NULL;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
48
49
  GstCaps *new = NULL;

50
51
52
53
54
55
56
57
  if (gst_bytestream_peek (bs, &buf, 4) == 4) {
    guint32 head = * (guint32 *) GST_BUFFER_DATA (buf);
    if (head == 0x2e736e64 || head == 0x646e732e) {
      new = gst_caps_new ("au_type_find",
			  "audio/x-au",
			    NULL);
    }
  }
58

59
60
61
  if (buf != NULL) {
    gst_buffer_unref (buf);
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
62
63
64
65
66
67

  return new;
}

/* typefactory for 'au' */
static GstTypeDefinition audefinition = {
68
  "auparse_audio/au",
69
  "audio/x-au",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
70
  ".au",
71
  au_type_find,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
72
73
};

74
GST_PAD_TEMPLATE_FACTORY (sink_factory_templ,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
75
76
77
78
79
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "auparse_sink",
80
    "audio/x-au",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
81
82
83
84
85
    NULL
  )
)


86
GST_PAD_TEMPLATE_FACTORY (src_factory_templ,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
87
88
89
90
91
  "src",
  GST_PAD_SRC,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "auparse_src",
92
93
    "audio/x-raw-int",
      "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      "signed",     GST_PROPS_LIST(
		      GST_PROPS_BOOLEAN (FALSE),
		      GST_PROPS_BOOLEAN (TRUE)
	  	    ),
      "width",      GST_PROPS_LIST(
		      GST_PROPS_INT (8),
		      GST_PROPS_INT (16)
		    ),
      "depth",      GST_PROPS_LIST(
		      GST_PROPS_INT (8),
		      GST_PROPS_INT (16)
		    ),
      "rate",       GST_PROPS_INT_RANGE (8000,48000),
      "channels",   GST_PROPS_INT_RANGE (1, 2)
108
109
110
111
112
113
  ),
  GST_CAPS_NEW (
    "auparse_src_alaw",
    "audio/x-alaw",
      "rate",       GST_PROPS_INT_RANGE (8000,48000),
      "channels",   GST_PROPS_INT_RANGE (1, 2)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
114
115
116
  )
)

117
/* AuParse signals and args */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
118
119
120
121
122
123
124
125
126
127
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  /* FILL ME */
};

128
129
static void 	gst_auparse_class_init		(GstAuParseClass *klass);
static void 	gst_auparse_init		(GstAuParse *auparse);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
130

131
static void 	gst_auparse_chain		(GstPad *pad,GstData *_data);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
132
133

static GstElementClass *parent_class = NULL;
134
/*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
135
136

GType
137
gst_auparse_get_type (void) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
138
{
139
  static GType auparse_type = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
140

141
142
143
  if (!auparse_type) {
    static const GTypeInfo auparse_info = {
      sizeof(GstAuParseClass),      NULL,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
144
      NULL,
145
      (GClassInitFunc) gst_auparse_class_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
146
147
      NULL,
      NULL,
148
      sizeof(GstAuParse),
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
149
      0,
150
      (GInstanceInitFunc) gst_auparse_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
151
    };
152
    auparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info, 0);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
153
  }
154
  return auparse_type;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
155
156
157
}

static void
158
gst_auparse_class_init (GstAuParseClass *klass) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
159
160
161
162
163
164
165
166
167
{
  GstElementClass *gstelement_class;

  gstelement_class = (GstElementClass*) klass;

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
}

static void 
168
gst_auparse_init (GstAuParse *auparse) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
169
{
170
  auparse->sinkpad = gst_pad_new_from_template (
171
		  GST_PAD_TEMPLATE_GET (sink_factory_templ), "sink");
172
173
  gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
  gst_pad_set_chain_function (auparse->sinkpad, gst_auparse_chain);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
174

175
  auparse->srcpad = gst_pad_new_from_template (
176
		  GST_PAD_TEMPLATE_GET (src_factory_templ), "src");
177
  gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
178

179
180
181
182
183
  auparse->offset = 0;
  auparse->size = 0;
  auparse->encoding = 0;
  auparse->frequency = 0;
  auparse->channels = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
184
185
186
}

static void 
187
gst_auparse_chain (GstPad *pad, GstData *_data) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
188
{
189
  GstBuffer *buf = GST_BUFFER (_data);
190
  GstAuParse *auparse;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
191
192
193
  gchar *data;
  glong size;
  GstCaps* tempcaps;
194
195
  gint law, depth;
  gboolean sign;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
196
197
198
199
200

  g_return_if_fail (pad != NULL);
  g_return_if_fail (GST_IS_PAD (pad));
  g_return_if_fail (buf != NULL);

201
  auparse = GST_AUPARSE (gst_pad_get_parent (pad));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
202
  
203
  GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
204
          gst_element_get_name (GST_ELEMENT (auparse)));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
205
206
207
208
209

  data = GST_BUFFER_DATA (buf);
  size = GST_BUFFER_SIZE (buf);

  /* if we haven't seen any data yet... */
210
  if (auparse->size == 0) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
211
    GstBuffer *newbuf;
212
    guint32 *head = (guint32 *)data;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
213
214

    /* normal format is big endian (au is a Sparc format) */
215
    if (GUINT32_FROM_BE (*head) == 0x2e736e64) {
216
      head++;
217
      auparse->le = 0;
218
      auparse->offset 		= GUINT32_FROM_BE (*head);
219
      head++;
220
      auparse->size 		= GUINT32_FROM_BE (*head);
221
      head++;
222
      auparse->encoding 	= GUINT32_FROM_BE (*head);
223
      head++;
224
      auparse->frequency 	= GUINT32_FROM_BE (*head);
225
      head++;
226
      auparse->channels 	= GUINT32_FROM_BE (*head);
227
      head++;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
228

229
230
231
    /* and of course, someone had to invent a little endian
     * version.  Used by DEC systems. */
    } else if (GUINT32_FROM_LE (*head) == 0x0064732E) {
232
      auparse->le = 1;
233
234
      head++;
      auparse->le = 0;
235
      auparse->offset 		= GUINT32_FROM_LE (*head);
236
      head++;
237
      auparse->size 		= GUINT32_FROM_LE (*head);
238
      head++;
239
      auparse->encoding 	= GUINT32_FROM_LE (*head);
240
      head++;
241
      auparse->frequency 	= GUINT32_FROM_LE (*head);
242
      head++;
243
      auparse->channels 	= GUINT32_FROM_LE (*head);
244
      head++;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
245
246
247
248
249
250
251
252

    } else {
      g_warning ("help, dunno what I'm looking at!\n");
      gst_buffer_unref(buf);
      return;
    }

    g_print ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
253
254
             auparse->offset,auparse->size,auparse->encoding,
             auparse->frequency,auparse->channels);
255
    GST_DEBUG ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld",
256
257
             auparse->offset,auparse->size,auparse->encoding,
             auparse->frequency,auparse->channels);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
258
    
259
    switch (auparse->encoding) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
260
      case 1:
261
262
263
	law = 1;
	depth = 8;
	sign = FALSE;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
264
265
	break;
      case 2:
266
267
	law = 0;
	depth = 8;
268
	sign = FALSE;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
269
270
	break;
      case 3:
271
272
273
	law = 0;
	depth = 16;
	sign = TRUE;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
274
275
276
277
278
279
	break;
      default:
	g_warning ("help!, dont know how to deal with this format yet\n");
	return;
    }

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    if (law) {
      tempcaps = GST_CAPS_NEW ("auparse_src",
			       "audio/x-alaw",
				 "rate",     GST_PROPS_INT (auparse->frequency),
				 "channels", GST_PROPS_INT (auparse->channels));
    } else {
      tempcaps = GST_CAPS_NEW ("auparse_src",
			       "audio/x-raw-int",
      				 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
				 "rate",       GST_PROPS_INT (auparse->frequency),
				 "channels",   GST_PROPS_INT (auparse->channels),
				 "depth",      GST_PROPS_INT (depth),
				 "width",      GST_PROPS_INT (depth),
				 "signed",     GST_PROPS_BOOLEAN (sign));
    }
295

296
    if (gst_pad_try_set_caps (auparse->srcpad, tempcaps) <= 0) {
297
      gst_buffer_unref (buf);
298
      gst_element_error (GST_ELEMENT (auparse), "could not set audio caps");
299
300
      return;
    }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
301
302

    newbuf = gst_buffer_new ();
303
304
305
    GST_BUFFER_DATA (newbuf) = (gpointer) malloc (size-(auparse->offset));
    memcpy (GST_BUFFER_DATA (newbuf), data+24, size-(auparse->offset));
    GST_BUFFER_SIZE (newbuf) = size-(auparse->offset);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
306
307
308

    gst_buffer_unref (buf);

309
    gst_pad_push (auparse->srcpad, GST_DATA (newbuf));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
310
311
312
    return;
  }

313
  gst_pad_push (auparse->srcpad, GST_DATA (buf));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
314
315
316
317
318
319
320
321
322
323
}


static gboolean
plugin_init (GModule *module, GstPlugin *plugin)
{
  GstElementFactory *factory;
  GstTypeFactory *type;

  /* create the plugin structure */
324
  /* create an elementfactory for the auparse element and list it */
325
  factory = gst_element_factory_new ("auparse", GST_TYPE_AUPARSE,
326
                                    &gst_auparse_details);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
327
  g_return_val_if_fail (factory != NULL, FALSE);
328
  gst_element_factory_set_rank (factory, GST_ELEMENT_RANK_SECONDARY);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
329

330
331
  gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (sink_factory_templ));
  gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_factory_templ));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
332

333
  type = gst_type_factory_new (&audefinition);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
334
335
336
337
338
339
340
341
342
343

  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));

  return TRUE;
}

GstPluginDesc plugin_desc = {
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
344
  "auparse",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
345
346
347
  plugin_init
};