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);
47 guint8 id[4]; /* RIFF */
49 guint8 wav_id[4]; /* WAVE */
62 guint32 dwSamplesPerSec;
63 guint32 dwAvgBytesPerSec;
65 guint16 wBitsPerSample; /* Only for PCM */
70 struct riff_struct riff;
71 struct chunk_struct format;
72 struct common_struct common;
73 struct chunk_struct data;
76 static GstElementDetails gst_wavenc_details =
77 GST_ELEMENT_DETAILS ("WAV encoder",
78 "Codec/Encoder/Audio",
79 "Encode raw audio into WAV",
80 "Iain Holmes <iain@prettypeople.org>");
82 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
85 GST_STATIC_CAPS ("audio/x-raw-int, "
86 "rate = (int) [ 1, MAX ], "
87 "channels = (int) [ 1, MAX ], "
88 "endianness = (int) LITTLE_ENDIAN, "
89 "width = (int) { 8, 16 }, "
90 "depth = (int) { 8, 16 }, " "signed = (boolean) true")
93 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
96 GST_STATIC_CAPS ("audio/x-wav")
104 static GstElementClass *parent_class = NULL;
107 gst_wavenc_get_type (void)
109 static GType type = 0;
112 static const GTypeInfo info = {
113 sizeof (GstWavEncClass),
114 gst_wavenc_base_init,
116 (GClassInitFunc) gst_wavenc_class_init,
121 (GInstanceInitFunc) gst_wavenc_init
124 type = g_type_register_static (GST_TYPE_ELEMENT, "GstWavEnc", &info, 0);
130 static GstElementStateReturn
131 gst_wavenc_change_state (GstElement * element)
133 GstWavEnc *wavenc = GST_WAVENC (element);
135 switch (GST_STATE_TRANSITION (element)) {
136 case GST_STATE_PAUSED_TO_READY:
137 wavenc->setup = FALSE;
138 wavenc->flush_header = TRUE;
144 if (parent_class->change_state) {
145 return parent_class->change_state (element);
148 return GST_STATE_SUCCESS;
152 set_property (GObject * object,
153 guint prop_id, const GValue * value, GParamSpec * pspec)
157 enc = GST_WAVENC (object);
166 gst_wavenc_base_init (gpointer g_class)
168 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
170 gst_element_class_set_details (element_class, &gst_wavenc_details);
172 gst_element_class_add_pad_template (element_class,
173 gst_static_pad_template_get (&src_factory));
174 gst_element_class_add_pad_template (element_class,
175 gst_static_pad_template_get (&sink_factory));
178 gst_wavenc_class_init (GstWavEncClass * klass)
180 GstElementClass *element_class;
181 GObjectClass *object_class;
183 element_class = (GstElementClass *) klass;
184 object_class = (GObjectClass *) klass;
185 object_class->set_property = set_property;
187 element_class->change_state = gst_wavenc_change_state;
189 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
193 gst_wavenc_setup (GstWavEnc * wavenc)
195 struct wave_header wave;
196 gint size = 0x7fffffff; /* Use a bogus size initially */
198 wave.common.wChannels = wavenc->channels;
199 wave.common.wBitsPerSample = wavenc->bits;
200 wave.common.dwSamplesPerSec = wavenc->rate;
202 memset (wavenc->header, 0, WAV_HEADER_LEN);
204 /* Fill out our wav-header with some information */
205 strncpy (wave.riff.id, "RIFF", 4);
206 wave.riff.len = size - 8;
207 strncpy (wave.riff.wav_id, "WAVE", 4);
209 strncpy (wave.format.id, "fmt ", 4);
210 wave.format.len = 16;
212 wave.common.wFormatTag = WAVE_FORMAT_PCM;
213 wave.common.dwAvgBytesPerSec =
214 wave.common.wChannels * wave.common.dwSamplesPerSec *
215 (wave.common.wBitsPerSample >> 3);
216 wave.common.wBlockAlign =
217 wave.common.wChannels * (wave.common.wBitsPerSample >> 3);
219 strncpy (wave.data.id, "data", 4);
220 wave.data.len = size - 44;
222 strncpy (wavenc->header, wave.riff.id, 4);
223 WRITE_U32 (wavenc->header + 4, wave.riff.len);
224 strncpy (wavenc->header + 8, wave.riff.wav_id, 4);
225 strncpy (wavenc->header + 12, wave.format.id, 4);
226 WRITE_U32 (wavenc->header + 16, wave.format.len);
227 WRITE_U16 (wavenc->header + 20, wave.common.wFormatTag);
228 WRITE_U16 (wavenc->header + 22, wave.common.wChannels);
229 WRITE_U32 (wavenc->header + 24, wave.common.dwSamplesPerSec);
230 WRITE_U32 (wavenc->header + 28, wave.common.dwAvgBytesPerSec);
231 WRITE_U16 (wavenc->header + 32, wave.common.wBlockAlign);
232 WRITE_U16 (wavenc->header + 34, wave.common.wBitsPerSample);
233 strncpy (wavenc->header + 36, wave.data.id, 4);
234 WRITE_U32 (wavenc->header + 40, wave.data.len);
236 wavenc->setup = TRUE;
240 static GstPadLinkReturn
241 gst_wavenc_sinkconnect (GstPad * pad, const GstCaps * caps)
244 GstStructure *structure;
246 wavenc = GST_WAVENC (gst_pad_get_parent (pad));
248 structure = gst_caps_get_structure (caps, 0);
250 gst_structure_get_int (structure, "channels", &wavenc->channels);
251 gst_structure_get_int (structure, "rate", &wavenc->rate);
252 gst_structure_get_int (structure, "depth", &wavenc->bits);
254 gst_wavenc_setup (wavenc);
257 return GST_PAD_LINK_OK;
260 return GST_PAD_LINK_REFUSED;
264 gst_wavenc_stop_file (GstWavEnc * wavenc)
269 event = gst_event_new_seek (GST_FORMAT_BYTES | GST_SEEK_METHOD_SET, 0);
270 gst_pad_send_event (GST_PAD_PEER (wavenc->srcpad), event);
272 outbuf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
273 WRITE_U32 (wavenc->header + 4, wavenc->length);
274 memcpy (GST_BUFFER_DATA (outbuf), wavenc->header, WAV_HEADER_LEN);
276 gst_pad_push (wavenc->srcpad, GST_DATA (outbuf));
280 gst_wavenc_init (GstWavEnc * wavenc)
282 GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavenc);
285 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
287 gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->sinkpad);
288 gst_pad_set_chain_function (wavenc->sinkpad, gst_wavenc_chain);
289 gst_pad_set_link_function (wavenc->sinkpad, gst_wavenc_sinkconnect);
292 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
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);
308 GST_RIFF_INFO_IARL, "Location"}, {
309 GST_RIFF_INFO_IART, "Artist"}, {
310 GST_RIFF_INFO_ICMS, "Commissioner"}, {
311 GST_RIFF_INFO_ICMT, "Comment"}, {
312 GST_RIFF_INFO_ICOP, "Copyright"}, {
313 GST_RIFF_INFO_ICRD, "Creation Date"}, {
314 GST_RIFF_INFO_IENG, "Engineer"}, {
315 GST_RIFF_INFO_IGNR, "Genre"}, {
316 GST_RIFF_INFO_IKEY, "Keywords"}, {
317 GST_RIFF_INFO_INAM, "Title"}, /* Name */
319 GST_RIFF_INFO_IPRD, "Product"}, {
320 GST_RIFF_INFO_ISBJ, "Subject"}, {
321 GST_RIFF_INFO_ISFT, "Software"}, {
322 GST_RIFF_INFO_ITCH, "Technician"}, {
328 get_id_from_name (const char *name)
332 for (i = 0; maps[i].name; i++) {
333 if (strcasecmp (maps[i].name, name) == 0) {
342 write_metadata (GstWavEnc * wavenc)
347 gboolean need_to_write = FALSE;
349 info_str = g_string_new ("LIST INFO");
351 for (props = wavenc->metadata->properties->properties; props;
352 props = props->next) {
353 GstPropsEntry *entry = props->data;
357 name = gst_props_entry_get_name (entry);
358 id = get_id_from_name (name);
364 need_to_write = TRUE; /* We've got at least one entry */
366 gst_props_entry_get_string (entry, &text);
367 len = strlen (text) + 1; /* The length in the file includes the \0 */
369 tmp = g_strdup_printf (GST_FOURCC_FORMAT "%d%s", GST_FOURCC_ARGS (id),
370 GUINT32_TO_LE (len), text);
371 g_string_append (info_str, tmp);
374 /* Check that we end on an even boundary */
375 req = ((len + 8) + 1) & ~1;
376 for (i = 0; i < req - len; i++) {
377 g_string_append_printf (info_str, "%c", 0);
387 /* Now we've got all the strings together, we can write our length in */
388 info_str->str[4] = GUINT32_TO_LE (total);
390 buf = gst_buffer_new ();
391 gst_buffer_set_data (buf, info_str->str, info_str->len);
393 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
394 g_string_free (info_str, FALSE);
399 write_cues (GstWavEnc * wavenc)
401 GString *cue_string, *point_string;
404 int num_cues, total = 4;
406 if (gst_props_get (wavenc->metadata->properties,
407 "cues", &cue_list, NULL) == FALSE) {
408 /* No cues, move along please, nothing to see here */
412 /* Space for 'cue ', chunk size and number of cuepoints */
413 cue_string = g_string_new ("cue ");
414 #define CUEPOINT_SIZE 24
415 point_string = g_string_sized_new (CUEPOINT_SIZE);
417 for (c = cue_list, num_cues = 0; c; c = c->next, num_cues++) {
418 GstCaps *cue_caps = c->data;
421 gst_props_get (cue_caps->properties, "position", &pos, NULL);
423 point_string->str[0] = GUINT32_TO_LE (num_cues + 1);
424 point_string->str[4] = GUINT32_TO_LE (0);
425 /* Fixme: There is probably a macro for this */
426 point_string->str[8] = 'd';
427 point_string->str[9] = 'a';
428 point_string->str[10] = 't';
429 point_string->str[11] = 'a';
430 point_string->str[12] = GUINT32_TO_LE (0);
431 point_string->str[16] = GUINT32_TO_LE (0);
432 point_string->str[20] = GUINT32_TO_LE (pos);
434 total += CUEPOINT_SIZE;
437 /* Set the length and chunk size */
438 cue_string->str[4] = GUINT32_TO_LE (total);
439 cue_string->str[8] = GUINT32_TO_LE (num_cues);
440 /* Stick the cue points on the end */
441 g_string_append (cue_string, point_string->str);
442 g_string_free (point_string, TRUE);
444 buf = gst_buffer_new ();
445 gst_buffer_set_data (buf, cue_string->str, cue_string->len);
447 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
448 g_string_free (cue_string, FALSE);
452 write_labels (GstWavEnc * wavenc)
459 info_str = g_string_new ("LIST adtl");
460 if (gst_props_get (wavenc->metadata->properties, "ltxts", &caps, NULL)) {
464 for (p = caps, i = 1; p; p = p->next, i++) {
465 GstCaps *ltxt_caps = p->data;
470 gst_props_get (ltxt_caps->properties, "name", &label, NULL);
471 len = strlen (label);
474 ltxt = g_string_new ("ltxt ");
475 ltxt->str[8] = GUINT32_TO_LE (i); /* Identifier */
476 ltxt->str[12] = GUINT32_TO_LE (0); /* Sample Length */
477 ltxt->str[16] = GUINT32_TO_LE (0); /* FIXME: Don't save the purpose yet */
478 ltxt->str[20] = GUINT16_TO_LE (0); /* Country */
479 ltxt->str[22] = GUINT16_TO_LE (0); /* Language */
480 ltxt->str[24] = GUINT16_TO_LE (0); /* Dialect */
481 ltxt->str[26] = GUINT16_TO_LE (0); /* Code Page */
482 g_string_append (ltxt, label);
487 ltxt->str[4] = GUINT32_TO_LE (len);
489 /* Check that we end on an even boundary */
490 req = ((len + 8) + 1) & ~1;
491 for (j = 0; j < req - len; j++) {
492 g_string_append_printf (ltxt, "%c", 0);
497 g_string_append (info_str, ltxt->str);
498 g_string_free (ltxt, TRUE);
502 if (gst_props_get (wavenc->metadata->properties, "labels", &caps, NULL)) {
506 for (p = caps, i = 1; p; p = p->next, i++) {
507 GstCaps *labl_caps = p->data;
512 gst_props_get (labl_caps->properties, "name", &label, NULL);
513 len = strlen (label);
516 labl = g_string_new ("labl ");
517 labl->str[8] = GUINT32_TO_LE (i);
518 g_string_append (labl, label);
523 labl->str[4] = GUINT32_TO_LE (len);
526 req = ((len + 8) + 1) & ~1;
527 for (j = 0; j < req - len; j++) {
528 g_string_append_printf (labl, "%c", 0);
533 g_string_append (info_str, labl->str);
534 g_string_free (labl, TRUE);
538 if (gst_props_get (wavenc->metadata->properties, "notes", &caps, NULL)) {
542 for (p = caps, i = 1; p; p = p->next, i++) {
543 GstCaps *note_caps = p->data;
548 gst_props_get (note_caps->properties, "name", &label, NULL);
549 len = strlen (label);
552 note = g_string_new ("note ");
553 note->str[8] = GUINT32_TO_LE (i);
554 g_string_append (note, label);
559 note->str[4] = GUINT32_TO_LE (len);
562 req = ((len + 8) + 1) & ~1;
563 for (j = 0; j < req - len; j++) {
564 g_string_append_printf (note, "%c", 0);
569 g_string_append (info_str, note->str);
570 g_string_free (note, TRUE);
574 info_str->str[4] = GUINT32_TO_LE (total);
576 buf = gst_buffer_new ();
577 gst_buffer_set_data (buf, info_str->str, info_str->len);
579 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
580 g_string_free (info_str, FALSE);
585 gst_wavenc_chain (GstPad * pad, GstData * _data)
587 GstBuffer *buf = GST_BUFFER (_data);
590 wavenc = GST_WAVENC (gst_pad_get_parent (pad));
592 if (!wavenc->setup) {
593 gst_buffer_unref (buf);
594 GST_ELEMENT_ERROR (wavenc, CORE, NEGOTIATION, (NULL),
595 ("encoder not initialised (input is not audio?)"));
599 if (GST_IS_EVENT (buf)) {
600 if (GST_EVENT_TYPE (buf) == GST_EVENT_EOS) {
601 wavenc->pad_eos = TRUE;
604 /* Write our metadata if we have any */
605 if (wavenc->metadata) {
606 write_metadata (wavenc);
608 write_labels (wavenc);
612 gst_wavenc_stop_file (wavenc);
613 gst_pad_push (wavenc->srcpad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
614 gst_element_set_eos (GST_ELEMENT (wavenc));
616 gst_pad_event_default (wavenc->srcpad, GST_EVENT (buf));
621 if (GST_PAD_IS_USABLE (wavenc->srcpad)) {
622 if (wavenc->flush_header) {
625 outbuf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
626 memcpy (GST_BUFFER_DATA (outbuf), wavenc->header, WAV_HEADER_LEN);
627 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
629 gst_pad_push (wavenc->srcpad, GST_DATA (outbuf));
630 wavenc->flush_header = FALSE;
633 wavenc->length += GST_BUFFER_SIZE (buf);
634 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
639 plugin_init (GstPlugin * plugin)
641 return gst_element_register (plugin, "wavenc", GST_RANK_NONE,
645 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
648 "Encode raw audio into WAV",
649 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)