1 /* -*- mOde: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
2 /* GStreamer .wav encoder
3 * Copyright (C) <2002> Iain Holmes <iain@prettypeople.org>
4 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-wavenc
25 * Format a audio stream into the wav format.
33 #include "gstwavenc.h"
36 GST_DEBUG_CATEGORY_STATIC (wavenc_debug);
37 #define GST_CAT_DEFAULT wavenc_debug
41 guint8 id[4]; /* RIFF */
43 guint8 wav_id[4]; /* WAVE */
56 guint32 dwSamplesPerSec;
57 guint32 dwAvgBytesPerSec;
59 guint16 wBitsPerSample; /* Only for PCM */
64 struct riff_struct riff;
65 struct chunk_struct format;
66 struct common_struct common;
67 struct chunk_struct data;
70 static const GstElementDetails gst_wavenc_details =
71 GST_ELEMENT_DETAILS ("WAV audio muxer",
73 "Encode raw audio into WAV",
74 "Iain Holmes <iain@prettypeople.org>");
76 /* FIXME: mono doesn't produce correct files it seems, at least mplayer xruns */
77 /* Max. of two channels, more channels need WAVFORMATEX with
78 * channel layout, which we do not support yet */
81 "rate = (int) [ 1, MAX ], " \
82 "channels = (int) [ 1, 2 ], " \
83 "endianness = (int) LITTLE_ENDIAN, " \
84 "width = (int) 32, " \
85 "depth = (int) 32, " \
86 "signed = (boolean) true" \
89 "rate = (int) [ 1, MAX ], " \
90 "channels = (int) [ 1, 2 ], " \
91 "endianness = (int) LITTLE_ENDIAN, " \
92 "width = (int) 24, " \
93 "depth = (int) 24, " \
94 "signed = (boolean) true" \
97 "rate = (int) [ 1, MAX ], " \
98 "channels = (int) [ 1, 2 ], " \
99 "endianness = (int) LITTLE_ENDIAN, " \
100 "width = (int) 16, " \
101 "depth = (int) 16, " \
102 "signed = (boolean) true" \
104 "audio/x-raw-int, " \
105 "rate = (int) [ 1, MAX ], " \
106 "channels = (int) [ 1, 2 ], " \
107 "width = (int) 8, " \
108 "depth = (int) 8, " \
109 "signed = (boolean) false" \
111 "audio/x-raw-float, " \
112 "rate = (int) [ 1, MAX ], " \
113 "channels = (int) [ 1, 2 ], " \
114 "endianness = (int) LITTLE_ENDIAN, " \
115 "width = (int) { 32, 64 }; " \
117 "rate = (int) [ 8000, 192000 ], " \
118 "channels = (int) [ 1, 2 ], " \
119 "width = (int) 8, " \
120 "depth = (int) 8, " \
121 "signed = (boolean) false; " \
123 "rate = (int) [ 8000, 192000 ], " \
124 "channels = (int) [ 1, 2 ], " \
125 "width = (int) 8, " \
126 "depth = (int) 8, " \
127 "signed = (boolean) false"
130 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
133 GST_STATIC_CAPS (SINK_CAPS)
136 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
139 GST_STATIC_CAPS ("audio/x-wav")
142 GST_BOILERPLATE (GstWavEnc, gst_wavenc, GstElement, GST_TYPE_ELEMENT);
144 static GstFlowReturn gst_wavenc_chain (GstPad * pad, GstBuffer * buf);
145 static gboolean gst_wavenc_event (GstPad * pad, GstEvent * event);
146 static GstStateChangeReturn gst_wavenc_change_state (GstElement * element,
147 GstStateChange transition);
148 static gboolean gst_wavenc_sink_setcaps (GstPad * pad, GstCaps * caps);
151 gst_wavenc_base_init (gpointer g_class)
153 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
155 gst_element_class_set_details (element_class, &gst_wavenc_details);
157 gst_element_class_add_pad_template (element_class,
158 gst_static_pad_template_get (&src_factory));
159 gst_element_class_add_pad_template (element_class,
160 gst_static_pad_template_get (&sink_factory));
162 GST_DEBUG_CATEGORY_INIT (wavenc_debug, "wavenc", 0, "WAV encoder element");
166 gst_wavenc_class_init (GstWavEncClass * klass)
168 GstElementClass *element_class;
170 element_class = (GstElementClass *) klass;
172 element_class->change_state = GST_DEBUG_FUNCPTR (gst_wavenc_change_state);
176 gst_wavenc_init (GstWavEnc * wavenc, GstWavEncClass * klass)
178 wavenc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
179 gst_pad_set_chain_function (wavenc->sinkpad,
180 GST_DEBUG_FUNCPTR (gst_wavenc_chain));
181 gst_pad_set_event_function (wavenc->sinkpad,
182 GST_DEBUG_FUNCPTR (gst_wavenc_event));
183 gst_pad_set_setcaps_function (wavenc->sinkpad,
184 GST_DEBUG_FUNCPTR (gst_wavenc_sink_setcaps));
185 gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->sinkpad);
187 wavenc->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
188 gst_pad_use_fixed_caps (wavenc->srcpad);
189 gst_pad_set_caps (wavenc->srcpad,
190 gst_static_pad_template_get_caps (&src_factory));
191 gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->srcpad);
194 #define WAV_HEADER_LEN 44
197 gst_wavenc_create_header_buf (GstWavEnc * wavenc, guint audio_data_size)
199 struct wave_header wave;
203 buf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
204 header = GST_BUFFER_DATA (buf);
205 memset (header, 0, WAV_HEADER_LEN);
207 wave.common.wChannels = wavenc->channels;
208 wave.common.wBitsPerSample = wavenc->width;
209 wave.common.dwSamplesPerSec = wavenc->rate;
211 /* Fill out our wav-header with some information */
212 memcpy (wave.riff.id, "RIFF", 4);
213 wave.riff.len = audio_data_size + WAV_HEADER_LEN - 8;
214 memcpy (wave.riff.wav_id, "WAVE", 4);
216 memcpy (wave.format.id, "fmt ", 4);
217 wave.format.len = 16;
219 wave.common.wFormatTag = wavenc->format;
220 wave.common.wBlockAlign = (wavenc->width / 8) * wave.common.wChannels;
221 wave.common.dwAvgBytesPerSec =
222 wave.common.wBlockAlign * wave.common.dwSamplesPerSec;
224 memcpy (wave.data.id, "data", 4);
225 wave.data.len = audio_data_size;
227 memcpy (header, (char *) wave.riff.id, 4);
228 GST_WRITE_UINT32_LE (header + 4, wave.riff.len);
229 memcpy (header + 8, (char *) wave.riff.wav_id, 4);
230 memcpy (header + 12, (char *) wave.format.id, 4);
231 GST_WRITE_UINT32_LE (header + 16, wave.format.len);
232 GST_WRITE_UINT16_LE (header + 20, wave.common.wFormatTag);
233 GST_WRITE_UINT16_LE (header + 22, wave.common.wChannels);
234 GST_WRITE_UINT32_LE (header + 24, wave.common.dwSamplesPerSec);
235 GST_WRITE_UINT32_LE (header + 28, wave.common.dwAvgBytesPerSec);
236 GST_WRITE_UINT16_LE (header + 32, wave.common.wBlockAlign);
237 GST_WRITE_UINT16_LE (header + 34, wave.common.wBitsPerSample);
238 memcpy (header + 36, (char *) wave.data.id, 4);
239 GST_WRITE_UINT32_LE (header + 40, wave.data.len);
241 gst_buffer_set_caps (buf, GST_PAD_CAPS (wavenc->srcpad));
247 gst_wavenc_push_header (GstWavEnc * wavenc, guint audio_data_size)
252 /* seek to beginning of file */
253 gst_pad_push_event (wavenc->srcpad,
254 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
255 0, GST_CLOCK_TIME_NONE, 0));
257 GST_DEBUG_OBJECT (wavenc, "writing header with datasize=%u", audio_data_size);
259 outbuf = gst_wavenc_create_header_buf (wavenc, audio_data_size);
260 GST_BUFFER_OFFSET (outbuf) = 0;
262 ret = gst_pad_push (wavenc->srcpad, outbuf);
264 if (ret != GST_FLOW_OK) {
265 GST_WARNING_OBJECT (wavenc, "push header failed: flow = %s",
266 gst_flow_get_name (ret));
273 gst_wavenc_sink_setcaps (GstPad * pad, GstCaps * caps)
276 GstStructure *structure;
278 gint chans, rate, width;
280 wavenc = GST_WAVENC (gst_pad_get_parent (pad));
282 if (wavenc->sent_header) {
283 GST_WARNING_OBJECT (wavenc, "cannot change format in middle of stream");
287 GST_DEBUG_OBJECT (wavenc, "got caps: %" GST_PTR_FORMAT, caps);
289 structure = gst_caps_get_structure (caps, 0);
290 name = gst_structure_get_name (structure);
292 if (!gst_structure_get_int (structure, "channels", &chans) ||
293 !gst_structure_get_int (structure, "rate", &rate)) {
294 GST_WARNING_OBJECT (wavenc, "caps incomplete");
298 if (strcmp (name, "audio/x-raw-int") == 0) {
299 if (!gst_structure_get_int (structure, "width", &width)) {
300 GST_WARNING_OBJECT (wavenc, "caps incomplete");
303 wavenc->format = GST_RIFF_WAVE_FORMAT_PCM;
304 wavenc->width = width;
305 } else if (strcmp (name, "audio/x-raw-float") == 0) {
306 if (!gst_structure_get_int (structure, "width", &width)) {
307 GST_WARNING_OBJECT (wavenc, "caps incomplete");
310 wavenc->format = GST_RIFF_WAVE_FORMAT_FLOAT;
311 wavenc->width = width;
312 } else if (strcmp (name, "audio/x-alaw") == 0) {
313 wavenc->format = GST_RIFF_WAVE_FORMAT_ALAW;
315 } else if (strcmp (name, "audio/x-mulaw") == 0) {
316 wavenc->format = GST_RIFF_WAVE_FORMAT_MULAW;
319 GST_WARNING_OBJECT (wavenc, "Unsupported format %s", name);
323 wavenc->channels = chans;
326 GST_LOG_OBJECT (wavenc,
327 "accepted caps: format=0x%04x chans=%u width=%u rate=%u",
328 wavenc->format, wavenc->channels, wavenc->width, wavenc->rate);
330 gst_object_unref (wavenc);
334 gst_object_unref (wavenc);
345 GST_RIFF_INFO_IARL, "Location"}, {
346 GST_RIFF_INFO_IART, "Artist"}, {
347 GST_RIFF_INFO_ICMS, "Commissioner"}, {
348 GST_RIFF_INFO_ICMT, "Comment"}, {
349 GST_RIFF_INFO_ICOP, "Copyright"}, {
350 GST_RIFF_INFO_ICRD, "Creation Date"}, {
351 GST_RIFF_INFO_IENG, "Engineer"}, {
352 GST_RIFF_INFO_IGNR, "Genre"}, {
353 GST_RIFF_INFO_IKEY, "Keywords"}, {
354 GST_RIFF_INFO_INAM, "Title"}, {
355 GST_RIFF_INFO_IPRD, "Product"}, {
356 GST_RIFF_INFO_ISBJ, "Subject"}, {
357 GST_RIFF_INFO_ISFT, "Software"}, {
358 GST_RIFF_INFO_ITCH, "Technician"}
362 get_id_from_name (const char *name)
366 for (i = 0; i < G_N_ELEMENTS (maps); i++) {
367 if (strcasecmp (maps[i].name, name) == 0) {
376 write_metadata (GstWavEnc * wavenc)
381 gboolean need_to_write = FALSE;
383 info_str = g_string_new ("LIST INFO");
385 for (props = wavenc->metadata->properties->properties; props;
386 props = props->next) {
387 GstPropsEntry *entry = props->data;
391 name = gst_props_entry_get_name (entry);
392 id = get_id_from_name (name);
398 need_to_write = TRUE; /* We've got at least one entry */
400 gst_props_entry_get_string (entry, &text);
401 len = strlen (text) + 1; /* The length in the file includes the \0 */
403 tmp = g_strdup_printf ("%" GST_FOURCC_FORMAT "%d%s", GST_FOURCC_ARGS (id),
404 GUINT32_TO_LE (len), text);
405 g_string_append (info_str, tmp);
408 /* Check that we end on an even boundary */
409 req = ((len + 8) + 1) & ~1;
410 for (i = 0; i < req - len; i++) {
411 g_string_append_printf (info_str, "%c", 0);
421 /* Now we've got all the strings together, we can write our length in */
422 info_str->str[4] = GUINT32_TO_LE (total);
424 buf = gst_buffer_new ();
425 gst_buffer_set_data (buf, info_str->str, info_str->len);
427 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
428 g_string_free (info_str, FALSE);
433 write_cues (GstWavEnc * wavenc)
435 GString *cue_string, *point_string;
438 int num_cues, total = 4;
440 if (gst_props_get (wavenc->metadata->properties,
441 "cues", &cue_list, NULL) == FALSE) {
442 /* No cues, move along please, nothing to see here */
446 /* Space for 'cue ', chunk size and number of cuepoints */
447 cue_string = g_string_new ("cue ");
448 #define CUEPOINT_SIZE 24
449 point_string = g_string_sized_new (CUEPOINT_SIZE);
451 for (c = cue_list, num_cues = 0; c; c = c->next, num_cues++) {
452 GstCaps *cue_caps = c->data;
455 gst_props_get (cue_caps->properties, "position", &pos, NULL);
457 point_string->str[0] = GUINT32_TO_LE (num_cues + 1);
458 point_string->str[4] = GUINT32_TO_LE (0);
459 /* Fixme: There is probably a macro for this */
460 point_string->str[8] = 'd';
461 point_string->str[9] = 'a';
462 point_string->str[10] = 't';
463 point_string->str[11] = 'a';
464 point_string->str[12] = GUINT32_TO_LE (0);
465 point_string->str[16] = GUINT32_TO_LE (0);
466 point_string->str[20] = GUINT32_TO_LE (pos);
468 total += CUEPOINT_SIZE;
471 /* Set the length and chunk size */
472 cue_string->str[4] = GUINT32_TO_LE (total);
473 cue_string->str[8] = GUINT32_TO_LE (num_cues);
474 /* Stick the cue points on the end */
475 g_string_append (cue_string, point_string->str);
476 g_string_free (point_string, TRUE);
478 buf = gst_buffer_new ();
479 gst_buffer_set_data (buf, cue_string->str, cue_string->len);
481 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
482 g_string_free (cue_string, FALSE);
486 write_labels (GstWavEnc * wavenc)
493 info_str = g_string_new ("LIST adtl");
494 if (gst_props_get (wavenc->metadata->properties, "ltxts", &caps, NULL)) {
498 for (p = caps, i = 1; p; p = p->next, i++) {
499 GstCaps *ltxt_caps = p->data;
504 gst_props_get (ltxt_caps->properties, "name", &label, NULL);
505 len = strlen (label);
508 ltxt = g_string_new ("ltxt ");
509 ltxt->str[8] = GUINT32_TO_LE (i); /* Identifier */
510 ltxt->str[12] = GUINT32_TO_LE (0); /* Sample Length */
511 ltxt->str[16] = GUINT32_TO_LE (0); /* FIXME: Don't save the purpose yet */
512 ltxt->str[20] = GUINT16_TO_LE (0); /* Country */
513 ltxt->str[22] = GUINT16_TO_LE (0); /* Language */
514 ltxt->str[24] = GUINT16_TO_LE (0); /* Dialect */
515 ltxt->str[26] = GUINT16_TO_LE (0); /* Code Page */
516 g_string_append (ltxt, label);
521 ltxt->str[4] = GUINT32_TO_LE (len);
523 /* Check that we end on an even boundary */
524 req = ((len + 8) + 1) & ~1;
525 for (j = 0; j < req - len; j++) {
526 g_string_append_printf (ltxt, "%c", 0);
531 g_string_append (info_str, ltxt->str);
532 g_string_free (ltxt, TRUE);
536 if (gst_props_get (wavenc->metadata->properties, "labels", &caps, NULL)) {
540 for (p = caps, i = 1; p; p = p->next, i++) {
541 GstCaps *labl_caps = p->data;
546 gst_props_get (labl_caps->properties, "name", &label, NULL);
547 len = strlen (label);
550 labl = g_string_new ("labl ");
551 labl->str[8] = GUINT32_TO_LE (i);
552 g_string_append (labl, label);
557 labl->str[4] = GUINT32_TO_LE (len);
560 req = ((len + 8) + 1) & ~1;
561 for (j = 0; j < req - len; j++) {
562 g_string_append_printf (labl, "%c", 0);
567 g_string_append (info_str, labl->str);
568 g_string_free (labl, TRUE);
572 if (gst_props_get (wavenc->metadata->properties, "notes", &caps, NULL)) {
576 for (p = caps, i = 1; p; p = p->next, i++) {
577 GstCaps *note_caps = p->data;
582 gst_props_get (note_caps->properties, "name", &label, NULL);
583 len = strlen (label);
586 note = g_string_new ("note ");
587 note->str[8] = GUINT32_TO_LE (i);
588 g_string_append (note, label);
593 note->str[4] = GUINT32_TO_LE (len);
596 req = ((len + 8) + 1) & ~1;
597 for (j = 0; j < req - len; j++) {
598 g_string_append_printf (note, "%c", 0);
603 g_string_append (info_str, note->str);
604 g_string_free (note, TRUE);
608 info_str->str[4] = GUINT32_TO_LE (total);
610 buf = gst_buffer_new ();
611 gst_buffer_set_data (buf, info_str->str, info_str->len);
613 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
614 g_string_free (info_str, FALSE);
619 gst_wavenc_event (GstPad * pad, GstEvent * event)
624 wavenc = GST_WAVENC (gst_pad_get_parent (pad));
626 switch (GST_EVENT_TYPE (event)) {
628 GST_DEBUG_OBJECT (wavenc, "got EOS");
630 /* Write our metadata if we have any */
631 if (wavenc->metadata) {
632 write_metadata (wavenc);
634 write_labels (wavenc);
637 /* write header with correct length values */
638 gst_wavenc_push_header (wavenc, wavenc->length);
640 /* and forward the EOS event */
641 res = gst_pad_event_default (pad, event);
644 case GST_EVENT_NEWSEGMENT:
645 /* Just drop it, it's probably in TIME format
646 * anyway. We'll send our own newsegment event */
647 gst_event_unref (event);
650 res = gst_pad_event_default (pad, event);
654 gst_object_unref (wavenc);
659 gst_wavenc_chain (GstPad * pad, GstBuffer * buf)
661 GstWavEnc *wavenc = GST_WAVENC (GST_PAD_PARENT (pad));
662 GstFlowReturn flow = GST_FLOW_OK;
664 g_return_val_if_fail (wavenc->channels > 0, GST_FLOW_WRONG_STATE);
666 if (!wavenc->sent_header) {
667 /* use bogus size initially, we'll write the real
668 * header when we get EOS and know the exact length */
669 flow = gst_wavenc_push_header (wavenc, 0x7FFF0000);
671 if (flow != GST_FLOW_OK)
674 GST_DEBUG_OBJECT (wavenc, "wrote dummy header");
675 wavenc->sent_header = TRUE;
678 GST_LOG_OBJECT (wavenc, "pushing %u bytes raw audio, ts=%" GST_TIME_FORMAT,
679 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
681 buf = gst_buffer_make_metadata_writable (buf);
683 gst_buffer_set_caps (buf, GST_PAD_CAPS (wavenc->srcpad));
684 GST_BUFFER_OFFSET (buf) = WAV_HEADER_LEN + wavenc->length;
685 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
687 wavenc->length += GST_BUFFER_SIZE (buf);
689 flow = gst_pad_push (wavenc->srcpad, buf);
694 static GstStateChangeReturn
695 gst_wavenc_change_state (GstElement * element, GstStateChange transition)
697 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
698 GstWavEnc *wavenc = GST_WAVENC (element);
700 switch (transition) {
701 case GST_STATE_CHANGE_NULL_TO_READY:
703 wavenc->channels = 0;
707 wavenc->sent_header = FALSE;
713 ret = parent_class->change_state (element, transition);
714 if (ret != GST_STATE_CHANGE_SUCCESS)
721 plugin_init (GstPlugin * plugin)
723 return gst_element_register (plugin, "wavenc", GST_RANK_PRIMARY,
727 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
730 "Encode raw audio into WAV",
731 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)