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 GST_DEBUG_CATEGORY_EXTERN (wavenc_debug);
31 #define GST_CAT_DEFAULT wavenc_debug
33 static void gst_wavenc_base_init (gpointer g_class);
34 static void gst_wavenc_class_init (GstWavEncClass * klass);
35 static void gst_wavenc_init (GstWavEnc * wavenc);
36 static GstFlowReturn gst_wavenc_chain (GstPad * pad, GstBuffer * buf);
38 #define WAVE_FORMAT_PCM 0x0001
42 guint8 id[4]; /* RIFF */
44 guint8 wav_id[4]; /* WAVE */
57 guint32 dwSamplesPerSec;
58 guint32 dwAvgBytesPerSec;
60 guint16 wBitsPerSample; /* Only for PCM */
65 struct riff_struct riff;
66 struct chunk_struct format;
67 struct common_struct common;
68 struct chunk_struct data;
71 static GstElementDetails gst_wavenc_details =
72 GST_ELEMENT_DETAILS ("WAV encoder",
74 "Encode raw audio into WAV",
75 "Iain Holmes <iain@prettypeople.org>");
77 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
80 GST_STATIC_CAPS ("audio/x-raw-int, "
81 "rate = (int) [ 1, MAX ], "
82 "channels = (int) [ 1, MAX ], "
83 "endianness = (int) LITTLE_ENDIAN, "
84 "width = (int) { 8, 16 }, "
85 "depth = (int) { 8, 16 }, " "signed = (boolean) true")
88 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
91 GST_STATIC_CAPS ("audio/x-wav")
99 static GstElementClass *parent_class = NULL;
102 gst_wavenc_get_type (void)
104 static GType type = 0;
107 static const GTypeInfo info = {
108 sizeof (GstWavEncClass),
109 gst_wavenc_base_init,
111 (GClassInitFunc) gst_wavenc_class_init,
116 (GInstanceInitFunc) gst_wavenc_init
119 type = g_type_register_static (GST_TYPE_ELEMENT, "GstWavEnc", &info, 0);
125 static GstElementStateReturn
126 gst_wavenc_change_state (GstElement * element)
128 GstWavEnc *wavenc = GST_WAVENC (element);
130 switch (GST_STATE_TRANSITION (element)) {
131 case GST_STATE_PAUSED_TO_READY:
132 wavenc->setup = FALSE;
133 wavenc->flush_header = TRUE;
139 if (parent_class->change_state) {
140 return parent_class->change_state (element);
143 return GST_STATE_SUCCESS;
147 set_property (GObject * object,
148 guint prop_id, const GValue * value, GParamSpec * pspec)
152 enc = GST_WAVENC (object);
161 gst_wavenc_base_init (gpointer g_class)
163 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
165 gst_element_class_set_details (element_class, &gst_wavenc_details);
167 gst_element_class_add_pad_template (element_class,
168 gst_static_pad_template_get (&src_factory));
169 gst_element_class_add_pad_template (element_class,
170 gst_static_pad_template_get (&sink_factory));
173 gst_wavenc_class_init (GstWavEncClass * klass)
175 GstElementClass *element_class;
176 GObjectClass *object_class;
178 element_class = (GstElementClass *) klass;
179 object_class = (GObjectClass *) klass;
180 object_class->set_property = set_property;
182 element_class->change_state = gst_wavenc_change_state;
184 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
188 gst_wavenc_setup (GstWavEnc * wavenc)
190 struct wave_header wave;
191 gint size = 0x7fffffff; /* Use a bogus size initially */
193 wave.common.wChannels = wavenc->channels;
194 wave.common.wBitsPerSample = wavenc->bits;
195 wave.common.dwSamplesPerSec = wavenc->rate;
197 memset (wavenc->header, 0, WAV_HEADER_LEN);
199 /* Fill out our wav-header with some information */
200 strncpy ((char *) wave.riff.id, "RIFF", 4);
201 wave.riff.len = size - 8;
202 strncpy ((char *) wave.riff.wav_id, "WAVE", 4);
204 strncpy ((char *) wave.format.id, "fmt ", 4);
205 wave.format.len = 16;
207 wave.common.wFormatTag = WAVE_FORMAT_PCM;
208 wave.common.dwAvgBytesPerSec =
209 wave.common.wChannels * wave.common.dwSamplesPerSec *
210 (wave.common.wBitsPerSample >> 3);
211 wave.common.wBlockAlign =
212 wave.common.wChannels * (wave.common.wBitsPerSample >> 3);
214 strncpy ((char *) wave.data.id, "data", 4);
215 wave.data.len = size - 44;
217 strncpy ((char *) wavenc->header, (char *) wave.riff.id, 4);
218 GST_WRITE_UINT32_LE (wavenc->header + 4, wave.riff.len);
219 strncpy ((char *) wavenc->header + 8, (char *) wave.riff.wav_id, 4);
220 strncpy ((char *) wavenc->header + 12, (char *) wave.format.id, 4);
221 GST_WRITE_UINT32_LE (wavenc->header + 16, wave.format.len);
222 GST_WRITE_UINT16_LE (wavenc->header + 20, wave.common.wFormatTag);
223 GST_WRITE_UINT16_LE (wavenc->header + 22, wave.common.wChannels);
224 GST_WRITE_UINT32_LE (wavenc->header + 24, wave.common.dwSamplesPerSec);
225 GST_WRITE_UINT32_LE (wavenc->header + 28, wave.common.dwAvgBytesPerSec);
226 GST_WRITE_UINT16_LE (wavenc->header + 32, wave.common.wBlockAlign);
227 GST_WRITE_UINT16_LE (wavenc->header + 34, wave.common.wBitsPerSample);
228 strncpy ((char *) wavenc->header + 36, (char *) wave.data.id, 4);
229 GST_WRITE_UINT32_LE (wavenc->header + 40, wave.data.len);
232 wavenc->setup = TRUE;
237 gst_wavenc_sink_setcaps (GstPad * pad, GstCaps * caps)
240 GstStructure *structure;
242 wavenc = GST_WAVENC (gst_pad_get_parent (pad));
243 wavenc->setup = FALSE;
245 structure = gst_caps_get_structure (caps, 0);
246 gst_structure_get_int (structure, "channels", (gint *) & wavenc->channels);
247 gst_structure_get_int (structure, "rate", (gint *) & wavenc->rate);
248 gst_structure_get_int (structure, "depth", (gint *) & wavenc->bits);
250 gst_wavenc_setup (wavenc);
260 gst_wavenc_stop_file (GstWavEnc * wavenc)
265 event = gst_event_new_newsegment (1.0, GST_FORMAT_TIME,
266 0, GST_CLOCK_TIME_NONE, 0);
268 gst_pad_push_event (wavenc->srcpad, event);
270 outbuf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
271 GST_WRITE_UINT32_LE (wavenc->header + 4,
272 wavenc->length + (WAV_HEADER_LEN - 8));
273 GST_WRITE_UINT32_LE (wavenc->header + 40, wavenc->length);
274 memcpy (GST_BUFFER_DATA (outbuf), wavenc->header, WAV_HEADER_LEN);
275 GST_BUFFER_OFFSET (outbuf) = 0;
276 GST_BUFFER_OFFSET_END (outbuf) = WAV_HEADER_LEN;
277 GST_BUFFER_TIMESTAMP (outbuf) = 0;
278 GST_BUFFER_DURATION (outbuf) = 0;
280 gst_pad_push (wavenc->srcpad, outbuf);
284 gst_wavenc_init (GstWavEnc * wavenc)
286 GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavenc);
289 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
291 gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->sinkpad);
292 gst_pad_set_chain_function (wavenc->sinkpad, gst_wavenc_chain);
293 gst_pad_set_setcaps_function (wavenc->sinkpad, gst_wavenc_sink_setcaps);
296 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
298 gst_element_add_pad (GST_ELEMENT (wavenc), wavenc->srcpad);
300 wavenc->setup = FALSE;
301 wavenc->flush_header = TRUE;
302 wavenc->newmediacount = 0;
312 GST_RIFF_INFO_IARL, "Location"}
314 GST_RIFF_INFO_IART, "Artist"}
316 GST_RIFF_INFO_ICMS, "Commissioner"}
318 GST_RIFF_INFO_ICMT, "Comment"}
320 GST_RIFF_INFO_ICOP, "Copyright"}
322 GST_RIFF_INFO_ICRD, "Creation Date"}
324 GST_RIFF_INFO_IENG, "Engineer"}
326 GST_RIFF_INFO_IGNR, "Genre"}
328 GST_RIFF_INFO_IKEY, "Keywords"}
330 GST_RIFF_INFO_INAM, "Title"}
333 GST_RIFF_INFO_IPRD, "Product"}
335 GST_RIFF_INFO_ISBJ, "Subject"}
337 GST_RIFF_INFO_ISFT, "Software"}
339 GST_RIFF_INFO_ITCH, "Technician"}
346 get_id_from_name (const char *name)
350 for (i = 0; maps[i].name; i++) {
351 if (strcasecmp (maps[i].name, name) == 0) {
360 write_metadata (GstWavEnc * wavenc)
365 gboolean need_to_write = FALSE;
367 info_str = g_string_new ("LIST INFO");
369 for (props = wavenc->metadata->properties->properties; props;
370 props = props->next) {
371 GstPropsEntry *entry = props->data;
375 name = gst_props_entry_get_name (entry);
376 id = get_id_from_name (name);
382 need_to_write = TRUE; /* We've got at least one entry */
384 gst_props_entry_get_string (entry, &text);
385 len = strlen (text) + 1; /* The length in the file includes the \0 */
387 tmp = g_strdup_printf (GST_FOURCC_FORMAT "%d%s", GST_FOURCC_ARGS (id),
388 GUINT32_TO_LE (len), text);
389 g_string_append (info_str, tmp);
392 /* Check that we end on an even boundary */
393 req = ((len + 8) + 1) & ~1;
394 for (i = 0; i < req - len; i++) {
395 g_string_append_printf (info_str, "%c", 0);
405 /* Now we've got all the strings together, we can write our length in */
406 info_str->str[4] = GUINT32_TO_LE (total);
408 buf = gst_buffer_new ();
409 gst_buffer_set_data (buf, info_str->str, info_str->len);
411 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
412 g_string_free (info_str, FALSE);
417 write_cues (GstWavEnc * wavenc)
419 GString *cue_string, *point_string;
422 int num_cues, total = 4;
424 if (gst_props_get (wavenc->metadata->properties,
425 "cues", &cue_list, NULL) == FALSE) {
426 /* No cues, move along please, nothing to see here */
430 /* Space for 'cue ', chunk size and number of cuepoints */
431 cue_string = g_string_new ("cue ");
432 #define CUEPOINT_SIZE 24
433 point_string = g_string_sized_new (CUEPOINT_SIZE);
435 for (c = cue_list, num_cues = 0; c; c = c->next, num_cues++) {
436 GstCaps *cue_caps = c->data;
439 gst_props_get (cue_caps->properties, "position", &pos, NULL);
441 point_string->str[0] = GUINT32_TO_LE (num_cues + 1);
442 point_string->str[4] = GUINT32_TO_LE (0);
443 /* Fixme: There is probably a macro for this */
444 point_string->str[8] = 'd';
445 point_string->str[9] = 'a';
446 point_string->str[10] = 't';
447 point_string->str[11] = 'a';
448 point_string->str[12] = GUINT32_TO_LE (0);
449 point_string->str[16] = GUINT32_TO_LE (0);
450 point_string->str[20] = GUINT32_TO_LE (pos);
452 total += CUEPOINT_SIZE;
455 /* Set the length and chunk size */
456 cue_string->str[4] = GUINT32_TO_LE (total);
457 cue_string->str[8] = GUINT32_TO_LE (num_cues);
458 /* Stick the cue points on the end */
459 g_string_append (cue_string, point_string->str);
460 g_string_free (point_string, TRUE);
462 buf = gst_buffer_new ();
463 gst_buffer_set_data (buf, cue_string->str, cue_string->len);
465 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
466 g_string_free (cue_string, FALSE);
470 write_labels (GstWavEnc * wavenc)
477 info_str = g_string_new ("LIST adtl");
478 if (gst_props_get (wavenc->metadata->properties, "ltxts", &caps, NULL)) {
482 for (p = caps, i = 1; p; p = p->next, i++) {
483 GstCaps *ltxt_caps = p->data;
488 gst_props_get (ltxt_caps->properties, "name", &label, NULL);
489 len = strlen (label);
492 ltxt = g_string_new ("ltxt ");
493 ltxt->str[8] = GUINT32_TO_LE (i); /* Identifier */
494 ltxt->str[12] = GUINT32_TO_LE (0); /* Sample Length */
495 ltxt->str[16] = GUINT32_TO_LE (0); /* FIXME: Don't save the purpose yet */
496 ltxt->str[20] = GUINT16_TO_LE (0); /* Country */
497 ltxt->str[22] = GUINT16_TO_LE (0); /* Language */
498 ltxt->str[24] = GUINT16_TO_LE (0); /* Dialect */
499 ltxt->str[26] = GUINT16_TO_LE (0); /* Code Page */
500 g_string_append (ltxt, label);
505 ltxt->str[4] = GUINT32_TO_LE (len);
507 /* Check that we end on an even boundary */
508 req = ((len + 8) + 1) & ~1;
509 for (j = 0; j < req - len; j++) {
510 g_string_append_printf (ltxt, "%c", 0);
515 g_string_append (info_str, ltxt->str);
516 g_string_free (ltxt, TRUE);
520 if (gst_props_get (wavenc->metadata->properties, "labels", &caps, NULL)) {
524 for (p = caps, i = 1; p; p = p->next, i++) {
525 GstCaps *labl_caps = p->data;
530 gst_props_get (labl_caps->properties, "name", &label, NULL);
531 len = strlen (label);
534 labl = g_string_new ("labl ");
535 labl->str[8] = GUINT32_TO_LE (i);
536 g_string_append (labl, label);
541 labl->str[4] = GUINT32_TO_LE (len);
544 req = ((len + 8) + 1) & ~1;
545 for (j = 0; j < req - len; j++) {
546 g_string_append_printf (labl, "%c", 0);
551 g_string_append (info_str, labl->str);
552 g_string_free (labl, TRUE);
556 if (gst_props_get (wavenc->metadata->properties, "notes", &caps, NULL)) {
560 for (p = caps, i = 1; p; p = p->next, i++) {
561 GstCaps *note_caps = p->data;
566 gst_props_get (note_caps->properties, "name", &label, NULL);
567 len = strlen (label);
570 note = g_string_new ("note ");
571 note->str[8] = GUINT32_TO_LE (i);
572 g_string_append (note, label);
577 note->str[4] = GUINT32_TO_LE (len);
580 req = ((len + 8) + 1) & ~1;
581 for (j = 0; j < req - len; j++) {
582 g_string_append_printf (note, "%c", 0);
587 g_string_append (info_str, note->str);
588 g_string_free (note, TRUE);
592 info_str->str[4] = GUINT32_TO_LE (total);
594 buf = gst_buffer_new ();
595 gst_buffer_set_data (buf, info_str->str, info_str->len);
597 gst_pad_push (wavenc->srcpad, GST_DATA (buf));
598 g_string_free (info_str, FALSE);
603 gst_wavenc_chain (GstPad * pad, GstBuffer * buf)
606 GstFlowReturn result = GST_FLOW_OK;
608 wavenc = GST_WAVENC (gst_pad_get_parent (pad));
610 if (GST_IS_EVENT (buf)) {
611 if (GST_EVENT_TYPE (buf) == GST_EVENT_EOS) {
612 wavenc->pad_eos = TRUE;
615 /* Write our metadata if we have any */
616 if (wavenc->metadata) {
617 write_metadata (wavenc);
619 write_labels (wavenc);
623 gst_wavenc_stop_file (wavenc);
624 gst_pad_push (wavenc->srcpad, buf);
625 gst_pad_push_event (wavenc->srcpad, gst_event_new_eos ());
626 } else if (GST_EVENT_TYPE (buf) == GST_EVENT_NEWSEGMENT) {
627 //if (GST_EVENT_DISCONT_NEW_MEDIA (buf)) {
628 if (wavenc->newmediacount++ > 0) {
629 gst_wavenc_stop_file (wavenc);
630 wavenc->setup = FALSE;
631 wavenc->flush_header = TRUE;
632 gst_wavenc_setup (wavenc);
634 gst_pad_event_default (wavenc->sinkpad, GST_EVENT (buf));
637 gst_pad_event_default (wavenc->sinkpad, GST_EVENT (buf));
643 if (!wavenc->setup) {
644 gst_buffer_unref (buf);
645 GST_ELEMENT_ERROR (wavenc, CORE, NEGOTIATION, (NULL),
646 ("encoder not initialised (input is not audio?)"));
647 return GST_FLOW_ERROR;
650 if (GST_PAD_IS_USABLE (wavenc->srcpad)) {
651 if (wavenc->flush_header) {
654 outbuf = gst_buffer_new_and_alloc (WAV_HEADER_LEN);
655 memcpy (GST_BUFFER_DATA (outbuf), wavenc->header, WAV_HEADER_LEN);
656 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
658 gst_pad_push (wavenc->srcpad, outbuf);
659 wavenc->flush_header = FALSE;
662 wavenc->length += GST_BUFFER_SIZE (buf);
663 gst_pad_push (wavenc->srcpad, buf);
670 plugin_init (GstPlugin * plugin)
672 return gst_element_register (plugin, "wavenc", GST_RANK_NONE,
676 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
679 "Encode raw audio into WAV",
680 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)