summaryrefslogtreecommitdiffstats
path: root/gst/wavparse/gstwavparse.c
blob: 2e57da8320d3347dd94d35786b91d45a64337cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* Gnome-Streamer
 * 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.
 */


#include <string.h>

#include <gstwavparse.h>

static void		gst_parsewav_class_init	(GstParseWavClass *klass);
static void		gst_parsewav_init	(GstParseWav *parsewav);

static GstCaps*		wav_typefind		(GstBuffer *buf, gpointer private);

static void		gst_parsewav_chain	(GstPad *pad, GstBuffer *buf);

/* elementfactory information */
static GstElementDetails gst_parsewav_details = {
  ".wav parser",
  "Parser/Audio",
  "Parse a .wav file into raw audio",
  VERSION,
  "Erik Walthinsen <omega@cse.ogi.edu>",
  "(C) 1999",
};

GST_PADTEMPLATE_FACTORY (sink_template_factory,
  "parsewav_sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "parsewav_wav",   
    "audio/wav",  
    NULL
  )
)

GST_PADTEMPLATE_FACTORY (src_template_factory,
  "parsewav_src",
  GST_PAD_SRC,
  GST_PAD_ALWAYS,
  GST_CAPS_NEW (
    "parsewav_raw",   
    "audio/raw",  
      "format",            GST_PROPS_STRING ("int"),
       "law",              GST_PROPS_INT (0),
       "endianness",       GST_PROPS_INT (G_BYTE_ORDER),
       "signed",           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)
  )
)

/* typefactory for 'wav' */
static GstTypeDefinition 
wavdefinition = 
{
  "parsewav_audio/wav",
  "audio/wav",
  ".wav",
  wav_typefind,
};


/* ParseWav signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

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

static GstElementClass *parent_class = NULL;
//static guint gst_parsewav_signals[LAST_SIGNAL] = { 0 };

GType
gst_parsewav_get_type (void) 
{
  static GType parsewav_type = 0;

  if (!parsewav_type) {
    static const GTypeInfo parsewav_info = {
      sizeof(GstParseWavClass),      NULL,
      NULL,
      (GClassInitFunc) gst_parsewav_class_init,
      NULL,
      NULL,
      sizeof(GstParseWav),
      0,
      (GInstanceInitFunc) gst_parsewav_init,
    };
    parsewav_type = g_type_register_static (GST_TYPE_ELEMENT, "GstParseWav", &parsewav_info, 0);
  }
  return parsewav_type;
}

static void
gst_parsewav_class_init (GstParseWavClass *klass) 
{
  GstElementClass *gstelement_class;

  gstelement_class = (GstElementClass*) klass;

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
}

static void 
gst_parsewav_init (GstParseWav *parsewav) 
{
  parsewav->sinkpad = gst_pad_new_from_template (GST_PADTEMPLATE_GET (sink_template_factory), "sink");
  gst_element_add_pad (GST_ELEMENT (parsewav), parsewav->sinkpad);
  gst_pad_set_chain_function (parsewav->sinkpad, gst_parsewav_chain);

  parsewav->srcpad = gst_pad_new_from_template (GST_PADTEMPLATE_GET (src_template_factory), "src");
  gst_element_add_pad (GST_ELEMENT (parsewav), parsewav->srcpad);

  parsewav->riff = NULL;

  parsewav->state = GST_PARSEWAV_UNKNOWN;
  parsewav->riff = NULL;
  parsewav->riff_nextlikely = 0;
  parsewav->size = 0;
  parsewav->bps = 0;
}

static GstCaps*
wav_typefind (GstBuffer *buf, gpointer private)
{
  gchar *data = GST_BUFFER_DATA (buf);

  if (strncmp (&data[0], "RIFF", 4)) return NULL;
  if (strncmp (&data[8], "WAVE", 4)) return NULL;

  return gst_caps_new ("wav_typefind", "audio/wav", NULL);
}


static void
gst_parsewav_chain (GstPad *pad, GstBuffer *buf)
{
  GstParseWav *parsewav;
  gboolean buffer_riffed = FALSE;	/* so we don't parse twice */
  gchar *data;
  gulong size;

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

  parsewav = GST_PARSEWAV (gst_pad_get_parent (pad));
  GST_DEBUG (0, "gst_parsewav_chain: got buffer in '%s'\n",
          gst_object_get_name (GST_OBJECT (parsewav)));
  data = (guchar *) GST_BUFFER_DATA (buf);
  size = GST_BUFFER_SIZE (buf);

  /* walk through the states in priority order */
  /* we're in the data region */
  if (parsewav->state == GST_PARSEWAV_DATA) {
    /* if we're expected to see a new chunk in this buffer */
    if ((parsewav->riff_nextlikely - GST_BUFFER_OFFSET (buf)) < GST_BUFFER_SIZE (buf)) {
	    
      GST_BUFFER_SIZE (buf) = parsewav->riff_nextlikely - GST_BUFFER_OFFSET (buf);
      
      parsewav->state = GST_PARSEWAV_OTHER;
      /* I suppose we could signal an EOF at this point, but that may be
         premature.  We've stopped data flow, that's the main thing. */
    } 
    gst_pad_push (parsewav->srcpad, buf);
    return;
  }

  if (parsewav->state == GST_PARSEWAV_OTHER) {
    GST_DEBUG (0, "we're in unknown territory here, not passing on\n");
    return;
  }


  /* here we deal with parsing out the primary state */
  /* these are sequenced such that in the normal case each (RIFF/WAVE,
     fmt, data) will fire in sequence, as they should */

  /* we're in null state now, look for the RIFF header, start parsing */
  if (parsewav->state == GST_PARSEWAV_UNKNOWN) {
    gint retval;

    GST_DEBUG (0, "GstParseWav: checking for RIFF format\n");

    /* create a new RIFF parser */
    parsewav->riff = gst_riff_new ();
    
    /* give it the current buffer to start parsing */
    retval = gst_riff_next_buffer (parsewav->riff, buf, 0);
    buffer_riffed = TRUE;
    if (retval < 0) {
      GST_DEBUG (0, "sorry, isn't RIFF\n");
      return;
    }

    /* this has to be a file of form WAVE for us to deal with it */
    if (parsewav->riff->form != gst_riff_fourcc_to_id ("WAVE")) {
      GST_DEBUG (0, "sorry, isn't WAVE\n");
      return;
    }

    /* at this point we're waiting for the 'fmt ' chunk */
    parsewav->state = GST_PARSEWAV_CHUNK_FMT;
  }

  /* we're now looking for the 'fmt ' chunk to get the audio info */
  if (parsewav->state == GST_PARSEWAV_CHUNK_FMT) {
    GstRiffChunk *fmt;
    GstParseWavFormat *format;

    GST_DEBUG (0, "GstParseWav: looking for fmt chunk\n");

    /* there's a good possibility we may not have parsed this buffer */
    if (buffer_riffed == FALSE) {
      gst_riff_next_buffer (parsewav->riff, buf, GST_BUFFER_OFFSET (buf));
      buffer_riffed = TRUE;
    }

    /* see if the fmt chunk is available yet */
    fmt = gst_riff_get_chunk (parsewav->riff, "fmt ");

    /* if we've got something, deal with it */
    if (fmt != NULL) {

      /* we can gather format information now */
      format = (GstParseWavFormat *)((guchar *) GST_BUFFER_DATA (buf) + fmt->offset);

      /* set the caps on the src pad */
      gst_pad_set_caps (parsewav->srcpad, gst_caps_new (
	"parsewav_src",
	"audio/raw",
	gst_props_new (
	"format",	GST_PROPS_STRING ("int"),
	  "law",	GST_PROPS_INT (0),		//FIXME
	  "endianness",	GST_PROPS_INT (G_BYTE_ORDER),
          "signed",     GST_PROPS_BOOLEAN (TRUE), //FIXME
	  "width",	GST_PROPS_INT (format->wBitsPerSample),
	  "depth",	GST_PROPS_INT (format->wBitsPerSample),
	  "rate",	GST_PROPS_INT (format->dwSamplesPerSec),
	  "channels",	GST_PROPS_INT (format->wChannels),
	  NULL
	)
      ));

      parsewav->bps = format->wBlockAlign;
      GST_DEBUG (0, "frequency %d, channels %d\n",
		 format->dwSamplesPerSec, format->wChannels); 

      /* we're now looking for the data chunk */
      parsewav->state = GST_PARSEWAV_CHUNK_DATA;
    } else {
      /* otherwise we just sort of give up for this buffer */
      gst_buffer_unref (buf);
      return;
    }
  }

  /* now we look for the data chunk */
  if (parsewav->state == GST_PARSEWAV_CHUNK_DATA) {
    GstBuffer *newbuf;
    GstRiffChunk *datachunk;

    GST_DEBUG (0, "GstParseWav: looking for data chunk\n");

    /* again, we might need to parse the buffer */
    if (buffer_riffed == FALSE) {
      gst_riff_next_buffer (parsewav->riff, buf, GST_BUFFER_OFFSET (buf));
      buffer_riffed = TRUE;
    }

    datachunk = gst_riff_get_chunk (parsewav->riff, "data");

    if (datachunk != NULL) {
      gulong subsize;

      GST_DEBUG (0, "data begins at %ld\n", datachunk->offset);

      /* at this point we can ACK that we have data */
      parsewav->state = GST_PARSEWAV_DATA;

      /* now we construct a new buffer for the remainder */
      subsize = size - datachunk->offset;
      GST_DEBUG (0, "sending last %ld bytes along as audio\n", subsize);
      
      newbuf = gst_buffer_new ();
      GST_BUFFER_DATA (newbuf) = g_malloc (subsize);
      GST_BUFFER_SIZE (newbuf) = subsize;
      
      memcpy (GST_BUFFER_DATA (newbuf), GST_BUFFER_DATA (buf) + datachunk->offset, subsize);

      gst_buffer_unref (buf);

      gst_pad_push (parsewav->srcpad, newbuf);

      /* now we're ready to go, the next buffer should start data */
      parsewav->state = GST_PARSEWAV_DATA;

      /* however, we may be expecting another chunk at some point */
      parsewav->riff_nextlikely = gst_riff_get_nextlikely (parsewav->riff);
    } else {
      /* otherwise we just sort of give up for this buffer */
      gst_buffer_unref (buf);
      return;
    }
  }
}


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

  /* create an elementfactory for the parsewav element */
  factory = gst_elementfactory_new ("parsewav", GST_TYPE_PARSEWAV,
                                    &gst_parsewav_details);
  g_return_val_if_fail(factory != NULL, FALSE);

  /* register src pads */
  gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (sink_template_factory));
  gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_template_factory));

  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));

  type = gst_typefactory_new (&wavdefinition);
  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));

  return TRUE;
}

GstPluginDesc plugin_desc = {
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
  "parsewav",
  plugin_init
};