gstauparse.c 8.21 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

/* elementfactory information */
33
static GstElementDetails gst_auparse_details = GST_ELEMENT_DETAILS (
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
34
  ".au parser",
35
  "Codec/Parser/Audio",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
36
  "Parse an .au file into raw audio",
37
38
  "Erik Walthinsen <omega@cse.ogi.edu>"
);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
39

40
GST_PAD_TEMPLATE_FACTORY (sink_factory_templ,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
41
42
43
44
45
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "auparse_sink",
46
    "audio/x-au",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
47
48
49
50
    NULL
  )
)

51
GST_PAD_TEMPLATE_FACTORY (src_factory_templ,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
52
53
54
55
56
  "src",
  GST_PAD_SRC,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "auparse_src",
57
58
    "audio/x-raw-int",
      "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
      "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)
73
74
75
76
77
78
  ),
  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
79
80
81
  )
)

82
/* AuParse signals and args */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
83
84
85
86
87
88
89
90
91
92
enum {
  /* FILL ME */
  LAST_SIGNAL
};

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

93
static void 	gst_auparse_base_init		(gpointer g_class);
94
95
static void 	gst_auparse_class_init		(GstAuParseClass *klass);
static void 	gst_auparse_init		(GstAuParse *auparse);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
96

97
static void 	gst_auparse_chain		(GstPad *pad,GstData *_data);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
98
99

static GstElementClass *parent_class = NULL;
100
/*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
101
102

GType
103
gst_auparse_get_type (void) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
104
{
105
  static GType auparse_type = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
106

107
108
  if (!auparse_type) {
    static const GTypeInfo auparse_info = {
109
110
      sizeof(GstAuParseClass),
      gst_auparse_base_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
111
      NULL,
112
      (GClassInitFunc) gst_auparse_class_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
113
114
      NULL,
      NULL,
115
      sizeof(GstAuParse),
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
116
      0,
117
      (GInstanceInitFunc) gst_auparse_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
118
    };
119
    auparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info, 0);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
120
  }
121
  return auparse_type;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
122
123
}

124
125
126
127
128
129
130
131
132
133
134
135
136
static void
gst_auparse_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class,
      GST_PAD_TEMPLATE_GET (sink_factory_templ));
  gst_element_class_add_pad_template (element_class,
      GST_PAD_TEMPLATE_GET (src_factory_templ));
  gst_element_class_set_details (element_class, &gst_auparse_details);

}

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
137
static void
138
gst_auparse_class_init (GstAuParseClass *klass) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
139
140
141
142
143
144
145
146
147
{
  GstElementClass *gstelement_class;

  gstelement_class = (GstElementClass*) klass;

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
}

static void 
148
gst_auparse_init (GstAuParse *auparse) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
149
{
150
  auparse->sinkpad = gst_pad_new_from_template (
151
		  GST_PAD_TEMPLATE_GET (sink_factory_templ), "sink");
152
153
  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
154

155
  auparse->srcpad = gst_pad_new_from_template (
156
		  GST_PAD_TEMPLATE_GET (src_factory_templ), "src");
157
  gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
158

159
160
161
162
163
  auparse->offset = 0;
  auparse->size = 0;
  auparse->encoding = 0;
  auparse->frequency = 0;
  auparse->channels = 0;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
164
165
166
}

static void 
167
gst_auparse_chain (GstPad *pad, GstData *_data) 
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
168
{
169
  GstBuffer *buf = GST_BUFFER (_data);
170
  GstAuParse *auparse;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
171
172
173
  gchar *data;
  glong size;
  GstCaps* tempcaps;
174
175
  gint law, depth;
  gboolean sign;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
176
177
178
179
180

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

181
  auparse = GST_AUPARSE (gst_pad_get_parent (pad));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
182
  
183
  GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
184
          gst_element_get_name (GST_ELEMENT (auparse)));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
185
186
187
188
189

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

  /* if we haven't seen any data yet... */
190
  if (auparse->size == 0) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
191
    GstBuffer *newbuf;
192
    guint32 *head = (guint32 *)data;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
193
194

    /* normal format is big endian (au is a Sparc format) */
195
    if (GUINT32_FROM_BE (*head) == 0x2e736e64) {
196
      head++;
197
      auparse->le = 0;
198
      auparse->offset 		= GUINT32_FROM_BE (*head);
199
      head++;
200
      auparse->size 		= GUINT32_FROM_BE (*head);
201
      head++;
202
      auparse->encoding 	= GUINT32_FROM_BE (*head);
203
      head++;
204
      auparse->frequency 	= GUINT32_FROM_BE (*head);
205
      head++;
206
      auparse->channels 	= GUINT32_FROM_BE (*head);
207
      head++;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
208

209
210
211
    /* and of course, someone had to invent a little endian
     * version.  Used by DEC systems. */
    } else if (GUINT32_FROM_LE (*head) == 0x0064732E) {
212
      auparse->le = 1;
213
214
      head++;
      auparse->le = 0;
215
      auparse->offset 		= GUINT32_FROM_LE (*head);
216
      head++;
217
      auparse->size 		= GUINT32_FROM_LE (*head);
218
      head++;
219
      auparse->encoding 	= GUINT32_FROM_LE (*head);
220
      head++;
221
      auparse->frequency 	= GUINT32_FROM_LE (*head);
222
      head++;
223
      auparse->channels 	= GUINT32_FROM_LE (*head);
224
      head++;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
225
226
227
228
229
230
231
232

    } 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",
233
234
             auparse->offset,auparse->size,auparse->encoding,
             auparse->frequency,auparse->channels);
235
    GST_DEBUG ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld",
236
237
             auparse->offset,auparse->size,auparse->encoding,
             auparse->frequency,auparse->channels);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
238
    
239
    switch (auparse->encoding) {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
240
      case 1:
241
242
243
	law = 1;
	depth = 8;
	sign = FALSE;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
244
245
	break;
      case 2:
246
247
	law = 0;
	depth = 8;
248
	sign = FALSE;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
249
250
	break;
      case 3:
251
252
253
	law = 0;
	depth = 16;
	sign = TRUE;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
254
255
256
257
258
259
	break;
      default:
	g_warning ("help!, dont know how to deal with this format yet\n");
	return;
    }

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    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));
    }
275

276
    if (gst_pad_try_set_caps (auparse->srcpad, tempcaps) <= 0) {
277
      gst_buffer_unref (buf);
278
      gst_element_error (GST_ELEMENT (auparse), "could not set audio caps");
279
280
      return;
    }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
281
282

    newbuf = gst_buffer_new ();
283
284
285
    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
286
287
288

    gst_buffer_unref (buf);

289
    gst_pad_push (auparse->srcpad, GST_DATA (newbuf));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
290
291
292
    return;
  }

293
  gst_pad_push (auparse->srcpad, GST_DATA (buf));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
294
295
296
297
}


static gboolean
298
plugin_init (GstPlugin *plugin)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
299
{
300
301
302
303
  if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
        GST_TYPE_AUPARSE)) {
    return FALSE;
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
304
305
306
307

  return TRUE;
}

308
GST_PLUGIN_DEFINE (
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
309
310
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
311
  "auparse",
312
313
314
315
316
317
318
319
  "parses au streams",
  plugin_init,
  VERSION,
  "GPL",
  GST_COPYRIGHT,
  GST_PACKAGE,
  GST_ORIGIN
)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
320