1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 * Copyright (C) <2002> Iain Holmes <iain@prettypeople.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include <gstwavenc.h>
30 static void gst_wavenc_base_init (gpointer g_class);
31 static void gst_wavenc_class_init (GstWavEncClass *klass);
32 static void gst_wavenc_init (GstWavEnc *wavenc);
33 static void gst_wavenc_chain (GstPad *pad, GstData *_data);
35 #define WAVE_FORMAT_PCM 0x0001
37 #define WRITE_U32(buf, x) *(buf) = (unsigned char) (x&0xff);\
38 *((buf)+1) = (unsigned char)((x>>8)&0xff);\
39 *((buf)+2) = (unsigned char)((x>>16)&0xff);\
40 *((buf)+3) = (unsigned char)((x>>24)&0xff);
42 #define WRITE_U16(buf, x) *(buf) = (unsigned char) (x&0xff);\
43 *((buf)+1) = (unsigned char)((x>>8)&0xff);
46 guint8 id[4]; /* RIFF */
48 guint8 wav_id[4]; /* WAVE */
56 struct common_struct {
59 guint32 dwSamplesPerSec;
60 guint32 dwAvgBytesPerSec;
62 guint16 wBitsPerSample; /* Only for PCM */
66 struct riff_struct riff;
67 struct chunk_struct format;
68 struct common_struct common;
69 struct chunk_struct data;
72 static GstElementDetails gst_wavenc_details = GST_ELEMENT_DETAILS (
74 "Codec/Encoder/Audio",
75 "Encode raw audio into WAV",
76 "Iain Holmes <iain@prettypeople.org>"
79 static GstStaticPadTemplate sink_factory =
80 GST_STATIC_PAD_TEMPLATE (
84 GST_STATIC_CAPS ("audio/x-raw-int, "
85 "rate = (int) [ 1, MAX ], "
86 "channels = (int) [ 1, MAX ], "
87 "endianness = (int) LITTLE_ENDIAN, "
88 "width = (int) { 8, 16 }, "
89 "depth = (int) { 8, 16 }, "
90 "signed = (boolean) true"
94 static GstStaticPadTemplate src_factory =
95 GST_STATIC_PAD_TEMPLATE (
99 GST_STATIC_CAPS ("audio/x-wav")
106 static GstElementClass *parent_class = NULL;
109 gst_wavenc_get_type (void)
111 static GType type = 0;
114 static const GTypeInfo info = {
115 sizeof (GstWavEncClass),
116 gst_wavenc_base_init,
118 (GClassInitFunc) gst_wavenc_class_init,
123 (GInstanceInitFunc) gst_wavenc_init
126 type = g_type_register_static (GST_TYPE_ELEMENT, "GstWavEnc", &info, 0);
132 static GstElementStateReturn
133 gst_wavenc_change_state (GstElement *element)
135 GstWavEnc *wavenc = GST_WAVENC (element);
137 switch (GST_STATE_TRANSITION (element)) {
138 case GST_STATE_PAUSED_TO_READY:
139 wavenc->setup = FALSE;
140 wavenc->flush_header = TRUE;
146 if (parent_class->change_state) {
147 return parent_class->change_state (element);
150 return GST_STATE_SUCCESS;
154 set_property (GObject *object,
161 enc = GST_WAVENC (object);
170 gst_wavenc_base_init (gpointer g_class)
172 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
174 gst_element_class_set_details (element_class, &gst_wavenc_details);
176 gst_element_class_add_pad_template (element_class,
177 gst_static_pad_template_get (&src_factory));
178 gst_element_class_add_pad_template (element_class,
179 gst_static_pad_template_get (&sink_factory));
182 gst_wavenc_class_init (GstWavEncClass *klass)
184 GstElementClass *element_class;
185 GObjectClass *object_class;
187 element_class = (GstElementClass *) klass;
188 object_class = (GObjectClass *) klass;
189 object_class->set_property = set_property;
191 element_class->change_state = gst_wavenc_change_state;
193 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
197 gst_wavenc_setup (GstWavEnc *wavenc)
199 struct wave_header wave;
200 gint size = 0x7fffffff; /* Use a bogus size initially */
202 wave.common.wChannels = wavenc->channels;
203 wave.common.wBitsPerSample = wavenc->bits;
204 wave.common.dwSamplesPerSec = wavenc->rate;
206 memset (wavenc->header, 0, WAV_HEADER_LEN);
208 /* Fill out our wav-header with some information */
209 strncpy (wave.riff.id, "RIFF", 4);
210 wave.riff.len = size - 8;
211 strncpy (wave.riff.wav_id, "WAVE", 4);
213 strncpy (wave.format.id, "fmt ", 4);
214 wave.format.len = 16;
216 wave.common.wFormatTag = WAVE_FORMAT_PCM;
217 wave.common.dwAvgBytesPerSec = wave.common.wChannels * wave.common.dwSamplesPerSec * (wave.common.wBitsPerSample >> 3);
218 wave.common.wBlockAlign = wave.common.wChannels * (wave.common.wBitsPerSample >> 3);
220 strncpy (wave.data.id, "data", 4);
221 wave.data.len = size - 44;
223 strncpy (wavenc->header, wave.riff.id, 4);
224 WRITE_U32 (wavenc->header + 4, wave.riff.len);
225 strncpy (wavenc->header + 8, wave.riff.wav_id, 4);
226 strncpy (wavenc->header + 12, wave.format.id, 4);
227 WRITE_U32 (wavenc->header + 16, wave.format.len);
228 WRITE_U16 (wavenc->header + 20, wave.common.wFormatTag);
229 WRITE_U16 (wavenc->header + 22, wave.common.wChannels);
230 WRITE_U32 (wavenc->header + 24, wave.common.dwSamplesPerSec);
231 WRITE_U32 (wavenc->header + 28, wave.common.dwAvgBytesPerSec);
232 WRITE_U16 (wavenc->header + 32, wave.common.wBlockAlign);
233 WRITE_U16 (wavenc->header + 34, wave.common.wBitsPerSample);
234 strncpy (wavenc->header + 36, wave.data.id, 4);
235 WRITE_U32 (wavenc->header + 40, wave.data.len);
237 wavenc->setup = TRUE;
241 static GstPadLinkReturn
242 gst_wavenc_sinkconnect (GstPad *pad, const GstCaps *caps)
245 GstStructure *structure;
247 wavenc = GST_WAVENC (gst_pad_get_parent (pad));
249 structure = gst_caps_get_structure (caps, 0);
251 gst_structure_get_int (structure, "channels", &wavenc->channels);
252 gst_structure_get_int (structure, "rate", &wavenc->rate);
253 gst_structure_get_int (structure, "depth", &wavenc->bits);
255 gst_wavenc_setup (wavenc);
258 return GST_PAD_LINK_OK;
261 return GST_PAD_LINK_REFUSED;
265 gst_wavenc_stop_file (GstWavEnc *wavenc)
270 event = gst_event_new_seek (GST_FORMAT_BYTES |
271 GST_SEEK_METHOD_SET, 0);
272 gst_pad_send_event (GST_PAD_PEER (wavenc->srcpad), event);
274 outbuf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
275 WRITE_U32 (wavenc->header + 4, wavenc->length);
276 memcpy (GST_BUFFER_DATA (outbuf), wavenc->header, WAV_HEADER_LEN);
278 gst_pad_push (wavenc->srcpad, GST_DATA (outbuf));
282 gst_wavenc_init (GstWavEnc *wavenc)
284 GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavenc);
286 wavenc->sinkpad = gst_pad_new_from_template (
287 gst_element_class_get_pad_template (klass, "sink"), "sink");
288 gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->sinkpad);
289 gst_pad_set_chain_function (wavenc->sinkpad, gst_wavenc_chain);
290 gst_pad_set_link_function (wavenc->sinkpad, gst_wavenc_sinkconnect);
292 wavenc->srcpad = gst_pad_new_from_template (
293 gst_element_class_get_pad_template (klass, "src"), "src");
294 gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->srcpad);
296 wavenc->setup = FALSE;
297 wavenc->flush_header = TRUE;
299 GST_FLAG_SET (wavenc, GST_ELEMENT_EVENT_AWARE);
306 { GST_RIFF_INFO_IARL, "Location" },
307 { GST_RIFF_INFO_IART, "Artist" },
308 { GST_RIFF_INFO_ICMS, "Commissioner" },
309 { GST_RIFF_INFO_ICMT, "Comment" },
310 { GST_RIFF_INFO_ICOP, "Copyright" },
311 { GST_RIFF_INFO_ICRD, "Creation Date" },
312 { GST_RIFF_INFO_IENG, "Engineer" },
313 { GST_RIFF_INFO_IGNR, "Genre" },
314 { GST_RIFF_INFO_IKEY, "Keywords" },
315 { GST_RIFF_INFO_INAM, "Title" }, /* Name */
316 { GST_RIFF_INFO_IPRD, "Product" },
317 { GST_RIFF_INFO_ISBJ, "Subject" },
318 { GST_RIFF_INFO_ISFT, "Software" },
319 { GST_RIFF_INFO_ITCH, "Technician" },
325 get_id_from_name (const char *name)
329 for (i = 0; maps[i].name; i++) {
330 if (strcasecmp (maps[i].name, name) == 0) {
339 write_metadata (GstWavEnc *wavenc)
344 gboolean need_to_write = FALSE;
346 info_str = g_string_new ("LIST INFO");
348 for (props = wavenc->metadata->properties->properties; props; props = props->next) {
349 GstPropsEntry *entry = props->data;
353 name = gst_props_entry_get_name (entry);
354 id = get_id_from_name (name);
360 need_to_write = TRUE; /* We've got at least one entry */
362 gst_props_entry_get_string (entry, &text);
363 len = strlen (text) + 1; /* The length in the file includes the \0 */
365 tmp = g_strdup_printf (GST_FOURCC_FORMAT "%d%s", GST_FOURCC_ARGS (id),
366 GUINT32_TO_LE (len), text);
367 g_string_append (info_str, tmp);
370 /* Check that we end on an even boundary */
371 req = ((len + 8) + 1) & ~1;
372 for (i = 0; i < req - len; i++) {
373 g_string_append_printf (info_str, "%c", 0);
382 /* Now we've got all the strings together, we can write our length in */
383 info_str->str[4] = GUINT32_TO_LE (total);
385 buf = gst_buffer_new ();
386 gst_buffer_set_data (buf, info_str->str, info_str->len);
388 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
389 g_string_free (info_str, FALSE);
394 write_cues (GstWavEnc *wavenc)
396 GString *cue_string, *point_string;
399 int num_cues, total = 4;
401 if (gst_props_get (wavenc->metadata->properties,
404 /* No cues, move along please, nothing to see here */
408 /* Space for 'cue ', chunk size and number of cuepoints */
409 cue_string = g_string_new ("cue ");
410 #define CUEPOINT_SIZE 24
411 point_string = g_string_sized_new (CUEPOINT_SIZE);
413 for (c = cue_list, num_cues = 0; c; c = c->next, num_cues++) {
414 GstCaps *cue_caps = c->data;
417 gst_props_get (cue_caps->properties, "position", &pos, NULL);
419 point_string->str[0] = GUINT32_TO_LE (num_cues + 1);
420 point_string->str[4] = GUINT32_TO_LE (0);
421 /* Fixme: There is probably a macro for this */
422 point_string->str[8] = 'd';
423 point_string->str[9] = 'a';
424 point_string->str[10] = 't';
425 point_string->str[11] = 'a';
426 point_string->str[12] = GUINT32_TO_LE (0);
427 point_string->str[16] = GUINT32_TO_LE (0);
428 point_string->str[20] = GUINT32_TO_LE (pos);
430 total += CUEPOINT_SIZE;
433 /* Set the length and chunk size */
434 cue_string->str[4] = GUINT32_TO_LE (total);
435 cue_string->str[8] = GUINT32_TO_LE (num_cues);
436 /* Stick the cue points on the end */
437 g_string_append (cue_string, point_string->str);
438 g_string_free (point_string, TRUE);
440 buf = gst_buffer_new ();
441 gst_buffer_set_data (buf, cue_string->str, cue_string->len);
443 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
444 g_string_free (cue_string, FALSE);
448 write_labels (GstWavEnc *wavenc)
455 info_str = g_string_new ("LIST adtl");
456 if (gst_props_get (wavenc->metadata->properties, "ltxts", &caps, NULL)) {
460 for (p = caps, i = 1; p; p = p->next, i++) {
461 GstCaps *ltxt_caps = p->data;
466 gst_props_get (ltxt_caps->properties, "name", &label, NULL);
467 len = strlen (label);
470 ltxt = g_string_new ("ltxt ");
471 ltxt->str[8] = GUINT32_TO_LE (i); /* Identifier */
472 ltxt->str[12] = GUINT32_TO_LE (0); /* Sample Length */
473 ltxt->str[16] = GUINT32_TO_LE (0); /* FIXME: Don't save the purpose yet */
474 ltxt->str[20] = GUINT16_TO_LE (0); /* Country */
475 ltxt->str[22] = GUINT16_TO_LE (0); /* Language */
476 ltxt->str[24] = GUINT16_TO_LE (0); /* Dialect */
477 ltxt->str[26] = GUINT16_TO_LE (0); /* Code Page */
478 g_string_append (ltxt, label);
483 ltxt->str[4] = GUINT32_TO_LE (len);
485 /* Check that we end on an even boundary */
486 req = ((len + 8) + 1) & ~1;
487 for (j = 0; j < req - len; j++) {
488 g_string_append_printf (ltxt, "%c", 0);
493 g_string_append (info_str, ltxt->str);
494 g_string_free (ltxt, TRUE);
498 if (gst_props_get (wavenc->metadata->properties, "labels", &caps, NULL)) {
502 for (p = caps, i = 1; p; p = p->next, i++) {
503 GstCaps *labl_caps = p->data;
508 gst_props_get (labl_caps->properties, "name", &label, NULL);
509 len = strlen (label);
512 labl = g_string_new ("labl ");
513 labl->str[8] = GUINT32_TO_LE (i);
514 g_string_append (labl, label);
519 labl->str[4] = GUINT32_TO_LE (len);
522 req = ((len + 8) + 1) & ~1;
523 for (j = 0; j < req - len; j++) {
524 g_string_append_printf (labl, "%c", 0);
529 g_string_append (info_str, labl->str);
530 g_string_free (labl, TRUE);
534 if (gst_props_get (wavenc->metadata->properties, "notes", &caps, NULL)) {
538 for (p = caps, i = 1; p; p = p->next, i++) {
539 GstCaps *note_caps = p->data;
544 gst_props_get (note_caps->properties, "name", &label, NULL);
545 len = strlen (label);
548 note = g_string_new ("note ");
549 note->str[8] = GUINT32_TO_LE (i);
550 g_string_append (note, label);
555 note->str[4] = GUINT32_TO_LE (len);
558 req = ((len + 8) + 1) & ~1;
559 for (j = 0; j < req - len; j++) {
560 g_string_append_printf (note, "%c", 0);
565 g_string_append (info_str, note->str);
566 g_string_free (note, TRUE);
570 info_str->str[4] = GUINT32_TO_LE (total);
572 buf = gst_buffer_new ();
573 gst_buffer_set_data (buf, info_str->str, info_str->len);
575 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
576 g_string_free (info_str, FALSE);
581 gst_wavenc_chain (GstPad *pad,
584 GstBuffer *buf = GST_BUFFER (_data);
587 wavenc = GST_WAVENC (gst_pad_get_parent (pad));
589 if (!wavenc->setup) {
590 gst_buffer_unref (buf);
591 gst_element_error (GST_ELEMENT (wavenc), "encoder not initialised (input is not audio?)");
595 if (GST_IS_EVENT (buf)) {
596 if (GST_EVENT_TYPE (buf) == GST_EVENT_EOS) {
597 wavenc->pad_eos = TRUE;
600 /* Write our metadata if we have any */
601 if (wavenc->metadata) {
602 write_metadata (wavenc);
604 write_labels (wavenc);
608 gst_wavenc_stop_file (wavenc);
609 gst_pad_push (wavenc->srcpad,
610 GST_DATA (gst_event_new (GST_EVENT_EOS)));
611 gst_element_set_eos (GST_ELEMENT (wavenc));
613 gst_pad_event_default (wavenc->srcpad, GST_EVENT (buf));
618 if (GST_PAD_IS_USABLE (wavenc->srcpad)) {
619 if (wavenc->flush_header) {
622 outbuf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
623 memcpy (GST_BUFFER_DATA (outbuf), wavenc->header, WAV_HEADER_LEN);
624 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
626 gst_pad_push (wavenc->srcpad, GST_DATA (outbuf));
627 wavenc->flush_header = FALSE;
630 wavenc->length += GST_BUFFER_SIZE (buf);
631 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
636 plugin_init (GstPlugin *plugin)
638 return gst_element_register (plugin, "wavenc", GST_RANK_NONE, GST_TYPE_WAVENC);
645 "Encode raw audio into WAV",