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, };
384 GST_DEBUG_OBJECT (wav, "doing seek with event");
386 gst_event_parse_seek (event, &rate, &format, &flags,
387 &cur_type, &cur, &stop_type, &stop);
388 seqnum = gst_event_get_seqnum (event);
390 /* no negative rates yet */
394 if (format != wav->segment.format) {
395 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
396 gst_format_get_name (format),
397 gst_format_get_name (wav->segment.format));
399 if (cur_type != GST_SEEK_TYPE_NONE)
401 gst_pad_query_convert (wav->srcpad, format, cur,
402 wav->segment.format, &cur);
403 if (res && stop_type != GST_SEEK_TYPE_NONE)
405 gst_pad_query_convert (wav->srcpad, format, stop,
406 wav->segment.format, &stop);
410 format = wav->segment.format;
413 GST_DEBUG_OBJECT (wav, "doing seek without event");
416 cur_type = GST_SEEK_TYPE_SET;
417 stop_type = GST_SEEK_TYPE_SET;
420 /* in push mode, we must delegate to upstream */
421 if (wav->streaming) {
422 gboolean res = FALSE;
424 /* if streaming not yet started; only prepare initial newsegment */
425 if (!event || wav->state != GST_WAVPARSE_DATA) {
426 if (wav->start_segment)
427 gst_event_unref (wav->start_segment);
428 wav->start_segment = gst_event_new_segment (&wav->segment);
431 /* convert seek positions to byte positions in data sections */
432 if (format == GST_FORMAT_TIME) {
433 /* should not fail */
434 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
436 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
439 /* mind sample boundary and header */
441 cur -= (cur % wav->bytes_per_sample);
442 cur += wav->datastart;
445 stop -= (stop % wav->bytes_per_sample);
446 stop += wav->datastart;
448 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
449 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
451 /* BYTE seek event */
452 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
454 gst_event_set_seqnum (event, seqnum);
455 res = gst_pad_push_event (wav->sinkpad, event);
461 flush = flags & GST_SEEK_FLAG_FLUSH;
463 /* now we need to make sure the streaming thread is stopped. We do this by
464 * either sending a FLUSH_START event downstream which will cause the
465 * streaming thread to stop with a WRONG_STATE.
466 * For a non-flushing seek we simply pause the task, which will happen as soon
467 * as it completes one iteration (and thus might block when the sink is
468 * blocking in preroll). */
471 GST_DEBUG_OBJECT (wav, "sending flush start");
473 fevent = gst_event_new_flush_start ();
474 gst_event_set_seqnum (fevent, seqnum);
475 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
476 gst_pad_push_event (wav->srcpad, fevent);
478 gst_pad_pause_task (wav->sinkpad);
481 /* we should now be able to grab the streaming thread because we stopped it
482 * with the above flush/pause code */
483 GST_PAD_STREAM_LOCK (wav->sinkpad);
485 /* save current position */
486 last_stop = wav->segment.position;
488 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
490 /* copy segment, we need this because we still need the old
491 * segment when we close the current segment. */
492 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
494 /* configure the seek parameters in the seeksegment. We will then have the
495 * right values in the segment to perform the seek */
497 GST_DEBUG_OBJECT (wav, "configuring seek");
498 gst_segment_do_seek (&seeksegment, rate, format, flags,
499 cur_type, cur, stop_type, stop, &update);
502 /* figure out the last position we need to play. If it's configured (stop !=
503 * -1), use that, else we play until the total duration of the file */
504 if ((stop = seeksegment.stop) == -1)
505 stop = seeksegment.duration;
507 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
508 if ((cur_type != GST_SEEK_TYPE_NONE)) {
509 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
510 * we can just copy the last_stop. If not, we use the bps to convert TIME to
512 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
513 (gint64 *) & wav->offset))
514 wav->offset = seeksegment.position;
515 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
516 wav->offset -= (wav->offset % wav->bytes_per_sample);
517 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
518 wav->offset += wav->datastart;
519 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
521 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
525 if (stop_type != GST_SEEK_TYPE_NONE) {
526 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
527 wav->end_offset = stop;
528 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
529 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
530 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
531 wav->end_offset += wav->datastart;
532 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
534 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
538 /* make sure filesize is not exceeded due to rounding errors or so,
539 * same precaution as in _stream_headers */
540 bformat = GST_FORMAT_BYTES;
541 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
542 wav->end_offset = MIN (wav->end_offset, upstream_size);
544 /* this is the range of bytes we will use for playback */
545 wav->offset = MIN (wav->offset, wav->end_offset);
546 wav->dataleft = wav->end_offset - wav->offset;
548 GST_DEBUG_OBJECT (wav,
549 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
550 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
551 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
553 /* prepare for streaming again */
557 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
558 GST_DEBUG_OBJECT (wav, "sending flush stop");
560 fevent = gst_event_new_flush_stop (TRUE);
561 gst_event_set_seqnum (fevent, seqnum);
562 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
563 gst_pad_push_event (wav->srcpad, fevent);
566 /* now we did the seek and can activate the new segment values */
567 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
569 /* if we're doing a segment seek, post a SEGMENT_START message */
570 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
571 gst_element_post_message (GST_ELEMENT_CAST (wav),
572 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
573 wav->segment.format, wav->segment.position));
576 /* now create the newsegment */
577 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
578 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
580 /* store the newsegment event so it can be sent from the streaming thread. */
581 if (wav->start_segment)
582 gst_event_unref (wav->start_segment);
583 wav->start_segment = gst_event_new_segment (&wav->segment);
584 gst_event_set_seqnum (wav->start_segment, seqnum);
586 /* mark discont if we are going to stream from another position. */
587 if (last_stop != wav->segment.position) {
588 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
592 /* and start the streaming task again */
593 if (!wav->streaming) {
594 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
598 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
605 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
610 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
615 GST_DEBUG_OBJECT (wav,
616 "Could not determine byte position for desired time");
622 * gst_wavparse_peek_chunk_info:
623 * @wav Wavparse object
624 * @tag holder for tag
625 * @size holder for tag size
627 * Peek next chunk info (tag and size)
629 * Returns: %TRUE when the chunk info (header) is available
632 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
634 const guint8 *data = NULL;
636 if (gst_adapter_available (wav->adapter) < 8)
639 data = gst_adapter_map (wav->adapter, 8);
640 *tag = GST_READ_UINT32_LE (data);
641 *size = GST_READ_UINT32_LE (data + 4);
642 gst_adapter_unmap (wav->adapter);
644 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
645 GST_FOURCC_ARGS (*tag));
651 * gst_wavparse_peek_chunk:
652 * @wav Wavparse object
653 * @tag holder for tag
654 * @size holder for tag size
656 * Peek enough data for one full chunk
658 * Returns: %TRUE when the full chunk is available
661 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
663 guint32 peek_size = 0;
666 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
669 /* size 0 -> empty data buffer would surprise most callers,
670 * large size -> do not bother trying to squeeze that into adapter,
671 * so we throw poor man's exception, which can be caught if caller really
672 * wants to handle 0 size chunk */
673 if (!(*size) || (*size) >= (1 << 30)) {
674 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
675 *size, GST_FOURCC_ARGS (*tag));
676 /* chain should give up */
677 wav->abort_buffering = TRUE;
680 peek_size = (*size + 1) & ~1;
681 available = gst_adapter_available (wav->adapter);
683 if (available >= (8 + peek_size)) {
686 GST_LOG ("but only %u bytes available now", available);
692 * gst_wavparse_calculate_duration:
693 * @wav: wavparse object
695 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
698 * Returns: %TRUE if duration is available.
701 gst_wavparse_calculate_duration (GstWavParse * wav)
703 if (wav->duration > 0)
707 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
709 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
711 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
712 GST_TIME_ARGS (wav->duration));
714 } else if (wav->fact) {
716 gst_util_uint64_scale_ceil (GST_SECOND, wav->fact, wav->rate);
717 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
718 GST_TIME_ARGS (wav->duration));
725 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
730 if (wav->streaming) {
731 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
734 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
735 GST_FOURCC_ARGS (tag));
736 flush = 8 + ((size + 1) & ~1);
737 wav->offset += flush;
738 if (wav->streaming) {
739 gst_adapter_flush (wav->adapter, flush);
741 gst_buffer_unref (buf);
748 * gst_wavparse_cue_chunk:
749 * @wav GstWavParse object
750 * @data holder for data
751 * @size holder for data size
753 * Parse cue chunk from @data to wav->cues.
755 * Returns: %TRUE when cue chunk is available
758 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
765 GST_WARNING_OBJECT (wav, "found another cue's");
769 ncues = GST_READ_UINT32_LE (data);
771 if (size < 4 + ncues * 24) {
772 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
778 for (i = 0; i < ncues; i++) {
779 cue = g_new0 (GstWavParseCue, 1);
780 cue->id = GST_READ_UINT32_LE (data);
781 cue->position = GST_READ_UINT32_LE (data + 4);
782 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
783 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
784 cue->block_start = GST_READ_UINT32_LE (data + 16);
785 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
786 cues = g_list_append (cues, cue);
796 * gst_wavparse_labl_chunk:
797 * @wav GstWavParse object
798 * @data holder for data
799 * @size holder for data size
801 * Parse labl from @data to wav->labls.
803 * Returns: %TRUE when labl chunk is available
806 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
808 GstWavParseLabl *labl;
813 labl = g_new0 (GstWavParseLabl, 1);
817 labl->cue_point_id = GST_READ_UINT32_LE (data);
818 labl->text = g_memdup (data + 4, size - 4);
820 wav->labls = g_list_append (wav->labls, labl);
826 * gst_wavparse_note_chunk:
827 * @wav GstWavParse object
828 * @data holder for data
829 * @size holder for data size
831 * Parse note from @data to wav->notes.
833 * Returns: %TRUE when note chunk is available
836 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
838 GstWavParseNote *note;
843 note = g_new0 (GstWavParseNote, 1);
847 note->cue_point_id = GST_READ_UINT32_LE (data);
848 note->text = g_memdup (data + 4, size - 4);
850 wav->notes = g_list_append (wav->notes, note);
856 * gst_wavparse_smpl_chunk:
857 * @wav GstWavParse object
858 * @data holder for data
859 * @size holder for data size
861 * Parse smpl chunk from @data.
863 * Returns: %TRUE when cue chunk is available
866 gst_wavparse_smpl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
871 manufacturer_id = GST_READ_UINT32_LE (data);
872 product_id = GST_READ_UINT32_LE (data + 4);
873 sample_period = GST_READ_UINT32_LE (data + 8);
875 note_number = GST_READ_UINT32_LE (data + 12);
877 pitch_fraction = GST_READ_UINT32_LE (data + 16);
878 SMPTE_format = GST_READ_UINT32_LE (data + 20);
879 SMPTE_offset = GST_READ_UINT32_LE (data + 24);
880 num_sample_loops = GST_READ_UINT32_LE (data + 28);
881 List of Sample Loops, 24 bytes each
885 wav->tags = gst_tag_list_new_empty ();
886 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
887 GST_TAG_MIDI_BASE_NOTE, (guint) note_number, NULL);
892 * gst_wavparse_adtl_chunk:
893 * @wav GstWavParse object
894 * @data holder for data
895 * @size holder for data size
897 * Parse adtl from @data.
899 * Returns: %TRUE when adtl chunk is available
902 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
904 guint32 ltag, lsize, offset = 0;
907 ltag = GST_READ_UINT32_LE (data + offset);
908 lsize = GST_READ_UINT32_LE (data + offset + 4);
910 if (lsize + 8 > size) {
911 GST_WARNING_OBJECT (wav, "Invalid adtl size: %u + 8 > %u", lsize, size);
916 case GST_RIFF_TAG_labl:
917 gst_wavparse_labl_chunk (wav, data + offset, size);
919 case GST_RIFF_TAG_note:
920 gst_wavparse_note_chunk (wav, data + offset, size);
923 GST_WARNING_OBJECT (wav, "Unknowm adtl %" GST_FOURCC_FORMAT,
924 GST_FOURCC_ARGS (ltag));
925 GST_MEMDUMP_OBJECT (wav, "Unknowm adtl", &data[offset], lsize);
928 offset += 8 + GST_ROUND_UP_2 (lsize);
929 size -= 8 + GST_ROUND_UP_2 (lsize);
936 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
938 GstTagList *tags = NULL;
939 GstTocEntry *entry = NULL;
941 entry = gst_toc_find_entry (toc, id);
943 tags = gst_toc_entry_get_tags (entry);
945 tags = gst_tag_list_new_empty ();
946 gst_toc_entry_set_tags (entry, tags);
954 * gst_wavparse_create_toc:
955 * @wav GstWavParse object
957 * Create TOC from wav->cues and wav->labls.
960 gst_wavparse_create_toc (GstWavParse * wav)
966 GstWavParseLabl *labl;
967 GstWavParseNote *note;
970 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
972 GST_OBJECT_LOCK (wav);
974 GST_OBJECT_UNLOCK (wav);
975 GST_WARNING_OBJECT (wav, "found another TOC");
980 GST_OBJECT_UNLOCK (wav);
984 /* FIXME: send CURRENT scope toc too */
985 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
987 /* add cue edition */
988 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
989 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
990 gst_toc_append_entry (toc, entry);
992 /* add tracks in cue edition */
996 prev_subentry = cur_subentry;
997 /* previous track stop time = current track start time */
998 if (prev_subentry != NULL) {
999 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
1000 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1001 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
1003 id = g_strdup_printf ("%08x", cue->id);
1004 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
1006 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1007 stop = wav->duration;
1008 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
1009 gst_toc_entry_append_sub_entry (entry, cur_subentry);
1010 list = g_list_next (list);
1013 /* add tags in tracks */
1017 id = g_strdup_printf ("%08x", labl->cue_point_id);
1018 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1021 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1024 list = g_list_next (list);
1029 id = g_strdup_printf ("%08x", note->cue_point_id);
1030 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1033 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1036 list = g_list_next (list);
1039 /* send data as TOC */
1042 /* send TOC event */
1044 GST_OBJECT_UNLOCK (wav);
1045 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1051 #define MAX_BUFFER_SIZE 4096
1054 parse_ds64 (GstWavParse * wav, GstBuffer * buf)
1057 guint32 dataSizeLow, dataSizeHigh;
1058 guint32 sampleCountLow, sampleCountHigh;
1060 gst_buffer_map (buf, &map, GST_MAP_READ);
1061 dataSizeLow = GST_READ_UINT32_LE (map.data + 2 * 4);
1062 dataSizeHigh = GST_READ_UINT32_LE (map.data + 3 * 4);
1063 sampleCountLow = GST_READ_UINT32_LE (map.data + 4 * 4);
1064 sampleCountHigh = GST_READ_UINT32_LE (map.data + 5 * 4);
1065 gst_buffer_unmap (buf, &map);
1066 if (dataSizeHigh != 0xFFFFFFFF && dataSizeLow != 0xFFFFFFFF) {
1067 wav->datasize = ((guint64) dataSizeHigh << 32) | dataSizeLow;
1069 if (sampleCountHigh != 0xFFFFFFFF && sampleCountLow != 0xFFFFFFFF) {
1070 wav->fact = ((guint64) sampleCountHigh << 32) | sampleCountLow;
1073 GST_DEBUG_OBJECT (wav, "Got 'ds64' TAG, datasize : %" G_GINT64_FORMAT
1074 " fact: %" G_GINT64_FORMAT, wav->datasize, wav->fact);
1078 static GstFlowReturn
1079 gst_wavparse_stream_headers (GstWavParse * wav)
1081 GstFlowReturn res = GST_FLOW_OK;
1082 GstBuffer *buf = NULL;
1083 gst_riff_strf_auds *header = NULL;
1085 gboolean gotdata = FALSE;
1086 GstCaps *caps = NULL;
1087 gchar *codec_name = NULL;
1089 gint64 upstream_size = 0;
1092 /* search for "_fmt" chunk, which should be first */
1093 while (!wav->got_fmt) {
1096 /* The header starts with a 'fmt ' tag */
1097 if (wav->streaming) {
1098 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1101 gst_adapter_flush (wav->adapter, 8);
1105 buf = gst_adapter_take_buffer (wav->adapter, size);
1107 gst_adapter_flush (wav->adapter, 1);
1108 wav->offset += GST_ROUND_UP_2 (size);
1110 buf = gst_buffer_new ();
1113 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1114 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1118 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1119 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1120 tag == GST_RIFF_TAG_LIST || tag == GST_RIFF_TAG_ID32 ||
1121 tag == GST_RIFF_TAG_id3 || tag == GST_RIFF_TAG_IDVX ||
1122 tag == GST_BWF_TAG_iXML || tag == GST_BWF_TAG_qlty ||
1123 tag == GST_BWF_TAG_mext || tag == GST_BWF_TAG_levl ||
1124 tag == GST_BWF_TAG_link || tag == GST_BWF_TAG_axml ||
1125 tag == GST_RIFF_TAG_Fake) {
1126 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1127 GST_FOURCC_ARGS (tag));
1128 gst_buffer_unref (buf);
1133 if (tag == GST_RS64_TAG_DS64) {
1134 if (!parse_ds64 (wav, buf))
1140 if (tag != GST_RIFF_TAG_fmt)
1143 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1145 goto parse_header_error;
1147 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1149 /* do sanity checks of header fields */
1150 if (header->channels == 0)
1152 if (header->rate == 0)
1155 GST_DEBUG_OBJECT (wav, "creating the caps");
1157 /* Note: gst_riff_create_audio_caps might need to fix values in
1158 * the header header depending on the format, so call it first */
1159 /* FIXME: Need to handle the channel reorder map */
1160 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1161 NULL, &codec_name, NULL);
1164 gst_buffer_unref (extra);
1167 goto unknown_format;
1169 /* If we got raw audio from upstream, we remove the codec_data field,
1170 * which may have been added if the wav header included an extended
1171 * chunk. We want to keep it for non raw audio.
1173 s = gst_caps_get_structure (caps, 0);
1174 if (s && gst_structure_has_name (s, "audio/x-raw")) {
1175 gst_structure_remove_field (s, "codec_data");
1178 /* do more sanity checks of header fields
1179 * (these can be sanitized by gst_riff_create_audio_caps()
1181 wav->format = header->format;
1182 wav->rate = header->rate;
1183 wav->channels = header->channels;
1184 wav->blockalign = header->blockalign;
1185 wav->depth = header->bits_per_sample;
1186 wav->av_bps = header->av_bps;
1192 /* do format specific handling */
1193 switch (wav->format) {
1194 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1195 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1197 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1198 * bitrate inside the mpeg stream */
1199 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1203 case GST_RIFF_WAVE_FORMAT_PCM:
1204 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1205 goto invalid_blockalign;
1208 if (wav->av_bps > wav->blockalign * wav->rate)
1210 /* use the configured bps */
1211 wav->bps = wav->av_bps;
1215 wav->width = (wav->blockalign * 8) / wav->channels;
1216 wav->bytes_per_sample = wav->channels * wav->width / 8;
1218 if (wav->bytes_per_sample <= 0)
1219 goto no_bytes_per_sample;
1221 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1222 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1223 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1224 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1225 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1226 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1227 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1229 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1230 * formats). This will make the element output a BYTE format segment and
1231 * will not timestamp the outgoing buffers.
1233 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1235 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1237 /* create pad later so we can sniff the first few bytes
1238 * of the real data and correct our caps if necessary */
1239 gst_caps_replace (&wav->caps, caps);
1240 gst_caps_replace (&caps, NULL);
1242 wav->got_fmt = TRUE;
1245 wav->tags = gst_tag_list_new_empty ();
1247 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1248 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1250 g_free (codec_name);
1256 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1257 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1259 /* loop headers until we get data */
1261 if (wav->streaming) {
1262 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1269 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1270 &buf)) != GST_FLOW_OK)
1271 goto header_read_error;
1272 gst_buffer_map (buf, &map, GST_MAP_READ);
1273 tag = GST_READ_UINT32_LE (map.data);
1274 size = GST_READ_UINT32_LE (map.data + 4);
1275 gst_buffer_unmap (buf, &map);
1278 GST_INFO_OBJECT (wav,
1279 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT ", size %"
1280 G_GUINT32_FORMAT, GST_FOURCC_ARGS (tag), wav->offset, size);
1282 /* Maximum valid size is INT_MAX */
1283 if (size & 0x80000000) {
1284 GST_WARNING_OBJECT (wav, "Invalid size, clipping to 0x7fffffff");
1288 /* Clip to upstream size if known */
1289 if (wav->datasize > 0 && size + wav->offset > wav->datasize) {
1290 GST_WARNING_OBJECT (wav, "Clipping chunk size to file size");
1291 size = wav->datasize - wav->offset;
1294 /* wav is a st00pid format, we don't know for sure where data starts.
1295 * So we have to go bit by bit until we find the 'data' header
1298 case GST_RIFF_TAG_data:{
1301 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1303 if (wav->ignore_length) {
1304 GST_DEBUG_OBJECT (wav, "Ignoring length");
1307 if (wav->streaming) {
1308 gst_adapter_flush (wav->adapter, 8);
1311 gst_buffer_unref (buf);
1314 wav->datastart = wav->offset;
1315 /* use size from ds64 chunk if available */
1316 if (size64 == -1 && wav->datasize > 0) {
1317 GST_DEBUG_OBJECT (wav, "Using ds64 datasize");
1318 size64 = wav->datasize;
1320 /* If size is zero, then the data chunk probably actually extends to
1321 the end of the file */
1322 if (size64 == 0 && upstream_size) {
1323 size64 = upstream_size - wav->datastart;
1325 /* Or the file might be truncated */
1326 else if (upstream_size) {
1327 size64 = MIN (size64, (upstream_size - wav->datastart));
1329 wav->datasize = size64;
1330 wav->dataleft = size64;
1331 wav->end_offset = size64 + wav->datastart;
1332 if (!wav->streaming) {
1333 /* We will continue parsing tags 'till end */
1334 wav->offset += size64;
1336 GST_DEBUG_OBJECT (wav, "datasize = %" G_GUINT64_FORMAT, size64);
1339 case GST_RIFF_TAG_fact:{
1340 if (wav->fact == 0 &&
1341 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1342 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1343 const guint data_size = 4;
1345 GST_INFO_OBJECT (wav, "Have fact chunk");
1346 if (size < data_size) {
1347 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1348 /* need more data */
1351 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1355 /* number of samples (for compressed formats) */
1356 if (wav->streaming) {
1357 const guint8 *data = NULL;
1359 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1362 gst_adapter_flush (wav->adapter, 8);
1363 data = gst_adapter_map (wav->adapter, data_size);
1364 wav->fact = GST_READ_UINT32_LE (data);
1365 gst_adapter_unmap (wav->adapter);
1366 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1368 gst_buffer_unref (buf);
1371 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1372 data_size, &buf)) != GST_FLOW_OK)
1373 goto header_read_error;
1374 gst_buffer_extract (buf, 0, &wav->fact, 4);
1375 wav->fact = GUINT32_FROM_LE (wav->fact);
1376 gst_buffer_unref (buf);
1378 GST_DEBUG_OBJECT (wav, "have fact %" G_GUINT64_FORMAT, wav->fact);
1379 wav->offset += 8 + GST_ROUND_UP_2 (size);
1382 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1383 /* need more data */
1389 case GST_RIFF_TAG_acid:{
1390 const gst_riff_acid *acid = NULL;
1391 const guint data_size = sizeof (gst_riff_acid);
1394 GST_INFO_OBJECT (wav, "Have acid chunk");
1395 if (size < data_size) {
1396 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1397 /* need more data */
1400 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1404 if (wav->streaming) {
1405 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1408 gst_adapter_flush (wav->adapter, 8);
1409 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1411 tempo = acid->tempo;
1412 gst_adapter_unmap (wav->adapter);
1415 gst_buffer_unref (buf);
1418 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1419 size, &buf)) != GST_FLOW_OK)
1420 goto header_read_error;
1421 gst_buffer_map (buf, &map, GST_MAP_READ);
1422 acid = (const gst_riff_acid *) map.data;
1423 tempo = acid->tempo;
1424 gst_buffer_unmap (buf, &map);
1426 /* send data as tags */
1428 wav->tags = gst_tag_list_new_empty ();
1429 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1430 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1432 size = GST_ROUND_UP_2 (size);
1433 if (wav->streaming) {
1434 gst_adapter_flush (wav->adapter, size);
1436 gst_buffer_unref (buf);
1438 wav->offset += 8 + size;
1441 /* FIXME: all list tags after data are ignored in streaming mode */
1442 case GST_RIFF_TAG_LIST:{
1445 if (wav->streaming) {
1446 const guint8 *data = NULL;
1448 if (gst_adapter_available (wav->adapter) < 12) {
1451 data = gst_adapter_map (wav->adapter, 12);
1452 ltag = GST_READ_UINT32_LE (data + 8);
1453 gst_adapter_unmap (wav->adapter);
1455 gst_buffer_unref (buf);
1458 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1459 &buf)) != GST_FLOW_OK)
1460 goto header_read_error;
1461 gst_buffer_extract (buf, 8, <ag, 4);
1462 ltag = GUINT32_FROM_LE (ltag);
1465 case GST_RIFF_LIST_INFO:{
1466 const gint data_size = size - 4;
1469 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1470 if (wav->streaming) {
1471 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1474 gst_adapter_flush (wav->adapter, 12);
1476 if (data_size > 0) {
1477 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1479 gst_adapter_flush (wav->adapter, 1);
1483 gst_buffer_unref (buf);
1485 if (data_size > 0) {
1487 gst_pad_pull_range (wav->sinkpad, wav->offset,
1488 data_size, &buf)) != GST_FLOW_OK)
1489 goto header_read_error;
1492 if (data_size > 0) {
1494 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1496 GstTagList *old = wav->tags;
1498 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1500 gst_tag_list_unref (old);
1501 gst_tag_list_unref (new);
1503 gst_buffer_unref (buf);
1504 wav->offset += GST_ROUND_UP_2 (data_size);
1508 case GST_RIFF_LIST_adtl:{
1509 const gint data_size = size - 4;
1511 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1512 if (wav->streaming) {
1513 const guint8 *data = NULL;
1515 gst_adapter_flush (wav->adapter, 12);
1517 data = gst_adapter_map (wav->adapter, data_size);
1518 gst_wavparse_adtl_chunk (wav, data, data_size);
1519 gst_adapter_unmap (wav->adapter);
1523 gst_buffer_unref (buf);
1527 gst_pad_pull_range (wav->sinkpad, wav->offset,
1528 data_size, &buf)) != GST_FLOW_OK)
1529 goto header_read_error;
1530 gst_buffer_map (buf, &map, GST_MAP_READ);
1531 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1533 gst_buffer_unmap (buf, &map);
1535 wav->offset += GST_ROUND_UP_2 (data_size);
1539 GST_WARNING_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1540 GST_FOURCC_ARGS (ltag));
1541 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1542 /* need more data */
1548 case GST_RIFF_TAG_cue:{
1549 const guint data_size = size;
1551 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1552 if (wav->streaming) {
1553 const guint8 *data = NULL;
1555 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1558 gst_adapter_flush (wav->adapter, 8);
1560 data = gst_adapter_map (wav->adapter, data_size);
1561 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1562 goto header_read_error;
1564 gst_adapter_unmap (wav->adapter);
1569 gst_buffer_unref (buf);
1572 gst_pad_pull_range (wav->sinkpad, wav->offset,
1573 data_size, &buf)) != GST_FLOW_OK)
1574 goto header_read_error;
1575 gst_buffer_map (buf, &map, GST_MAP_READ);
1576 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1578 goto header_read_error;
1580 gst_buffer_unmap (buf, &map);
1582 size = GST_ROUND_UP_2 (size);
1583 if (wav->streaming) {
1584 gst_adapter_flush (wav->adapter, size);
1586 gst_buffer_unref (buf);
1588 size = GST_ROUND_UP_2 (size);
1589 wav->offset += size;
1592 case GST_RIFF_TAG_smpl:{
1593 const gint data_size = size;
1595 GST_DEBUG_OBJECT (wav, "Have 'smpl' TAG, size : %u", data_size);
1596 if (wav->streaming) {
1597 const guint8 *data = NULL;
1599 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1602 gst_adapter_flush (wav->adapter, 8);
1604 data = gst_adapter_map (wav->adapter, data_size);
1605 if (!gst_wavparse_smpl_chunk (wav, data, data_size)) {
1606 goto header_read_error;
1608 gst_adapter_unmap (wav->adapter);
1613 gst_buffer_unref (buf);
1616 gst_pad_pull_range (wav->sinkpad, wav->offset,
1617 data_size, &buf)) != GST_FLOW_OK)
1618 goto header_read_error;
1619 gst_buffer_map (buf, &map, GST_MAP_READ);
1620 if (!gst_wavparse_smpl_chunk (wav, (const guint8 *) map.data,
1622 goto header_read_error;
1624 gst_buffer_unmap (buf, &map);
1626 size = GST_ROUND_UP_2 (size);
1627 if (wav->streaming) {
1628 gst_adapter_flush (wav->adapter, size);
1630 gst_buffer_unref (buf);
1632 size = GST_ROUND_UP_2 (size);
1633 wav->offset += size;
1637 GST_WARNING_OBJECT (wav, "Ignoring chunk %" GST_FOURCC_FORMAT,
1638 GST_FOURCC_ARGS (tag));
1639 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1640 /* need more data */
1645 if (upstream_size && (wav->offset >= upstream_size)) {
1646 /* Now we are gone through the whole file */
1651 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1653 if (wav->bps <= 0 && wav->fact) {
1655 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1657 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1658 (guint64) wav->fact);
1659 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1664 if (gst_wavparse_calculate_duration (wav)) {
1665 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1666 if (!wav->ignore_length)
1667 wav->segment.duration = wav->duration;
1669 gst_wavparse_create_toc (wav);
1671 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1672 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1673 if (!wav->ignore_length)
1674 wav->segment.duration = wav->datasize;
1677 /* now we have all the info to perform a pending seek if any, if no
1678 * event, this will still do the right thing and it will also send
1679 * the right newsegment event downstream. */
1680 gst_wavparse_perform_seek (wav, wav->seek_event);
1681 /* remove pending event */
1682 event_p = &wav->seek_event;
1683 gst_event_replace (event_p, NULL);
1685 /* we just started, we are discont */
1686 wav->discont = TRUE;
1688 wav->state = GST_WAVPARSE_DATA;
1690 /* determine reasonable max buffer size,
1691 * that is, buffers not too small either size or time wise
1692 * so we do not end up with too many of them */
1694 if (gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size))
1695 wav->max_buf_size = upstream_size;
1697 wav->max_buf_size = 0;
1698 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1699 if (wav->blockalign > 0)
1700 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1702 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1709 g_free (codec_name);
1712 gst_caps_unref (caps);
1717 res = GST_FLOW_ERROR;
1722 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1723 ("Invalid WAV header (no fmt at start): %"
1724 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1729 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1730 ("Couldn't parse audio header"));
1735 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1736 ("Stream claims to contain no channels - invalid data"));
1741 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1742 ("Stream with sample_rate == 0 - invalid data"));
1747 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1748 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1749 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1754 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1755 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1756 wav->av_bps, wav->blockalign * wav->rate));
1759 no_bytes_per_sample:
1761 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1762 ("Could not caluclate bytes per sample - invalid data"));
1767 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1768 ("No caps found for format 0x%x, %u channels, %u Hz",
1769 wav->format, wav->channels, wav->rate));
1774 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1775 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1781 * Read WAV file tag when streaming
1783 static GstFlowReturn
1784 gst_wavparse_parse_stream_init (GstWavParse * wav)
1786 if (gst_adapter_available (wav->adapter) >= 12) {
1789 /* _take flushes the data */
1790 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1792 GST_DEBUG ("Parsing wav header");
1793 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1794 return GST_FLOW_ERROR;
1797 /* Go to next state */
1798 wav->state = GST_WAVPARSE_HEADER;
1803 /* handle an event sent directly to the element.
1805 * This event can be sent either in the READY state or the
1806 * >READY state. The only event of interest really is the seek
1809 * In the READY state we can only store the event and try to
1810 * respect it when going to PAUSED. We assume we are in the
1811 * READY state when our parsing state != GST_WAVPARSE_DATA.
1813 * When we are steaming, we can simply perform the seek right
1817 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1819 GstWavParse *wav = GST_WAVPARSE (element);
1820 gboolean res = FALSE;
1823 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1825 switch (GST_EVENT_TYPE (event)) {
1826 case GST_EVENT_SEEK:
1827 if (wav->state == GST_WAVPARSE_DATA) {
1828 /* we can handle the seek directly when streaming data */
1829 res = gst_wavparse_perform_seek (wav, event);
1831 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1833 event_p = &wav->seek_event;
1834 gst_event_replace (event_p, event);
1836 /* we always return true */
1843 gst_event_unref (event);
1848 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1852 s = gst_caps_get_structure (caps, 0);
1853 if (!gst_structure_has_name (s, "audio/x-dts"))
1855 /* typefind behavior for DTS:
1856 * MAXIMUM: multiple frame syncs detected, certainly DTS
1857 * LIKELY: single frame sync at offset 0. Maybe DTS?
1858 * POSSIBLE: single frame sync, not at offset 0. Highly unlikely
1860 if (prob > GST_TYPE_FIND_LIKELY)
1862 if (prob <= GST_TYPE_FIND_POSSIBLE)
1864 /* for maybe, check for at least a valid-looking rate and channels */
1865 if (!gst_structure_has_field (s, "channels"))
1867 /* and for extra assurance we could also check the rate from the DTS frame
1868 * against the one in the wav header, but for now let's not do that */
1869 return gst_structure_has_field (s, "rate");
1873 gst_wavparse_get_upstream_tags (GstWavParse * wav, GstTagScope scope)
1875 GstTagList *tags = NULL;
1880 while ((ev = gst_pad_get_sticky_event (wav->sinkpad, GST_EVENT_TAG, i++))) {
1881 gst_event_parse_tag (ev, &tags);
1882 if (tags != NULL && gst_tag_list_get_scope (tags) == scope) {
1883 tags = gst_tag_list_copy (tags);
1884 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1885 gst_event_unref (ev);
1889 gst_event_unref (ev);
1895 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1898 GstTagList *tags, *utags;
1900 GST_DEBUG_OBJECT (wav, "adding src pad");
1902 g_assert (wav->caps != NULL);
1904 s = gst_caps_get_structure (wav->caps, 0);
1905 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1906 GstTypeFindProbability prob;
1909 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1910 if (tf_caps != NULL) {
1911 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1912 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1913 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1914 gst_caps_unref (wav->caps);
1915 wav->caps = tf_caps;
1917 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1918 GST_TAG_AUDIO_CODEC, "dts", NULL);
1920 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1921 "marked as raw PCM audio, but ignoring for now", tf_caps);
1922 gst_caps_unref (tf_caps);
1927 gst_pad_set_caps (wav->srcpad, wav->caps);
1928 gst_caps_replace (&wav->caps, NULL);
1930 if (wav->start_segment) {
1931 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1932 gst_pad_push_event (wav->srcpad, wav->start_segment);
1933 wav->start_segment = NULL;
1936 /* upstream tags, e.g. from id3/ape tag before the wav file; assume for now
1937 * that there'll be only one scope/type of tag list from upstream, if any */
1938 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_GLOBAL);
1940 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_STREAM);
1942 /* if there's a tag upstream it's probably been added to override the
1943 * tags from inside the wav header, so keep upstream tags if in doubt */
1944 tags = gst_tag_list_merge (utags, wav->tags, GST_TAG_MERGE_KEEP);
1946 if (wav->tags != NULL) {
1947 gst_tag_list_unref (wav->tags);
1952 gst_tag_list_unref (utags);
1954 /* send tags downstream, if any */
1956 gst_pad_push_event (wav->srcpad, gst_event_new_tag (tags));
1959 static GstFlowReturn
1960 gst_wavparse_stream_data (GstWavParse * wav)
1962 GstBuffer *buf = NULL;
1963 GstFlowReturn res = GST_FLOW_OK;
1964 guint64 desired, obtained;
1965 GstClockTime timestamp, next_timestamp, duration;
1966 guint64 pos, nextpos;
1969 GST_LOG_OBJECT (wav,
1970 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1971 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1973 /* Get the next n bytes and output them */
1974 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1977 /* scale the amount of data by the segment rate so we get equal
1978 * amounts of data regardless of the playback rate */
1980 MIN (gst_guint64_to_gdouble (wav->dataleft),
1981 wav->max_buf_size * ABS (wav->segment.rate));
1983 if (desired >= wav->blockalign && wav->blockalign > 0)
1984 desired -= (desired % wav->blockalign);
1986 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1987 "from the sinkpad", desired);
1989 if (wav->streaming) {
1990 guint avail = gst_adapter_available (wav->adapter);
1993 /* flush some bytes if evil upstream sends segment that starts
1994 * before data or does is not send sample aligned segment */
1995 if (G_LIKELY (wav->offset >= wav->datastart)) {
1996 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1998 extra = wav->datastart - wav->offset;
2001 if (G_UNLIKELY (extra)) {
2002 extra = wav->bytes_per_sample - extra;
2003 if (extra <= avail) {
2004 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
2005 gst_adapter_flush (wav->adapter, extra);
2006 wav->offset += extra;
2007 wav->dataleft -= extra;
2008 goto iterate_adapter;
2010 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
2011 gst_adapter_clear (wav->adapter);
2012 wav->offset += avail;
2013 wav->dataleft -= avail;
2018 if (avail < desired) {
2019 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
2023 buf = gst_adapter_take_buffer (wav->adapter, desired);
2025 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
2026 desired, &buf)) != GST_FLOW_OK)
2029 /* we may get a short buffer at the end of the file */
2030 if (gst_buffer_get_size (buf) < desired) {
2031 gsize size = gst_buffer_get_size (buf);
2033 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2034 if (size >= wav->blockalign) {
2035 if (wav->blockalign > 0) {
2036 buf = gst_buffer_make_writable (buf);
2037 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2040 gst_buffer_unref (buf);
2046 obtained = gst_buffer_get_size (buf);
2048 /* our positions in bytes */
2049 pos = wav->offset - wav->datastart;
2050 nextpos = pos + obtained;
2052 /* update offsets, does not overflow. */
2053 buf = gst_buffer_make_writable (buf);
2054 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2055 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2057 /* first chunk of data? create the source pad. We do this only here so
2058 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2059 if (G_UNLIKELY (wav->first)) {
2061 /* this will also push the segment events */
2062 gst_wavparse_add_src_pad (wav, buf);
2064 /* If we have a pending start segment, send it now. */
2065 if (G_UNLIKELY (wav->start_segment != NULL)) {
2066 gst_pad_push_event (wav->srcpad, wav->start_segment);
2067 wav->start_segment = NULL;
2072 /* and timestamps if we have a bitrate, be careful for overflows */
2074 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2076 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2077 duration = next_timestamp - timestamp;
2079 /* update current running segment position */
2080 if (G_LIKELY (next_timestamp >= wav->segment.start))
2081 wav->segment.position = next_timestamp;
2082 } else if (wav->fact) {
2084 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2085 /* and timestamps if we have a bitrate, be careful for overflows */
2086 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2087 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2088 duration = next_timestamp - timestamp;
2090 /* no bitrate, all we know is that the first sample has timestamp 0, all
2091 * other positions and durations have unknown timestamp. */
2095 timestamp = GST_CLOCK_TIME_NONE;
2096 duration = GST_CLOCK_TIME_NONE;
2097 /* update current running segment position with byte offset */
2098 if (G_LIKELY (nextpos >= wav->segment.start))
2099 wav->segment.position = nextpos;
2101 if ((pos > 0) && wav->vbr) {
2102 /* don't set timestamps for VBR files if it's not the first buffer */
2103 timestamp = GST_CLOCK_TIME_NONE;
2104 duration = GST_CLOCK_TIME_NONE;
2107 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2108 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2109 wav->discont = FALSE;
2112 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2113 GST_BUFFER_DURATION (buf) = duration;
2115 GST_LOG_OBJECT (wav,
2116 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2117 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2118 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2120 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2123 if (obtained < wav->dataleft) {
2124 wav->offset += obtained;
2125 wav->dataleft -= obtained;
2127 wav->offset += wav->dataleft;
2131 /* Iterate until need more data, so adapter size won't grow */
2132 if (wav->streaming) {
2133 GST_LOG_OBJECT (wav,
2134 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2136 goto iterate_adapter;
2143 GST_DEBUG_OBJECT (wav, "found EOS");
2144 return GST_FLOW_EOS;
2148 /* check if we got EOS */
2149 if (res == GST_FLOW_EOS)
2152 GST_WARNING_OBJECT (wav,
2153 "Error getting %" G_GINT64_FORMAT " bytes from the "
2154 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2159 GST_INFO_OBJECT (wav,
2160 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2161 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2162 gst_pad_is_linked (wav->srcpad));
2168 gst_wavparse_loop (GstPad * pad)
2171 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2175 GST_LOG_OBJECT (wav, "process data");
2177 switch (wav->state) {
2178 case GST_WAVPARSE_START:
2179 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2180 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2184 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2185 event = gst_event_new_stream_start (stream_id);
2186 gst_event_set_group_id (event, gst_util_group_id_next ());
2187 gst_pad_push_event (wav->srcpad, event);
2190 wav->state = GST_WAVPARSE_HEADER;
2193 case GST_WAVPARSE_HEADER:
2194 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2195 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2198 wav->state = GST_WAVPARSE_DATA;
2199 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2202 case GST_WAVPARSE_DATA:
2203 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2207 g_assert_not_reached ();
2214 const gchar *reason = gst_flow_get_name (ret);
2216 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2217 gst_pad_pause_task (pad);
2219 if (ret == GST_FLOW_EOS) {
2220 /* handle end-of-stream/segment */
2221 /* so align our position with the end of it, if there is one
2222 * this ensures a subsequent will arrive at correct base/acc time */
2223 if (wav->segment.format == GST_FORMAT_TIME) {
2224 if (wav->segment.rate > 0.0 &&
2225 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2226 wav->segment.position = wav->segment.stop;
2227 else if (wav->segment.rate < 0.0)
2228 wav->segment.position = wav->segment.start;
2230 if (wav->state == GST_WAVPARSE_START) {
2231 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2232 ("No valid input found before end of stream"));
2233 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2235 /* add pad before we perform EOS */
2236 if (G_UNLIKELY (wav->first)) {
2238 gst_wavparse_add_src_pad (wav, NULL);
2241 /* perform EOS logic */
2242 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2245 if ((stop = wav->segment.stop) == -1)
2246 stop = wav->segment.duration;
2248 gst_element_post_message (GST_ELEMENT_CAST (wav),
2249 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2250 wav->segment.format, stop));
2251 gst_pad_push_event (wav->srcpad,
2252 gst_event_new_segment_done (wav->segment.format, stop));
2254 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2257 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2258 /* for fatal errors we post an error message, post the error
2259 * first so the app knows about the error first. */
2260 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2261 (_("Internal data flow error.")),
2262 ("streaming task paused, reason %s (%d)", reason, ret));
2263 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2269 static GstFlowReturn
2270 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2273 GstWavParse *wav = GST_WAVPARSE (parent);
2275 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2276 gst_buffer_get_size (buf));
2278 gst_adapter_push (wav->adapter, buf);
2280 switch (wav->state) {
2281 case GST_WAVPARSE_START:
2282 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2283 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2286 if (wav->state != GST_WAVPARSE_HEADER)
2289 /* otherwise fall-through */
2290 case GST_WAVPARSE_HEADER:
2291 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2292 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2295 if (!wav->got_fmt || wav->datastart == 0)
2298 wav->state = GST_WAVPARSE_DATA;
2299 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2302 case GST_WAVPARSE_DATA:
2303 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2304 wav->discont = TRUE;
2305 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2309 g_return_val_if_reached (GST_FLOW_ERROR);
2312 if (G_UNLIKELY (wav->abort_buffering)) {
2313 wav->abort_buffering = FALSE;
2314 ret = GST_FLOW_ERROR;
2315 /* sort of demux/parse error */
2316 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2322 static GstFlowReturn
2323 gst_wavparse_flush_data (GstWavParse * wav)
2325 GstFlowReturn ret = GST_FLOW_OK;
2328 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2330 wav->end_offset = wav->offset + av;
2331 ret = gst_wavparse_stream_data (wav);
2338 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2340 GstWavParse *wav = GST_WAVPARSE (parent);
2341 gboolean ret = TRUE;
2343 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2345 switch (GST_EVENT_TYPE (event)) {
2346 case GST_EVENT_CAPS:
2348 /* discard, we'll come up with proper src caps */
2349 gst_event_unref (event);
2352 case GST_EVENT_SEGMENT:
2354 gint64 start, stop, offset = 0, end_offset = -1;
2357 /* some debug output */
2358 gst_event_copy_segment (event, &segment);
2359 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2362 if (wav->state != GST_WAVPARSE_DATA) {
2363 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2367 /* now we are either committed to TIME or BYTE format,
2368 * and we only expect a BYTE segment, e.g. following a seek */
2369 if (segment.format == GST_FORMAT_BYTES) {
2370 /* handle (un)signed issues */
2371 start = segment.start;
2372 stop = segment.stop;
2375 start -= wav->datastart;
2376 start = MAX (start, 0);
2380 stop -= wav->datastart;
2381 stop = MAX (stop, 0);
2383 if (wav->segment.format == GST_FORMAT_TIME) {
2384 guint64 bps = wav->bps;
2386 /* operating in format TIME, so we can convert */
2387 if (!bps && wav->fact)
2389 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2393 gst_util_uint64_scale_ceil (start, GST_SECOND,
2394 (guint64) wav->bps);
2397 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2398 (guint64) wav->bps);
2402 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2406 segment.start = start;
2407 segment.stop = stop;
2409 /* accept upstream's notion of segment and distribute along */
2410 segment.format = wav->segment.format;
2411 segment.time = segment.position = segment.start;
2412 segment.duration = wav->segment.duration;
2413 segment.base = gst_segment_to_running_time (&wav->segment,
2414 GST_FORMAT_TIME, wav->segment.position);
2416 gst_segment_copy_into (&segment, &wav->segment);
2418 /* also store the newsegment event for the streaming thread */
2419 if (wav->start_segment)
2420 gst_event_unref (wav->start_segment);
2421 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2422 wav->start_segment = gst_event_new_segment (&segment);
2424 /* stream leftover data in current segment */
2425 gst_wavparse_flush_data (wav);
2426 /* and set up streaming thread for next one */
2427 wav->offset = offset;
2428 wav->end_offset = end_offset;
2429 if (wav->end_offset > 0) {
2430 wav->dataleft = wav->end_offset - wav->offset;
2432 /* infinity; upstream will EOS when done */
2433 wav->dataleft = G_MAXUINT64;
2436 gst_event_unref (event);
2440 if (wav->state == GST_WAVPARSE_START) {
2441 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2442 ("No valid input found before end of stream"));
2444 /* add pad if needed so EOS is seen downstream */
2445 if (G_UNLIKELY (wav->first)) {
2447 gst_wavparse_add_src_pad (wav, NULL);
2449 /* stream leftover data in current segment */
2450 gst_wavparse_flush_data (wav);
2455 case GST_EVENT_FLUSH_STOP:
2459 gst_adapter_clear (wav->adapter);
2460 wav->discont = TRUE;
2461 dur = wav->segment.duration;
2462 gst_segment_init (&wav->segment, wav->segment.format);
2463 wav->segment.duration = dur;
2467 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2475 /* convert and query stuff */
2476 static const GstFormat *
2477 gst_wavparse_get_formats (GstPad * pad)
2479 static const GstFormat formats[] = {
2482 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2491 gst_wavparse_pad_convert (GstPad * pad,
2492 GstFormat src_format, gint64 src_value,
2493 GstFormat * dest_format, gint64 * dest_value)
2495 GstWavParse *wavparse;
2496 gboolean res = TRUE;
2498 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2500 if (*dest_format == src_format) {
2501 *dest_value = src_value;
2505 if ((wavparse->bps == 0) && !wavparse->fact)
2508 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2509 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2511 switch (src_format) {
2512 case GST_FORMAT_BYTES:
2513 switch (*dest_format) {
2514 case GST_FORMAT_DEFAULT:
2515 *dest_value = src_value / wavparse->bytes_per_sample;
2516 /* make sure we end up on a sample boundary */
2517 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2519 case GST_FORMAT_TIME:
2520 /* src_value + datastart = offset */
2521 GST_INFO_OBJECT (wavparse,
2522 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2524 if (wavparse->bps > 0)
2525 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2526 (guint64) wavparse->bps);
2527 else if (wavparse->fact) {
2528 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2529 wavparse->rate, wavparse->fact);
2532 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2543 case GST_FORMAT_DEFAULT:
2544 switch (*dest_format) {
2545 case GST_FORMAT_BYTES:
2546 *dest_value = src_value * wavparse->bytes_per_sample;
2548 case GST_FORMAT_TIME:
2549 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2550 (guint64) wavparse->rate);
2558 case GST_FORMAT_TIME:
2559 switch (*dest_format) {
2560 case GST_FORMAT_BYTES:
2561 if (wavparse->bps > 0)
2562 *dest_value = gst_util_uint64_scale (src_value,
2563 (guint64) wavparse->bps, GST_SECOND);
2565 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2566 wavparse->rate, wavparse->fact);
2568 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2570 /* make sure we end up on a sample boundary */
2571 *dest_value -= *dest_value % wavparse->blockalign;
2573 case GST_FORMAT_DEFAULT:
2574 *dest_value = gst_util_uint64_scale (src_value,
2575 (guint64) wavparse->rate, GST_SECOND);
2594 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2600 /* handle queries for location and length in requested format */
2602 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2604 gboolean res = TRUE;
2605 GstWavParse *wav = GST_WAVPARSE (parent);
2607 /* only if we know */
2608 if (wav->state != GST_WAVPARSE_DATA) {
2612 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2614 switch (GST_QUERY_TYPE (query)) {
2615 case GST_QUERY_POSITION:
2621 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2622 curb = wav->offset - wav->datastart;
2623 gst_query_parse_position (query, &format, NULL);
2624 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2627 case GST_FORMAT_BYTES:
2628 format = GST_FORMAT_BYTES;
2632 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2637 gst_query_set_position (query, format, cur);
2640 case GST_QUERY_DURATION:
2642 gint64 duration = 0;
2645 if (wav->ignore_length) {
2650 gst_query_parse_duration (query, &format, NULL);
2653 case GST_FORMAT_BYTES:{
2654 format = GST_FORMAT_BYTES;
2655 duration = wav->datasize;
2658 case GST_FORMAT_TIME:
2659 if ((res = gst_wavparse_calculate_duration (wav))) {
2660 duration = wav->duration;
2668 gst_query_set_duration (query, format, duration);
2671 case GST_QUERY_CONVERT:
2673 gint64 srcvalue, dstvalue;
2674 GstFormat srcformat, dstformat;
2676 gst_query_parse_convert (query, &srcformat, &srcvalue,
2677 &dstformat, &dstvalue);
2678 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2679 &dstformat, &dstvalue);
2681 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2684 case GST_QUERY_SEEKING:{
2686 gboolean seekable = FALSE;
2688 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2689 if (fmt == wav->segment.format) {
2690 if (wav->streaming) {
2693 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2694 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2695 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2696 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2698 gst_query_unref (q);
2700 GST_LOG_OBJECT (wav, "looping => seekable");
2704 } else if (fmt == GST_FORMAT_TIME) {
2708 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2713 res = gst_pad_query_default (pad, parent, query);
2720 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2722 GstWavParse *wavparse = GST_WAVPARSE (parent);
2723 gboolean res = FALSE;
2725 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2727 switch (GST_EVENT_TYPE (event)) {
2728 case GST_EVENT_SEEK:
2729 /* can only handle events when we are in the data state */
2730 if (wavparse->state == GST_WAVPARSE_DATA) {
2731 res = gst_wavparse_perform_seek (wavparse, event);
2733 gst_event_unref (event);
2736 case GST_EVENT_TOC_SELECT:
2739 GstTocEntry *entry = NULL;
2740 GstEvent *seek_event;
2743 if (!wavparse->toc) {
2744 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2747 gst_event_parse_toc_select (event, &uid);
2749 GST_OBJECT_LOCK (wavparse);
2750 entry = gst_toc_find_entry (wavparse->toc, uid);
2751 if (entry == NULL) {
2752 GST_OBJECT_UNLOCK (wavparse);
2753 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2757 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2758 GST_OBJECT_UNLOCK (wavparse);
2759 seek_event = gst_event_new_seek (1.0,
2761 GST_SEEK_FLAG_FLUSH,
2762 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2763 res = gst_wavparse_perform_seek (wavparse, seek_event);
2764 gst_event_unref (seek_event);
2768 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2772 gst_event_unref (event);
2777 res = gst_pad_push_event (wavparse->sinkpad, event);
2784 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2786 GstWavParse *wav = GST_WAVPARSE (parent);
2791 gst_adapter_clear (wav->adapter);
2792 g_object_unref (wav->adapter);
2793 wav->adapter = NULL;
2796 query = gst_query_new_scheduling ();
2798 if (!gst_pad_peer_query (sinkpad, query)) {
2799 gst_query_unref (query);
2803 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2804 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2805 gst_query_unref (query);
2810 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2811 wav->streaming = FALSE;
2812 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2816 GST_DEBUG_OBJECT (sinkpad, "activating push");
2817 wav->streaming = TRUE;
2818 wav->adapter = gst_adapter_new ();
2819 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2825 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2826 GstPadMode mode, gboolean active)
2831 case GST_PAD_MODE_PUSH:
2834 case GST_PAD_MODE_PULL:
2836 /* if we have a scheduler we can start the task */
2837 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2840 res = gst_pad_stop_task (sinkpad);
2850 static GstStateChangeReturn
2851 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2853 GstStateChangeReturn ret;
2854 GstWavParse *wav = GST_WAVPARSE (element);
2856 switch (transition) {
2857 case GST_STATE_CHANGE_NULL_TO_READY:
2859 case GST_STATE_CHANGE_READY_TO_PAUSED:
2860 gst_wavparse_reset (wav);
2862 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2868 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2870 switch (transition) {
2871 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2873 case GST_STATE_CHANGE_PAUSED_TO_READY:
2874 gst_wavparse_reset (wav);
2876 case GST_STATE_CHANGE_READY_TO_NULL:
2885 gst_wavparse_set_property (GObject * object, guint prop_id,
2886 const GValue * value, GParamSpec * pspec)
2890 g_return_if_fail (GST_IS_WAVPARSE (object));
2891 self = GST_WAVPARSE (object);
2894 case PROP_IGNORE_LENGTH:
2895 self->ignore_length = g_value_get_boolean (value);
2898 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2904 gst_wavparse_get_property (GObject * object, guint prop_id,
2905 GValue * value, GParamSpec * pspec)
2909 g_return_if_fail (GST_IS_WAVPARSE (object));
2910 self = GST_WAVPARSE (object);
2913 case PROP_IGNORE_LENGTH:
2914 g_value_set_boolean (value, self->ignore_length);
2917 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2922 plugin_init (GstPlugin * plugin)
2926 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2930 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2933 "Parse a .wav file into raw audio",
2934 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)