1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) <2006> Nokia Corporation, Stefan Kost <stefan.kost@nokia.com>.
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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-wavparse
25 * Parse a .wav file into raw or compressed audio.
27 * Wavparse supports both push and pull mode operations, making it possible to
28 * stream from a network source.
31 * <title>Example launch line</title>
33 * gst-launch-1.0 filesrc location=sine.wav ! wavparse ! audioconvert ! alsasink
34 * ]| Read a wav file and output to the soundcard using the ALSA element. The
35 * wav file is assumed to contain raw uncompressed samples.
37 * gst-launch-1.0 gnomevfssrc location=http://www.example.org/sine.wav ! queue ! wavparse ! audioconvert ! alsasink
38 * ]| Stream data from a network url.
44 * http://replaygain.hydrogenaudio.org/file_format_wav.html
54 #include "gstwavparse.h"
55 #include "gst/riff/riff-media.h"
56 #include <gst/base/gsttypefindhelper.h>
57 #include <gst/gst-i18n-plugin.h>
59 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
60 #define GST_CAT_DEFAULT (wavparse_debug)
62 #define GST_RIFF_TAG_Fake GST_MAKE_FOURCC ('F','a','k','e')
64 #define GST_BWF_TAG_iXML GST_MAKE_FOURCC ('i','X','M','L')
65 #define GST_BWF_TAG_qlty GST_MAKE_FOURCC ('q','l','t','y')
66 #define GST_BWF_TAG_mext GST_MAKE_FOURCC ('m','e','x','t')
67 #define GST_BWF_TAG_levl GST_MAKE_FOURCC ('l','e','v','l')
68 #define GST_BWF_TAG_link GST_MAKE_FOURCC ('l','i','n','k')
69 #define GST_BWF_TAG_axml GST_MAKE_FOURCC ('a','x','m','l')
71 /* Data size chunk of RF64,
72 * see http://tech.ebu.ch/docs/tech/tech3306-2009.pdf */
73 #define GST_RS64_TAG_DS64 GST_MAKE_FOURCC ('d','s','6','4')
75 static void gst_wavparse_dispose (GObject * object);
77 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad,
79 static gboolean gst_wavparse_sink_activate_mode (GstPad * sinkpad,
80 GstObject * parent, GstPadMode mode, gboolean active);
81 static gboolean gst_wavparse_send_event (GstElement * element,
83 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
84 GstStateChange transition);
86 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
88 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
89 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
91 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
93 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
95 static void gst_wavparse_loop (GstPad * pad);
96 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
99 static void gst_wavparse_set_property (GObject * object, guint prop_id,
100 const GValue * value, GParamSpec * pspec);
101 static void gst_wavparse_get_property (GObject * object, guint prop_id,
102 GValue * value, GParamSpec * pspec);
104 #define DEFAULT_IGNORE_LENGTH FALSE
112 static GstStaticPadTemplate sink_template_factory =
113 GST_STATIC_PAD_TEMPLATE ("sink",
116 GST_STATIC_CAPS ("audio/x-wav")
120 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
122 #define gst_wavparse_parent_class parent_class
123 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
128 /* Offset Size Description Value
129 * 0x00 4 ID unique identification value
130 * 0x04 4 Position play order position
131 * 0x08 4 Data Chunk ID RIFF ID of corresponding data chunk
132 * 0x0c 4 Chunk Start Byte Offset of Data Chunk *
133 * 0x10 4 Block Start Byte Offset to sample of First Channel
134 * 0x14 4 Sample Offset Byte Offset to sample byte of First Channel
138 guint32 data_chunk_id;
141 guint32 sample_offset;
146 /* Offset Size Description Value
147 * 0x08 4 Cue Point ID 0 - 0xFFFFFFFF
150 guint32 cue_point_id;
152 } GstWavParseLabl, GstWavParseNote;
155 gst_wavparse_class_init (GstWavParseClass * klass)
157 GstElementClass *gstelement_class;
158 GObjectClass *object_class;
159 GstPadTemplate *src_template;
161 gstelement_class = (GstElementClass *) klass;
162 object_class = (GObjectClass *) klass;
164 parent_class = g_type_class_peek_parent (klass);
166 object_class->dispose = gst_wavparse_dispose;
168 object_class->set_property = gst_wavparse_set_property;
169 object_class->get_property = gst_wavparse_get_property;
172 * GstWavParse:ignore-length:
174 * This selects whether the length found in a data chunk
175 * should be ignored. This may be useful for streamed audio
176 * where the length is unknown until the end of streaming,
177 * and various software/hardware just puts some random value
178 * in there and hopes it doesn't break too much.
180 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
181 g_param_spec_boolean ("ignore-length",
183 "Ignore length from the Wave header",
184 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
187 gstelement_class->change_state = gst_wavparse_change_state;
188 gstelement_class->send_event = gst_wavparse_send_event;
191 gst_element_class_add_pad_template (gstelement_class,
192 gst_static_pad_template_get (&sink_template_factory));
194 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
195 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
196 gst_element_class_add_pad_template (gstelement_class, src_template);
198 gst_element_class_set_static_metadata (gstelement_class, "WAV audio demuxer",
199 "Codec/Demuxer/Audio",
200 "Parse a .wav file into raw audio",
201 "Erik Walthinsen <omega@cse.ogi.edu>");
205 gst_wavparse_reset (GstWavParse * wav)
207 wav->state = GST_WAVPARSE_START;
209 /* These will all be set correctly in the fmt chunk */
223 wav->got_fmt = FALSE;
227 gst_event_unref (wav->seek_event);
228 wav->seek_event = NULL;
230 gst_adapter_clear (wav->adapter);
231 g_object_unref (wav->adapter);
235 gst_tag_list_unref (wav->tags);
238 gst_toc_unref (wav->toc);
241 g_list_free_full (wav->cues, g_free);
244 g_list_free_full (wav->labls, g_free);
247 gst_caps_unref (wav->caps);
249 if (wav->start_segment)
250 gst_event_unref (wav->start_segment);
251 wav->start_segment = NULL;
255 gst_wavparse_dispose (GObject * object)
257 GstWavParse *wav = GST_WAVPARSE (object);
259 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
260 gst_wavparse_reset (wav);
262 G_OBJECT_CLASS (parent_class)->dispose (object);
266 gst_wavparse_init (GstWavParse * wavparse)
268 gst_wavparse_reset (wavparse);
272 gst_pad_new_from_static_template (&sink_template_factory, "sink");
273 gst_pad_set_activate_function (wavparse->sinkpad,
274 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
275 gst_pad_set_activatemode_function (wavparse->sinkpad,
276 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
277 gst_pad_set_chain_function (wavparse->sinkpad,
278 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
279 gst_pad_set_event_function (wavparse->sinkpad,
280 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
281 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
285 gst_pad_new_from_template (gst_element_class_get_pad_template
286 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
287 gst_pad_use_fixed_caps (wavparse->srcpad);
288 gst_pad_set_query_function (wavparse->srcpad,
289 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
290 gst_pad_set_event_function (wavparse->srcpad,
291 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
292 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
296 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
300 if (!gst_riff_parse_file_header (element, buf, &doctype))
303 if (doctype != GST_RIFF_RIFF_WAVE)
311 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
312 ("File is not a WAVE file: 0x%" G_GINT32_MODIFIER "x", doctype));
318 gst_wavparse_stream_init (GstWavParse * wav)
321 GstBuffer *buf = NULL;
323 if ((res = gst_pad_pull_range (wav->sinkpad,
324 wav->offset, 12, &buf)) != GST_FLOW_OK)
326 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
327 return GST_FLOW_ERROR;
335 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
337 /* -1 always maps to -1 */
343 /* 0 always maps to 0 */
350 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
352 } else if (wav->fact) {
353 guint64 bps = gst_util_uint64_scale (wav->datasize, wav->rate, wav->fact);
354 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
361 /* This function is used to perform seeks on the element.
363 * It also works when event is NULL, in which case it will just
364 * start from the last configured segment. This technique is
365 * used when activating the element and to perform the seek in
369 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
373 GstFormat format, bformat;
375 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
376 gint64 cur, stop, upstream_size;
379 GstSegment seeksegment = { 0, };
383 GST_DEBUG_OBJECT (wav, "doing seek with event");
385 gst_event_parse_seek (event, &rate, &format, &flags,
386 &cur_type, &cur, &stop_type, &stop);
388 /* no negative rates yet */
392 if (format != wav->segment.format) {
393 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
394 gst_format_get_name (format),
395 gst_format_get_name (wav->segment.format));
397 if (cur_type != GST_SEEK_TYPE_NONE)
399 gst_pad_query_convert (wav->srcpad, format, cur,
400 wav->segment.format, &cur);
401 if (res && stop_type != GST_SEEK_TYPE_NONE)
403 gst_pad_query_convert (wav->srcpad, format, stop,
404 wav->segment.format, &stop);
408 format = wav->segment.format;
411 GST_DEBUG_OBJECT (wav, "doing seek without event");
414 cur_type = GST_SEEK_TYPE_SET;
415 stop_type = GST_SEEK_TYPE_SET;
418 /* in push mode, we must delegate to upstream */
419 if (wav->streaming) {
420 gboolean res = FALSE;
422 /* if streaming not yet started; only prepare initial newsegment */
423 if (!event || wav->state != GST_WAVPARSE_DATA) {
424 if (wav->start_segment)
425 gst_event_unref (wav->start_segment);
426 wav->start_segment = gst_event_new_segment (&wav->segment);
429 /* convert seek positions to byte positions in data sections */
430 if (format == GST_FORMAT_TIME) {
431 /* should not fail */
432 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
434 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
437 /* mind sample boundary and header */
439 cur -= (cur % wav->bytes_per_sample);
440 cur += wav->datastart;
443 stop -= (stop % wav->bytes_per_sample);
444 stop += wav->datastart;
446 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
447 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
449 /* BYTE seek event */
450 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
452 res = gst_pad_push_event (wav->sinkpad, event);
458 flush = flags & GST_SEEK_FLAG_FLUSH;
460 /* now we need to make sure the streaming thread is stopped. We do this by
461 * either sending a FLUSH_START event downstream which will cause the
462 * streaming thread to stop with a WRONG_STATE.
463 * For a non-flushing seek we simply pause the task, which will happen as soon
464 * as it completes one iteration (and thus might block when the sink is
465 * blocking in preroll). */
467 GST_DEBUG_OBJECT (wav, "sending flush start");
468 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
470 gst_pad_pause_task (wav->sinkpad);
473 /* we should now be able to grab the streaming thread because we stopped it
474 * with the above flush/pause code */
475 GST_PAD_STREAM_LOCK (wav->sinkpad);
477 /* save current position */
478 last_stop = wav->segment.position;
480 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
482 /* copy segment, we need this because we still need the old
483 * segment when we close the current segment. */
484 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
486 /* configure the seek parameters in the seeksegment. We will then have the
487 * right values in the segment to perform the seek */
489 GST_DEBUG_OBJECT (wav, "configuring seek");
490 gst_segment_do_seek (&seeksegment, rate, format, flags,
491 cur_type, cur, stop_type, stop, &update);
494 /* figure out the last position we need to play. If it's configured (stop !=
495 * -1), use that, else we play until the total duration of the file */
496 if ((stop = seeksegment.stop) == -1)
497 stop = seeksegment.duration;
499 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
500 if ((cur_type != GST_SEEK_TYPE_NONE)) {
501 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
502 * we can just copy the last_stop. If not, we use the bps to convert TIME to
504 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
505 (gint64 *) & wav->offset))
506 wav->offset = seeksegment.position;
507 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
508 wav->offset -= (wav->offset % wav->bytes_per_sample);
509 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
510 wav->offset += wav->datastart;
511 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
513 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
517 if (stop_type != GST_SEEK_TYPE_NONE) {
518 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
519 wav->end_offset = stop;
520 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
521 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
522 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
523 wav->end_offset += wav->datastart;
524 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
526 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
530 /* make sure filesize is not exceeded due to rounding errors or so,
531 * same precaution as in _stream_headers */
532 bformat = GST_FORMAT_BYTES;
533 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
534 wav->end_offset = MIN (wav->end_offset, upstream_size);
536 /* this is the range of bytes we will use for playback */
537 wav->offset = MIN (wav->offset, wav->end_offset);
538 wav->dataleft = wav->end_offset - wav->offset;
540 GST_DEBUG_OBJECT (wav,
541 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
542 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
543 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
545 /* prepare for streaming again */
547 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
548 GST_DEBUG_OBJECT (wav, "sending flush stop");
549 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
552 /* now we did the seek and can activate the new segment values */
553 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
555 /* if we're doing a segment seek, post a SEGMENT_START message */
556 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
557 gst_element_post_message (GST_ELEMENT_CAST (wav),
558 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
559 wav->segment.format, wav->segment.position));
562 /* now create the newsegment */
563 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
564 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
566 /* store the newsegment event so it can be sent from the streaming thread. */
567 if (wav->start_segment)
568 gst_event_unref (wav->start_segment);
569 wav->start_segment = gst_event_new_segment (&wav->segment);
571 /* mark discont if we are going to stream from another position. */
572 if (last_stop != wav->segment.position) {
573 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
577 /* and start the streaming task again */
578 if (!wav->streaming) {
579 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
583 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
590 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
595 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
600 GST_DEBUG_OBJECT (wav,
601 "Could not determine byte position for desired time");
607 * gst_wavparse_peek_chunk_info:
608 * @wav Wavparse object
609 * @tag holder for tag
610 * @size holder for tag size
612 * Peek next chunk info (tag and size)
614 * Returns: %TRUE when the chunk info (header) is available
617 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
619 const guint8 *data = NULL;
621 if (gst_adapter_available (wav->adapter) < 8)
624 data = gst_adapter_map (wav->adapter, 8);
625 *tag = GST_READ_UINT32_LE (data);
626 *size = GST_READ_UINT32_LE (data + 4);
627 gst_adapter_unmap (wav->adapter);
629 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
630 GST_FOURCC_ARGS (*tag));
636 * gst_wavparse_peek_chunk:
637 * @wav Wavparse object
638 * @tag holder for tag
639 * @size holder for tag size
641 * Peek enough data for one full chunk
643 * Returns: %TRUE when the full chunk is available
646 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
648 guint32 peek_size = 0;
651 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
654 /* size 0 -> empty data buffer would surprise most callers,
655 * large size -> do not bother trying to squeeze that into adapter,
656 * so we throw poor man's exception, which can be caught if caller really
657 * wants to handle 0 size chunk */
658 if (!(*size) || (*size) >= (1 << 30)) {
659 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
660 *size, GST_FOURCC_ARGS (*tag));
661 /* chain should give up */
662 wav->abort_buffering = TRUE;
665 peek_size = (*size + 1) & ~1;
666 available = gst_adapter_available (wav->adapter);
668 if (available >= (8 + peek_size)) {
671 GST_LOG ("but only %u bytes available now", available);
677 * gst_wavparse_calculate_duration:
678 * @wav: wavparse object
680 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
683 * Returns: %TRUE if duration is available.
686 gst_wavparse_calculate_duration (GstWavParse * wav)
688 if (wav->duration > 0)
692 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
694 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
696 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
697 GST_TIME_ARGS (wav->duration));
699 } else if (wav->fact) {
701 gst_util_uint64_scale_ceil (GST_SECOND, wav->fact, wav->rate);
702 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
703 GST_TIME_ARGS (wav->duration));
710 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
715 if (wav->streaming) {
716 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
719 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
720 GST_FOURCC_ARGS (tag));
721 flush = 8 + ((size + 1) & ~1);
722 wav->offset += flush;
723 if (wav->streaming) {
724 gst_adapter_flush (wav->adapter, flush);
726 gst_buffer_unref (buf);
733 * gst_wavparse_cue_chunk:
734 * @wav GstWavParse object
735 * @data holder for data
736 * @size holder for data size
738 * Parse cue chunk from @data to wav->cues.
740 * Returns: %TRUE when cue chunk is available
743 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
750 GST_WARNING_OBJECT (wav, "found another cue's");
754 ncues = GST_READ_UINT32_LE (data);
756 if (size < 4 + ncues * 24) {
757 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
763 for (i = 0; i < ncues; i++) {
764 cue = g_new0 (GstWavParseCue, 1);
765 cue->id = GST_READ_UINT32_LE (data);
766 cue->position = GST_READ_UINT32_LE (data + 4);
767 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
768 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
769 cue->block_start = GST_READ_UINT32_LE (data + 16);
770 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
771 cues = g_list_append (cues, cue);
781 * gst_wavparse_labl_chunk:
782 * @wav GstWavParse object
783 * @data holder for data
784 * @size holder for data size
786 * Parse labl from @data to wav->labls.
788 * Returns: %TRUE when labl chunk is available
791 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
793 GstWavParseLabl *labl;
798 labl = g_new0 (GstWavParseLabl, 1);
802 labl->cue_point_id = GST_READ_UINT32_LE (data);
803 labl->text = g_memdup (data + 4, size - 4);
805 wav->labls = g_list_append (wav->labls, labl);
811 * gst_wavparse_note_chunk:
812 * @wav GstWavParse object
813 * @data holder for data
814 * @size holder for data size
816 * Parse note from @data to wav->notes.
818 * Returns: %TRUE when note chunk is available
821 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
823 GstWavParseNote *note;
828 note = g_new0 (GstWavParseNote, 1);
832 note->cue_point_id = GST_READ_UINT32_LE (data);
833 note->text = g_memdup (data + 4, size - 4);
835 wav->notes = g_list_append (wav->notes, note);
841 * gst_wavparse_smpl_chunk:
842 * @wav GstWavParse object
843 * @data holder for data
844 * @size holder for data size
846 * Parse smpl chunk from @data.
848 * Returns: %TRUE when cue chunk is available
851 gst_wavparse_smpl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
856 manufacturer_id = GST_READ_UINT32_LE (data);
857 product_id = GST_READ_UINT32_LE (data + 4);
858 sample_period = GST_READ_UINT32_LE (data + 8);
860 note_number = GST_READ_UINT32_LE (data + 12);
862 pitch_fraction = GST_READ_UINT32_LE (data + 16);
863 SMPTE_format = GST_READ_UINT32_LE (data + 20);
864 SMPTE_offset = GST_READ_UINT32_LE (data + 24);
865 num_sample_loops = GST_READ_UINT32_LE (data + 28);
866 List of Sample Loops, 24 bytes each
870 wav->tags = gst_tag_list_new_empty ();
871 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
872 GST_TAG_MIDI_BASE_NOTE, (guint) note_number, NULL);
877 * gst_wavparse_adtl_chunk:
878 * @wav GstWavParse object
879 * @data holder for data
880 * @size holder for data size
882 * Parse adtl from @data.
884 * Returns: %TRUE when adtl chunk is available
887 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
889 guint32 ltag, lsize, offset = 0;
892 ltag = GST_READ_UINT32_LE (data + offset);
893 lsize = GST_READ_UINT32_LE (data + offset + 4);
895 if (lsize + 8 > size) {
896 GST_WARNING_OBJECT (wav, "Invalid adtl size: %u + 8 > %u", lsize, size);
901 case GST_RIFF_TAG_labl:
902 gst_wavparse_labl_chunk (wav, data + offset, size);
904 case GST_RIFF_TAG_note:
905 gst_wavparse_note_chunk (wav, data + offset, size);
908 GST_WARNING_OBJECT (wav, "Unknowm adtl %" GST_FOURCC_FORMAT,
909 GST_FOURCC_ARGS (ltag));
910 GST_MEMDUMP_OBJECT (wav, "Unknowm adtl", &data[offset], lsize);
913 offset += 8 + GST_ROUND_UP_2 (lsize);
914 size -= 8 + GST_ROUND_UP_2 (lsize);
921 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
923 GstTagList *tags = NULL;
924 GstTocEntry *entry = NULL;
926 entry = gst_toc_find_entry (toc, id);
928 tags = gst_toc_entry_get_tags (entry);
930 tags = gst_tag_list_new_empty ();
931 gst_toc_entry_set_tags (entry, tags);
939 * gst_wavparse_create_toc:
940 * @wav GstWavParse object
942 * Create TOC from wav->cues and wav->labls.
945 gst_wavparse_create_toc (GstWavParse * wav)
951 GstWavParseLabl *labl;
952 GstWavParseNote *note;
955 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
957 GST_OBJECT_LOCK (wav);
959 GST_OBJECT_UNLOCK (wav);
960 GST_WARNING_OBJECT (wav, "found another TOC");
965 GST_OBJECT_UNLOCK (wav);
969 /* FIXME: send CURRENT scope toc too */
970 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
972 /* add cue edition */
973 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
974 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
975 gst_toc_append_entry (toc, entry);
977 /* add tracks in cue edition */
981 prev_subentry = cur_subentry;
982 /* previous track stop time = current track start time */
983 if (prev_subentry != NULL) {
984 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
985 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
986 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
988 id = g_strdup_printf ("%08x", cue->id);
989 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
991 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
992 stop = wav->duration;
993 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
994 gst_toc_entry_append_sub_entry (entry, cur_subentry);
995 list = g_list_next (list);
998 /* add tags in tracks */
1002 id = g_strdup_printf ("%08x", labl->cue_point_id);
1003 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1006 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1009 list = g_list_next (list);
1014 id = g_strdup_printf ("%08x", note->cue_point_id);
1015 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1018 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1021 list = g_list_next (list);
1024 /* send data as TOC */
1027 /* send TOC event */
1029 GST_OBJECT_UNLOCK (wav);
1030 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1036 #define MAX_BUFFER_SIZE 4096
1039 parse_ds64 (GstWavParse * wav, GstBuffer * buf)
1042 guint32 dataSizeLow, dataSizeHigh;
1043 guint32 sampleCountLow, sampleCountHigh;
1045 gst_buffer_map (buf, &map, GST_MAP_READ);
1046 dataSizeLow = GST_READ_UINT32_LE (map.data + 2 * 4);
1047 dataSizeHigh = GST_READ_UINT32_LE (map.data + 3 * 4);
1048 sampleCountLow = GST_READ_UINT32_LE (map.data + 4 * 4);
1049 sampleCountHigh = GST_READ_UINT32_LE (map.data + 5 * 4);
1050 gst_buffer_unmap (buf, &map);
1051 if (dataSizeHigh != 0xFFFFFFFF && dataSizeLow != 0xFFFFFFFF) {
1052 wav->datasize = ((guint64) dataSizeHigh << 32) | dataSizeLow;
1054 if (sampleCountHigh != 0xFFFFFFFF && sampleCountLow != 0xFFFFFFFF) {
1055 wav->fact = ((guint64) sampleCountHigh << 32) | sampleCountLow;
1058 GST_DEBUG_OBJECT (wav, "Got 'ds64' TAG, datasize : %" G_GINT64_FORMAT
1059 " fact: %" G_GINT64_FORMAT, wav->datasize, wav->fact);
1063 static GstFlowReturn
1064 gst_wavparse_stream_headers (GstWavParse * wav)
1066 GstFlowReturn res = GST_FLOW_OK;
1067 GstBuffer *buf = NULL;
1068 gst_riff_strf_auds *header = NULL;
1070 gboolean gotdata = FALSE;
1071 GstCaps *caps = NULL;
1072 gchar *codec_name = NULL;
1074 gint64 upstream_size = 0;
1077 /* search for "_fmt" chunk, which should be first */
1078 while (!wav->got_fmt) {
1081 /* The header starts with a 'fmt ' tag */
1082 if (wav->streaming) {
1083 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1086 gst_adapter_flush (wav->adapter, 8);
1090 buf = gst_adapter_take_buffer (wav->adapter, size);
1092 gst_adapter_flush (wav->adapter, 1);
1093 wav->offset += GST_ROUND_UP_2 (size);
1095 buf = gst_buffer_new ();
1098 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1099 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1103 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1104 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1105 tag == GST_RIFF_TAG_LIST || tag == GST_RIFF_TAG_ID32 ||
1106 tag == GST_RIFF_TAG_id3 || tag == GST_RIFF_TAG_IDVX ||
1107 tag == GST_BWF_TAG_iXML || tag == GST_BWF_TAG_qlty ||
1108 tag == GST_BWF_TAG_mext || tag == GST_BWF_TAG_levl ||
1109 tag == GST_BWF_TAG_link || tag == GST_BWF_TAG_axml ||
1110 tag == GST_RIFF_TAG_Fake) {
1111 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1112 GST_FOURCC_ARGS (tag));
1113 gst_buffer_unref (buf);
1118 if (tag == GST_RS64_TAG_DS64) {
1119 if (!parse_ds64 (wav, buf))
1125 if (tag != GST_RIFF_TAG_fmt)
1128 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1130 goto parse_header_error;
1132 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1134 /* do sanity checks of header fields */
1135 if (header->channels == 0)
1137 if (header->rate == 0)
1140 GST_DEBUG_OBJECT (wav, "creating the caps");
1142 /* Note: gst_riff_create_audio_caps might need to fix values in
1143 * the header header depending on the format, so call it first */
1144 /* FIXME: Need to handle the channel reorder map */
1145 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1146 NULL, &codec_name, NULL);
1149 gst_buffer_unref (extra);
1152 goto unknown_format;
1154 /* If we got raw audio from upstream, we remove the codec_data field,
1155 * which may have been added if the wav header included an extended
1156 * chunk. We want to keep it for non raw audio.
1158 s = gst_caps_get_structure (caps, 0);
1159 if (s && gst_structure_has_name (s, "audio/x-raw")) {
1160 gst_structure_remove_field (s, "codec_data");
1163 /* do more sanity checks of header fields
1164 * (these can be sanitized by gst_riff_create_audio_caps()
1166 wav->format = header->format;
1167 wav->rate = header->rate;
1168 wav->channels = header->channels;
1169 wav->blockalign = header->blockalign;
1170 wav->depth = header->bits_per_sample;
1171 wav->av_bps = header->av_bps;
1177 /* do format specific handling */
1178 switch (wav->format) {
1179 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1180 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1182 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1183 * bitrate inside the mpeg stream */
1184 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1188 case GST_RIFF_WAVE_FORMAT_PCM:
1189 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1190 goto invalid_blockalign;
1193 if (wav->av_bps > wav->blockalign * wav->rate)
1195 /* use the configured bps */
1196 wav->bps = wav->av_bps;
1200 wav->width = (wav->blockalign * 8) / wav->channels;
1201 wav->bytes_per_sample = wav->channels * wav->width / 8;
1203 if (wav->bytes_per_sample <= 0)
1204 goto no_bytes_per_sample;
1206 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1207 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1208 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1209 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1210 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1211 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1212 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1214 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1215 * formats). This will make the element output a BYTE format segment and
1216 * will not timestamp the outgoing buffers.
1218 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1220 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1222 /* create pad later so we can sniff the first few bytes
1223 * of the real data and correct our caps if necessary */
1224 gst_caps_replace (&wav->caps, caps);
1225 gst_caps_replace (&caps, NULL);
1227 wav->got_fmt = TRUE;
1230 wav->tags = gst_tag_list_new_empty ();
1232 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1233 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1235 g_free (codec_name);
1241 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1242 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1244 /* loop headers until we get data */
1246 if (wav->streaming) {
1247 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1254 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1255 &buf)) != GST_FLOW_OK)
1256 goto header_read_error;
1257 gst_buffer_map (buf, &map, GST_MAP_READ);
1258 tag = GST_READ_UINT32_LE (map.data);
1259 size = GST_READ_UINT32_LE (map.data + 4);
1260 gst_buffer_unmap (buf, &map);
1263 GST_INFO_OBJECT (wav,
1264 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1265 GST_FOURCC_ARGS (tag), wav->offset);
1267 /* wav is a st00pid format, we don't know for sure where data starts.
1268 * So we have to go bit by bit until we find the 'data' header
1271 case GST_RIFF_TAG_data:{
1274 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1276 if (wav->ignore_length) {
1277 GST_DEBUG_OBJECT (wav, "Ignoring length");
1280 if (wav->streaming) {
1281 gst_adapter_flush (wav->adapter, 8);
1284 gst_buffer_unref (buf);
1287 wav->datastart = wav->offset;
1288 /* use size from ds64 chunk if available */
1289 if (size64 == -1 && wav->datasize > 0) {
1290 GST_DEBUG_OBJECT (wav, "Using ds64 datasize");
1291 size64 = wav->datasize;
1293 /* If size is zero, then the data chunk probably actually extends to
1294 the end of the file */
1295 if (size64 == 0 && upstream_size) {
1296 size64 = upstream_size - wav->datastart;
1298 /* Or the file might be truncated */
1299 else if (upstream_size) {
1300 size64 = MIN (size64, (upstream_size - wav->datastart));
1302 wav->datasize = size64;
1303 wav->dataleft = size64;
1304 wav->end_offset = size64 + wav->datastart;
1305 if (!wav->streaming) {
1306 /* We will continue parsing tags 'till end */
1307 wav->offset += size64;
1309 GST_DEBUG_OBJECT (wav, "datasize = %" G_GUINT64_FORMAT, size64);
1312 case GST_RIFF_TAG_fact:{
1313 if (wav->fact == 0 &&
1314 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1315 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1316 const guint data_size = 4;
1318 GST_INFO_OBJECT (wav, "Have fact chunk");
1319 if (size < data_size) {
1320 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1321 /* need more data */
1324 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1328 /* number of samples (for compressed formats) */
1329 if (wav->streaming) {
1330 const guint8 *data = NULL;
1332 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1335 gst_adapter_flush (wav->adapter, 8);
1336 data = gst_adapter_map (wav->adapter, data_size);
1337 wav->fact = GST_READ_UINT32_LE (data);
1338 gst_adapter_unmap (wav->adapter);
1339 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1341 gst_buffer_unref (buf);
1344 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1345 data_size, &buf)) != GST_FLOW_OK)
1346 goto header_read_error;
1347 gst_buffer_extract (buf, 0, &wav->fact, 4);
1348 wav->fact = GUINT32_FROM_LE (wav->fact);
1349 gst_buffer_unref (buf);
1351 GST_DEBUG_OBJECT (wav, "have fact %" G_GUINT64_FORMAT, wav->fact);
1352 wav->offset += 8 + GST_ROUND_UP_2 (size);
1355 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1356 /* need more data */
1362 case GST_RIFF_TAG_acid:{
1363 const gst_riff_acid *acid = NULL;
1364 const guint data_size = sizeof (gst_riff_acid);
1367 GST_INFO_OBJECT (wav, "Have acid chunk");
1368 if (size < data_size) {
1369 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1370 /* need more data */
1373 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1377 if (wav->streaming) {
1378 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1381 gst_adapter_flush (wav->adapter, 8);
1382 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1384 tempo = acid->tempo;
1385 gst_adapter_unmap (wav->adapter);
1388 gst_buffer_unref (buf);
1391 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1392 size, &buf)) != GST_FLOW_OK)
1393 goto header_read_error;
1394 gst_buffer_map (buf, &map, GST_MAP_READ);
1395 acid = (const gst_riff_acid *) map.data;
1396 tempo = acid->tempo;
1397 gst_buffer_unmap (buf, &map);
1399 /* send data as tags */
1401 wav->tags = gst_tag_list_new_empty ();
1402 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1403 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1405 size = GST_ROUND_UP_2 (size);
1406 if (wav->streaming) {
1407 gst_adapter_flush (wav->adapter, size);
1409 gst_buffer_unref (buf);
1411 wav->offset += 8 + size;
1414 /* FIXME: all list tags after data are ignored in streaming mode */
1415 case GST_RIFF_TAG_LIST:{
1418 if (wav->streaming) {
1419 const guint8 *data = NULL;
1421 if (gst_adapter_available (wav->adapter) < 12) {
1424 data = gst_adapter_map (wav->adapter, 12);
1425 ltag = GST_READ_UINT32_LE (data + 8);
1426 gst_adapter_unmap (wav->adapter);
1428 gst_buffer_unref (buf);
1431 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1432 &buf)) != GST_FLOW_OK)
1433 goto header_read_error;
1434 gst_buffer_extract (buf, 8, <ag, 4);
1435 ltag = GUINT32_FROM_LE (ltag);
1438 case GST_RIFF_LIST_INFO:{
1439 const gint data_size = size - 4;
1442 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1443 if (wav->streaming) {
1444 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1447 gst_adapter_flush (wav->adapter, 12);
1449 if (data_size > 0) {
1450 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1452 gst_adapter_flush (wav->adapter, 1);
1456 gst_buffer_unref (buf);
1458 if (data_size > 0) {
1460 gst_pad_pull_range (wav->sinkpad, wav->offset,
1461 data_size, &buf)) != GST_FLOW_OK)
1462 goto header_read_error;
1465 if (data_size > 0) {
1467 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1469 GstTagList *old = wav->tags;
1471 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1473 gst_tag_list_unref (old);
1474 gst_tag_list_unref (new);
1476 gst_buffer_unref (buf);
1477 wav->offset += GST_ROUND_UP_2 (data_size);
1481 case GST_RIFF_LIST_adtl:{
1482 const gint data_size = size - 4;
1484 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1485 if (wav->streaming) {
1486 const guint8 *data = NULL;
1488 gst_adapter_flush (wav->adapter, 12);
1490 data = gst_adapter_map (wav->adapter, data_size);
1491 gst_wavparse_adtl_chunk (wav, data, data_size);
1492 gst_adapter_unmap (wav->adapter);
1496 gst_buffer_unref (buf);
1500 gst_pad_pull_range (wav->sinkpad, wav->offset,
1501 data_size, &buf)) != GST_FLOW_OK)
1502 goto header_read_error;
1503 gst_buffer_map (buf, &map, GST_MAP_READ);
1504 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1506 gst_buffer_unmap (buf, &map);
1508 wav->offset += GST_ROUND_UP_2 (data_size);
1512 GST_WARNING_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1513 GST_FOURCC_ARGS (ltag));
1514 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1515 /* need more data */
1521 case GST_RIFF_TAG_cue:{
1522 const guint data_size = size;
1524 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1525 if (wav->streaming) {
1526 const guint8 *data = NULL;
1528 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1531 gst_adapter_flush (wav->adapter, 8);
1533 data = gst_adapter_map (wav->adapter, data_size);
1534 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1535 goto header_read_error;
1537 gst_adapter_unmap (wav->adapter);
1542 gst_buffer_unref (buf);
1545 gst_pad_pull_range (wav->sinkpad, wav->offset,
1546 data_size, &buf)) != GST_FLOW_OK)
1547 goto header_read_error;
1548 gst_buffer_map (buf, &map, GST_MAP_READ);
1549 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1551 goto header_read_error;
1553 gst_buffer_unmap (buf, &map);
1555 size = GST_ROUND_UP_2 (size);
1556 if (wav->streaming) {
1557 gst_adapter_flush (wav->adapter, size);
1559 gst_buffer_unref (buf);
1561 size = GST_ROUND_UP_2 (size);
1562 wav->offset += size;
1565 case GST_RIFF_TAG_smpl:{
1566 const gint data_size = size;
1568 GST_DEBUG_OBJECT (wav, "Have 'smpl' TAG, size : %u", data_size);
1569 if (wav->streaming) {
1570 const guint8 *data = NULL;
1572 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1575 gst_adapter_flush (wav->adapter, 8);
1577 data = gst_adapter_map (wav->adapter, data_size);
1578 if (!gst_wavparse_smpl_chunk (wav, data, data_size)) {
1579 goto header_read_error;
1581 gst_adapter_unmap (wav->adapter);
1586 gst_buffer_unref (buf);
1589 gst_pad_pull_range (wav->sinkpad, wav->offset,
1590 data_size, &buf)) != GST_FLOW_OK)
1591 goto header_read_error;
1592 gst_buffer_map (buf, &map, GST_MAP_READ);
1593 if (!gst_wavparse_smpl_chunk (wav, (const guint8 *) map.data,
1595 goto header_read_error;
1597 gst_buffer_unmap (buf, &map);
1599 size = GST_ROUND_UP_2 (size);
1600 if (wav->streaming) {
1601 gst_adapter_flush (wav->adapter, size);
1603 gst_buffer_unref (buf);
1605 size = GST_ROUND_UP_2 (size);
1606 wav->offset += size;
1610 GST_WARNING_OBJECT (wav, "Ignoring chunk %" GST_FOURCC_FORMAT,
1611 GST_FOURCC_ARGS (tag));
1612 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1613 /* need more data */
1618 if (upstream_size && (wav->offset >= upstream_size)) {
1619 /* Now we are gone through the whole file */
1624 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1626 if (wav->bps <= 0 && wav->fact) {
1628 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1630 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1631 (guint64) wav->fact);
1632 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1637 if (gst_wavparse_calculate_duration (wav)) {
1638 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1639 if (!wav->ignore_length)
1640 wav->segment.duration = wav->duration;
1642 gst_wavparse_create_toc (wav);
1644 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1645 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1646 if (!wav->ignore_length)
1647 wav->segment.duration = wav->datasize;
1650 /* now we have all the info to perform a pending seek if any, if no
1651 * event, this will still do the right thing and it will also send
1652 * the right newsegment event downstream. */
1653 gst_wavparse_perform_seek (wav, wav->seek_event);
1654 /* remove pending event */
1655 event_p = &wav->seek_event;
1656 gst_event_replace (event_p, NULL);
1658 /* we just started, we are discont */
1659 wav->discont = TRUE;
1661 wav->state = GST_WAVPARSE_DATA;
1663 /* determine reasonable max buffer size,
1664 * that is, buffers not too small either size or time wise
1665 * so we do not end up with too many of them */
1667 if (gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size))
1668 wav->max_buf_size = upstream_size;
1670 wav->max_buf_size = 0;
1671 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1672 if (wav->blockalign > 0)
1673 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1675 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1683 g_free (codec_name);
1687 gst_caps_unref (caps);
1692 res = GST_FLOW_ERROR;
1697 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1698 ("Invalid WAV header (no fmt at start): %"
1699 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1704 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1705 ("Couldn't parse audio header"));
1710 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1711 ("Stream claims to contain no channels - invalid data"));
1716 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1717 ("Stream with sample_rate == 0 - invalid data"));
1722 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1723 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1724 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1729 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1730 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1731 wav->av_bps, wav->blockalign * wav->rate));
1734 no_bytes_per_sample:
1736 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1737 ("Could not caluclate bytes per sample - invalid data"));
1742 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1743 ("No caps found for format 0x%x, %u channels, %u Hz",
1744 wav->format, wav->channels, wav->rate));
1749 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1750 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1756 * Read WAV file tag when streaming
1758 static GstFlowReturn
1759 gst_wavparse_parse_stream_init (GstWavParse * wav)
1761 if (gst_adapter_available (wav->adapter) >= 12) {
1764 /* _take flushes the data */
1765 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1767 GST_DEBUG ("Parsing wav header");
1768 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1769 return GST_FLOW_ERROR;
1772 /* Go to next state */
1773 wav->state = GST_WAVPARSE_HEADER;
1778 /* handle an event sent directly to the element.
1780 * This event can be sent either in the READY state or the
1781 * >READY state. The only event of interest really is the seek
1784 * In the READY state we can only store the event and try to
1785 * respect it when going to PAUSED. We assume we are in the
1786 * READY state when our parsing state != GST_WAVPARSE_DATA.
1788 * When we are steaming, we can simply perform the seek right
1792 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1794 GstWavParse *wav = GST_WAVPARSE (element);
1795 gboolean res = FALSE;
1798 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1800 switch (GST_EVENT_TYPE (event)) {
1801 case GST_EVENT_SEEK:
1802 if (wav->state == GST_WAVPARSE_DATA) {
1803 /* we can handle the seek directly when streaming data */
1804 res = gst_wavparse_perform_seek (wav, event);
1806 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1808 event_p = &wav->seek_event;
1809 gst_event_replace (event_p, event);
1811 /* we always return true */
1818 gst_event_unref (event);
1823 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1827 s = gst_caps_get_structure (caps, 0);
1828 if (!gst_structure_has_name (s, "audio/x-dts"))
1830 if (prob >= GST_TYPE_FIND_LIKELY)
1832 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1833 if (prob < GST_TYPE_FIND_POSSIBLE)
1835 /* .. in which case we want at least a valid-looking rate and channels */
1836 if (!gst_structure_has_field (s, "channels"))
1838 /* and for extra assurance we could also check the rate from the DTS frame
1839 * against the one in the wav header, but for now let's not do that */
1840 return gst_structure_has_field (s, "rate");
1844 gst_wavparse_get_upstream_tags (GstWavParse * wav, GstTagScope scope)
1846 GstTagList *tags = NULL;
1851 while ((ev = gst_pad_get_sticky_event (wav->sinkpad, GST_EVENT_TAG, i++))) {
1852 gst_event_parse_tag (ev, &tags);
1853 if (tags != NULL && gst_tag_list_get_scope (tags) == scope) {
1854 tags = gst_tag_list_copy (tags);
1855 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1856 gst_event_unref (ev);
1860 gst_event_unref (ev);
1866 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1869 GstTagList *tags, *utags;
1871 GST_DEBUG_OBJECT (wav, "adding src pad");
1873 g_assert (wav->caps != NULL);
1875 s = gst_caps_get_structure (wav->caps, 0);
1876 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1877 GstTypeFindProbability prob;
1880 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1881 if (tf_caps != NULL) {
1882 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1883 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1884 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1885 gst_caps_unref (wav->caps);
1886 wav->caps = tf_caps;
1888 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1889 GST_TAG_AUDIO_CODEC, "dts", NULL);
1891 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1892 "marked as raw PCM audio, but ignoring for now", tf_caps);
1893 gst_caps_unref (tf_caps);
1898 gst_pad_set_caps (wav->srcpad, wav->caps);
1899 gst_caps_replace (&wav->caps, NULL);
1901 if (wav->start_segment) {
1902 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1903 gst_pad_push_event (wav->srcpad, wav->start_segment);
1904 wav->start_segment = NULL;
1907 /* upstream tags, e.g. from id3/ape tag before the wav file; assume for now
1908 * that there'll be only one scope/type of tag list from upstream, if any */
1909 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_GLOBAL);
1911 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_STREAM);
1913 /* if there's a tag upstream it's probably been added to override the
1914 * tags from inside the wav header, so keep upstream tags if in doubt */
1915 tags = gst_tag_list_merge (utags, wav->tags, GST_TAG_MERGE_KEEP);
1917 if (wav->tags != NULL) {
1918 gst_tag_list_unref (wav->tags);
1923 gst_tag_list_unref (utags);
1925 /* send tags downstream, if any */
1927 gst_pad_push_event (wav->srcpad, gst_event_new_tag (tags));
1930 static GstFlowReturn
1931 gst_wavparse_stream_data (GstWavParse * wav)
1933 GstBuffer *buf = NULL;
1934 GstFlowReturn res = GST_FLOW_OK;
1935 guint64 desired, obtained;
1936 GstClockTime timestamp, next_timestamp, duration;
1937 guint64 pos, nextpos;
1940 GST_LOG_OBJECT (wav,
1941 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1942 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1944 /* Get the next n bytes and output them */
1945 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1948 /* scale the amount of data by the segment rate so we get equal
1949 * amounts of data regardless of the playback rate */
1951 MIN (gst_guint64_to_gdouble (wav->dataleft),
1952 wav->max_buf_size * ABS (wav->segment.rate));
1954 if (desired >= wav->blockalign && wav->blockalign > 0)
1955 desired -= (desired % wav->blockalign);
1957 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1958 "from the sinkpad", desired);
1960 if (wav->streaming) {
1961 guint avail = gst_adapter_available (wav->adapter);
1964 /* flush some bytes if evil upstream sends segment that starts
1965 * before data or does is not send sample aligned segment */
1966 if (G_LIKELY (wav->offset >= wav->datastart)) {
1967 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1969 extra = wav->datastart - wav->offset;
1972 if (G_UNLIKELY (extra)) {
1973 extra = wav->bytes_per_sample - extra;
1974 if (extra <= avail) {
1975 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
1976 gst_adapter_flush (wav->adapter, extra);
1977 wav->offset += extra;
1978 wav->dataleft -= extra;
1979 goto iterate_adapter;
1981 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
1982 gst_adapter_clear (wav->adapter);
1983 wav->offset += avail;
1984 wav->dataleft -= avail;
1989 if (avail < desired) {
1990 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
1994 buf = gst_adapter_take_buffer (wav->adapter, desired);
1996 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1997 desired, &buf)) != GST_FLOW_OK)
2000 /* we may get a short buffer at the end of the file */
2001 if (gst_buffer_get_size (buf) < desired) {
2002 gsize size = gst_buffer_get_size (buf);
2004 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2005 if (size >= wav->blockalign) {
2006 if (wav->blockalign > 0) {
2007 buf = gst_buffer_make_writable (buf);
2008 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2011 gst_buffer_unref (buf);
2017 obtained = gst_buffer_get_size (buf);
2019 /* our positions in bytes */
2020 pos = wav->offset - wav->datastart;
2021 nextpos = pos + obtained;
2023 /* update offsets, does not overflow. */
2024 buf = gst_buffer_make_writable (buf);
2025 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2026 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2028 /* first chunk of data? create the source pad. We do this only here so
2029 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2030 if (G_UNLIKELY (wav->first)) {
2032 /* this will also push the segment events */
2033 gst_wavparse_add_src_pad (wav, buf);
2035 /* If we have a pending start segment, send it now. */
2036 if (G_UNLIKELY (wav->start_segment != NULL)) {
2037 gst_pad_push_event (wav->srcpad, wav->start_segment);
2038 wav->start_segment = NULL;
2043 /* and timestamps if we have a bitrate, be careful for overflows */
2045 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2047 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2048 duration = next_timestamp - timestamp;
2050 /* update current running segment position */
2051 if (G_LIKELY (next_timestamp >= wav->segment.start))
2052 wav->segment.position = next_timestamp;
2053 } else if (wav->fact) {
2055 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2056 /* and timestamps if we have a bitrate, be careful for overflows */
2057 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2058 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2059 duration = next_timestamp - timestamp;
2061 /* no bitrate, all we know is that the first sample has timestamp 0, all
2062 * other positions and durations have unknown timestamp. */
2066 timestamp = GST_CLOCK_TIME_NONE;
2067 duration = GST_CLOCK_TIME_NONE;
2068 /* update current running segment position with byte offset */
2069 if (G_LIKELY (nextpos >= wav->segment.start))
2070 wav->segment.position = nextpos;
2072 if ((pos > 0) && wav->vbr) {
2073 /* don't set timestamps for VBR files if it's not the first buffer */
2074 timestamp = GST_CLOCK_TIME_NONE;
2075 duration = GST_CLOCK_TIME_NONE;
2078 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2079 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2080 wav->discont = FALSE;
2083 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2084 GST_BUFFER_DURATION (buf) = duration;
2086 GST_LOG_OBJECT (wav,
2087 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2088 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2089 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2091 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2094 if (obtained < wav->dataleft) {
2095 wav->offset += obtained;
2096 wav->dataleft -= obtained;
2098 wav->offset += wav->dataleft;
2102 /* Iterate until need more data, so adapter size won't grow */
2103 if (wav->streaming) {
2104 GST_LOG_OBJECT (wav,
2105 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2107 goto iterate_adapter;
2114 GST_DEBUG_OBJECT (wav, "found EOS");
2115 return GST_FLOW_EOS;
2119 /* check if we got EOS */
2120 if (res == GST_FLOW_EOS)
2123 GST_WARNING_OBJECT (wav,
2124 "Error getting %" G_GINT64_FORMAT " bytes from the "
2125 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2130 GST_INFO_OBJECT (wav,
2131 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2132 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2133 gst_pad_is_linked (wav->srcpad));
2139 gst_wavparse_loop (GstPad * pad)
2142 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2146 GST_LOG_OBJECT (wav, "process data");
2148 switch (wav->state) {
2149 case GST_WAVPARSE_START:
2150 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2151 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2155 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2156 event = gst_event_new_stream_start (stream_id);
2157 gst_event_set_group_id (event, gst_util_group_id_next ());
2158 gst_pad_push_event (wav->srcpad, event);
2161 wav->state = GST_WAVPARSE_HEADER;
2164 case GST_WAVPARSE_HEADER:
2165 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2166 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2169 wav->state = GST_WAVPARSE_DATA;
2170 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2173 case GST_WAVPARSE_DATA:
2174 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2178 g_assert_not_reached ();
2185 const gchar *reason = gst_flow_get_name (ret);
2187 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2188 gst_pad_pause_task (pad);
2190 if (ret == GST_FLOW_EOS) {
2191 /* handle end-of-stream/segment */
2192 /* so align our position with the end of it, if there is one
2193 * this ensures a subsequent will arrive at correct base/acc time */
2194 if (wav->segment.format == GST_FORMAT_TIME) {
2195 if (wav->segment.rate > 0.0 &&
2196 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2197 wav->segment.position = wav->segment.stop;
2198 else if (wav->segment.rate < 0.0)
2199 wav->segment.position = wav->segment.start;
2201 if (wav->state == GST_WAVPARSE_START) {
2202 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2203 ("No valid input found before end of stream"));
2204 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2206 /* add pad before we perform EOS */
2207 if (G_UNLIKELY (wav->first)) {
2209 gst_wavparse_add_src_pad (wav, NULL);
2212 /* perform EOS logic */
2213 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2216 if ((stop = wav->segment.stop) == -1)
2217 stop = wav->segment.duration;
2219 gst_element_post_message (GST_ELEMENT_CAST (wav),
2220 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2221 wav->segment.format, stop));
2222 gst_pad_push_event (wav->srcpad,
2223 gst_event_new_segment_done (wav->segment.format, stop));
2225 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2228 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2229 /* for fatal errors we post an error message, post the error
2230 * first so the app knows about the error first. */
2231 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2232 (_("Internal data flow error.")),
2233 ("streaming task paused, reason %s (%d)", reason, ret));
2234 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2240 static GstFlowReturn
2241 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2244 GstWavParse *wav = GST_WAVPARSE (parent);
2246 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2247 gst_buffer_get_size (buf));
2249 gst_adapter_push (wav->adapter, buf);
2251 switch (wav->state) {
2252 case GST_WAVPARSE_START:
2253 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2254 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2257 if (wav->state != GST_WAVPARSE_HEADER)
2260 /* otherwise fall-through */
2261 case GST_WAVPARSE_HEADER:
2262 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2263 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2266 if (!wav->got_fmt || wav->datastart == 0)
2269 wav->state = GST_WAVPARSE_DATA;
2270 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2273 case GST_WAVPARSE_DATA:
2274 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2275 wav->discont = TRUE;
2276 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2280 g_return_val_if_reached (GST_FLOW_ERROR);
2283 if (G_UNLIKELY (wav->abort_buffering)) {
2284 wav->abort_buffering = FALSE;
2285 ret = GST_FLOW_ERROR;
2286 /* sort of demux/parse error */
2287 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2293 static GstFlowReturn
2294 gst_wavparse_flush_data (GstWavParse * wav)
2296 GstFlowReturn ret = GST_FLOW_OK;
2299 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2301 wav->end_offset = wav->offset + av;
2302 ret = gst_wavparse_stream_data (wav);
2309 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2311 GstWavParse *wav = GST_WAVPARSE (parent);
2312 gboolean ret = TRUE;
2314 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2316 switch (GST_EVENT_TYPE (event)) {
2317 case GST_EVENT_CAPS:
2319 /* discard, we'll come up with proper src caps */
2320 gst_event_unref (event);
2323 case GST_EVENT_SEGMENT:
2325 gint64 start, stop, offset = 0, end_offset = -1;
2328 /* some debug output */
2329 gst_event_copy_segment (event, &segment);
2330 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2333 if (wav->state != GST_WAVPARSE_DATA) {
2334 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2338 /* now we are either committed to TIME or BYTE format,
2339 * and we only expect a BYTE segment, e.g. following a seek */
2340 if (segment.format == GST_FORMAT_BYTES) {
2341 /* handle (un)signed issues */
2342 start = segment.start;
2343 stop = segment.stop;
2346 start -= wav->datastart;
2347 start = MAX (start, 0);
2351 stop -= wav->datastart;
2352 stop = MAX (stop, 0);
2354 if (wav->segment.format == GST_FORMAT_TIME) {
2355 guint64 bps = wav->bps;
2357 /* operating in format TIME, so we can convert */
2358 if (!bps && wav->fact)
2360 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2364 gst_util_uint64_scale_ceil (start, GST_SECOND,
2365 (guint64) wav->bps);
2368 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2369 (guint64) wav->bps);
2373 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2377 segment.start = start;
2378 segment.stop = stop;
2380 /* accept upstream's notion of segment and distribute along */
2381 segment.format = wav->segment.format;
2382 segment.time = segment.position = segment.start;
2383 segment.duration = wav->segment.duration;
2384 segment.base = gst_segment_to_running_time (&wav->segment,
2385 GST_FORMAT_TIME, wav->segment.position);
2387 gst_segment_copy_into (&segment, &wav->segment);
2389 /* also store the newsegment event for the streaming thread */
2390 if (wav->start_segment)
2391 gst_event_unref (wav->start_segment);
2392 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2393 wav->start_segment = gst_event_new_segment (&segment);
2395 /* stream leftover data in current segment */
2396 gst_wavparse_flush_data (wav);
2397 /* and set up streaming thread for next one */
2398 wav->offset = offset;
2399 wav->end_offset = end_offset;
2400 if (wav->end_offset > 0) {
2401 wav->dataleft = wav->end_offset - wav->offset;
2403 /* infinity; upstream will EOS when done */
2404 wav->dataleft = G_MAXUINT64;
2407 gst_event_unref (event);
2411 if (wav->state == GST_WAVPARSE_START) {
2412 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2413 ("No valid input found before end of stream"));
2415 /* add pad if needed so EOS is seen downstream */
2416 if (G_UNLIKELY (wav->first)) {
2418 gst_wavparse_add_src_pad (wav, NULL);
2420 /* stream leftover data in current segment */
2421 gst_wavparse_flush_data (wav);
2426 case GST_EVENT_FLUSH_STOP:
2430 gst_adapter_clear (wav->adapter);
2431 wav->discont = TRUE;
2432 dur = wav->segment.duration;
2433 gst_segment_init (&wav->segment, wav->segment.format);
2434 wav->segment.duration = dur;
2438 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2446 /* convert and query stuff */
2447 static const GstFormat *
2448 gst_wavparse_get_formats (GstPad * pad)
2450 static const GstFormat formats[] = {
2453 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2462 gst_wavparse_pad_convert (GstPad * pad,
2463 GstFormat src_format, gint64 src_value,
2464 GstFormat * dest_format, gint64 * dest_value)
2466 GstWavParse *wavparse;
2467 gboolean res = TRUE;
2469 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2471 if (*dest_format == src_format) {
2472 *dest_value = src_value;
2476 if ((wavparse->bps == 0) && !wavparse->fact)
2479 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2480 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2482 switch (src_format) {
2483 case GST_FORMAT_BYTES:
2484 switch (*dest_format) {
2485 case GST_FORMAT_DEFAULT:
2486 *dest_value = src_value / wavparse->bytes_per_sample;
2487 /* make sure we end up on a sample boundary */
2488 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2490 case GST_FORMAT_TIME:
2491 /* src_value + datastart = offset */
2492 GST_INFO_OBJECT (wavparse,
2493 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2495 if (wavparse->bps > 0)
2496 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2497 (guint64) wavparse->bps);
2498 else if (wavparse->fact) {
2499 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2500 wavparse->rate, wavparse->fact);
2503 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2514 case GST_FORMAT_DEFAULT:
2515 switch (*dest_format) {
2516 case GST_FORMAT_BYTES:
2517 *dest_value = src_value * wavparse->bytes_per_sample;
2519 case GST_FORMAT_TIME:
2520 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2521 (guint64) wavparse->rate);
2529 case GST_FORMAT_TIME:
2530 switch (*dest_format) {
2531 case GST_FORMAT_BYTES:
2532 if (wavparse->bps > 0)
2533 *dest_value = gst_util_uint64_scale (src_value,
2534 (guint64) wavparse->bps, GST_SECOND);
2536 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2537 wavparse->rate, wavparse->fact);
2539 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2541 /* make sure we end up on a sample boundary */
2542 *dest_value -= *dest_value % wavparse->blockalign;
2544 case GST_FORMAT_DEFAULT:
2545 *dest_value = gst_util_uint64_scale (src_value,
2546 (guint64) wavparse->rate, GST_SECOND);
2565 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2571 /* handle queries for location and length in requested format */
2573 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2575 gboolean res = TRUE;
2576 GstWavParse *wav = GST_WAVPARSE (parent);
2578 /* only if we know */
2579 if (wav->state != GST_WAVPARSE_DATA) {
2583 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2585 switch (GST_QUERY_TYPE (query)) {
2586 case GST_QUERY_POSITION:
2592 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2593 curb = wav->offset - wav->datastart;
2594 gst_query_parse_position (query, &format, NULL);
2595 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2598 case GST_FORMAT_BYTES:
2599 format = GST_FORMAT_BYTES;
2603 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2608 gst_query_set_position (query, format, cur);
2611 case GST_QUERY_DURATION:
2613 gint64 duration = 0;
2616 if (wav->ignore_length) {
2621 gst_query_parse_duration (query, &format, NULL);
2624 case GST_FORMAT_BYTES:{
2625 format = GST_FORMAT_BYTES;
2626 duration = wav->datasize;
2629 case GST_FORMAT_TIME:
2630 if ((res = gst_wavparse_calculate_duration (wav))) {
2631 duration = wav->duration;
2639 gst_query_set_duration (query, format, duration);
2642 case GST_QUERY_CONVERT:
2644 gint64 srcvalue, dstvalue;
2645 GstFormat srcformat, dstformat;
2647 gst_query_parse_convert (query, &srcformat, &srcvalue,
2648 &dstformat, &dstvalue);
2649 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2650 &dstformat, &dstvalue);
2652 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2655 case GST_QUERY_SEEKING:{
2657 gboolean seekable = FALSE;
2659 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2660 if (fmt == wav->segment.format) {
2661 if (wav->streaming) {
2664 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2665 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2666 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2667 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2669 gst_query_unref (q);
2671 GST_LOG_OBJECT (wav, "looping => seekable");
2675 } else if (fmt == GST_FORMAT_TIME) {
2679 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2684 res = gst_pad_query_default (pad, parent, query);
2691 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2693 GstWavParse *wavparse = GST_WAVPARSE (parent);
2694 gboolean res = FALSE;
2696 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2698 switch (GST_EVENT_TYPE (event)) {
2699 case GST_EVENT_SEEK:
2700 /* can only handle events when we are in the data state */
2701 if (wavparse->state == GST_WAVPARSE_DATA) {
2702 res = gst_wavparse_perform_seek (wavparse, event);
2704 gst_event_unref (event);
2707 case GST_EVENT_TOC_SELECT:
2710 GstTocEntry *entry = NULL;
2711 GstEvent *seek_event;
2714 if (!wavparse->toc) {
2715 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2718 gst_event_parse_toc_select (event, &uid);
2720 GST_OBJECT_LOCK (wavparse);
2721 entry = gst_toc_find_entry (wavparse->toc, uid);
2722 if (entry == NULL) {
2723 GST_OBJECT_UNLOCK (wavparse);
2724 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2728 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2729 GST_OBJECT_UNLOCK (wavparse);
2730 seek_event = gst_event_new_seek (1.0,
2732 GST_SEEK_FLAG_FLUSH,
2733 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2734 res = gst_wavparse_perform_seek (wavparse, seek_event);
2735 gst_event_unref (seek_event);
2739 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2743 gst_event_unref (event);
2748 res = gst_pad_push_event (wavparse->sinkpad, event);
2755 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2757 GstWavParse *wav = GST_WAVPARSE (parent);
2762 gst_adapter_clear (wav->adapter);
2763 g_object_unref (wav->adapter);
2764 wav->adapter = NULL;
2767 query = gst_query_new_scheduling ();
2769 if (!gst_pad_peer_query (sinkpad, query)) {
2770 gst_query_unref (query);
2774 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2775 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2776 gst_query_unref (query);
2781 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2782 wav->streaming = FALSE;
2783 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2787 GST_DEBUG_OBJECT (sinkpad, "activating push");
2788 wav->streaming = TRUE;
2789 wav->adapter = gst_adapter_new ();
2790 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2796 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2797 GstPadMode mode, gboolean active)
2802 case GST_PAD_MODE_PUSH:
2805 case GST_PAD_MODE_PULL:
2807 /* if we have a scheduler we can start the task */
2808 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2811 res = gst_pad_stop_task (sinkpad);
2821 static GstStateChangeReturn
2822 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2824 GstStateChangeReturn ret;
2825 GstWavParse *wav = GST_WAVPARSE (element);
2827 switch (transition) {
2828 case GST_STATE_CHANGE_NULL_TO_READY:
2830 case GST_STATE_CHANGE_READY_TO_PAUSED:
2831 gst_wavparse_reset (wav);
2833 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2839 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2841 switch (transition) {
2842 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2844 case GST_STATE_CHANGE_PAUSED_TO_READY:
2845 gst_wavparse_reset (wav);
2847 case GST_STATE_CHANGE_READY_TO_NULL:
2856 gst_wavparse_set_property (GObject * object, guint prop_id,
2857 const GValue * value, GParamSpec * pspec)
2861 g_return_if_fail (GST_IS_WAVPARSE (object));
2862 self = GST_WAVPARSE (object);
2865 case PROP_IGNORE_LENGTH:
2866 self->ignore_length = g_value_get_boolean (value);
2869 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2875 gst_wavparse_get_property (GObject * object, guint prop_id,
2876 GValue * value, GParamSpec * pspec)
2880 g_return_if_fail (GST_IS_WAVPARSE (object));
2881 self = GST_WAVPARSE (object);
2884 case PROP_IGNORE_LENGTH:
2885 g_value_set_boolean (value, self->ignore_length);
2888 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2893 plugin_init (GstPlugin * plugin)
2897 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2901 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2904 "Parse a .wav file into raw audio",
2905 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)