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/pbutils/descriptions.h>
58 #include <gst/gst-i18n-plugin.h>
60 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
61 #define GST_CAT_DEFAULT (wavparse_debug)
63 /* Data size chunk of RF64,
64 * see http://tech.ebu.ch/docs/tech/tech3306-2009.pdf */
65 #define GST_RS64_TAG_DS64 GST_MAKE_FOURCC ('d','s','6','4')
67 static void gst_wavparse_dispose (GObject * object);
69 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad,
71 static gboolean gst_wavparse_sink_activate_mode (GstPad * sinkpad,
72 GstObject * parent, GstPadMode mode, gboolean active);
73 static gboolean gst_wavparse_send_event (GstElement * element,
75 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
76 GstStateChange transition);
78 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
80 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
81 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
83 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
85 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
87 static void gst_wavparse_loop (GstPad * pad);
88 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
91 static void gst_wavparse_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_wavparse_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 #define DEFAULT_IGNORE_LENGTH FALSE
104 static GstStaticPadTemplate sink_template_factory =
105 GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_STATIC_CAPS ("audio/x-wav")
112 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
114 #define gst_wavparse_parent_class parent_class
115 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
120 /* Offset Size Description Value
121 * 0x00 4 ID unique identification value
122 * 0x04 4 Position play order position
123 * 0x08 4 Data Chunk ID RIFF ID of corresponding data chunk
124 * 0x0c 4 Chunk Start Byte Offset of Data Chunk *
125 * 0x10 4 Block Start Byte Offset to sample of First Channel
126 * 0x14 4 Sample Offset Byte Offset to sample byte of First Channel
130 guint32 data_chunk_id;
133 guint32 sample_offset;
138 /* Offset Size Description Value
139 * 0x08 4 Cue Point ID 0 - 0xFFFFFFFF
142 guint32 cue_point_id;
144 } GstWavParseLabl, GstWavParseNote;
147 gst_wavparse_class_init (GstWavParseClass * klass)
149 GstElementClass *gstelement_class;
150 GObjectClass *object_class;
151 GstPadTemplate *src_template;
153 gstelement_class = (GstElementClass *) klass;
154 object_class = (GObjectClass *) klass;
156 parent_class = g_type_class_peek_parent (klass);
158 object_class->dispose = gst_wavparse_dispose;
160 object_class->set_property = gst_wavparse_set_property;
161 object_class->get_property = gst_wavparse_get_property;
164 * GstWavParse:ignore-length:
166 * This selects whether the length found in a data chunk
167 * should be ignored. This may be useful for streamed audio
168 * where the length is unknown until the end of streaming,
169 * and various software/hardware just puts some random value
170 * in there and hopes it doesn't break too much.
172 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
173 g_param_spec_boolean ("ignore-length",
175 "Ignore length from the Wave header",
176 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
179 gstelement_class->change_state = gst_wavparse_change_state;
180 gstelement_class->send_event = gst_wavparse_send_event;
183 gst_element_class_add_static_pad_template (gstelement_class,
184 &sink_template_factory);
186 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
187 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
188 gst_element_class_add_pad_template (gstelement_class, src_template);
190 gst_element_class_set_static_metadata (gstelement_class, "WAV audio demuxer",
191 "Codec/Demuxer/Audio",
192 "Parse a .wav file into raw audio",
193 "Erik Walthinsen <omega@cse.ogi.edu>");
197 gst_wavparse_notes_free (GstWavParseNote * note)
205 gst_wavparse_labls_free (GstWavParseLabl * labl)
213 gst_wavparse_reset (GstWavParse * wav)
215 wav->state = GST_WAVPARSE_START;
217 /* These will all be set correctly in the fmt chunk */
232 wav->got_fmt = FALSE;
236 gst_event_unref (wav->seek_event);
237 wav->seek_event = NULL;
239 gst_adapter_clear (wav->adapter);
240 g_object_unref (wav->adapter);
244 gst_tag_list_unref (wav->tags);
247 gst_toc_unref (wav->toc);
250 g_list_free_full (wav->cues, g_free);
253 g_list_free_full (wav->labls, (GDestroyNotify) gst_wavparse_labls_free);
256 g_list_free_full (wav->notes, (GDestroyNotify) gst_wavparse_notes_free);
259 gst_caps_unref (wav->caps);
261 if (wav->start_segment)
262 gst_event_unref (wav->start_segment);
263 wav->start_segment = NULL;
267 gst_wavparse_dispose (GObject * object)
269 GstWavParse *wav = GST_WAVPARSE (object);
271 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
272 gst_wavparse_reset (wav);
274 G_OBJECT_CLASS (parent_class)->dispose (object);
278 gst_wavparse_init (GstWavParse * wavparse)
280 gst_wavparse_reset (wavparse);
284 gst_pad_new_from_static_template (&sink_template_factory, "sink");
285 gst_pad_set_activate_function (wavparse->sinkpad,
286 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
287 gst_pad_set_activatemode_function (wavparse->sinkpad,
288 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
289 gst_pad_set_chain_function (wavparse->sinkpad,
290 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
291 gst_pad_set_event_function (wavparse->sinkpad,
292 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
293 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
297 gst_pad_new_from_template (gst_element_class_get_pad_template
298 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
299 gst_pad_use_fixed_caps (wavparse->srcpad);
300 gst_pad_set_query_function (wavparse->srcpad,
301 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
302 gst_pad_set_event_function (wavparse->srcpad,
303 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
304 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
308 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
312 if (!gst_riff_parse_file_header (element, buf, &doctype))
315 if (doctype != GST_RIFF_RIFF_WAVE)
323 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
324 ("File is not a WAVE file: 0x%" G_GINT32_MODIFIER "x", doctype));
330 gst_wavparse_stream_init (GstWavParse * wav)
333 GstBuffer *buf = NULL;
335 if ((res = gst_pad_pull_range (wav->sinkpad,
336 wav->offset, 12, &buf)) != GST_FLOW_OK)
338 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
339 return GST_FLOW_ERROR;
347 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
349 /* -1 always maps to -1 */
355 /* 0 always maps to 0 */
362 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
364 } else if (wav->fact) {
365 guint64 bps = gst_util_uint64_scale (wav->datasize, wav->rate, wav->fact);
366 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
373 /* This function is used to perform seeks on the element.
375 * It also works when event is NULL, in which case it will just
376 * start from the last configured segment. This technique is
377 * used when activating the element and to perform the seek in
381 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
385 GstFormat format, bformat;
387 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
388 gint64 cur, stop, upstream_size;
391 GstSegment seeksegment = { 0, };
396 GST_DEBUG_OBJECT (wav, "doing seek with event");
398 gst_event_parse_seek (event, &rate, &format, &flags,
399 &cur_type, &cur, &stop_type, &stop);
400 seqnum = gst_event_get_seqnum (event);
402 /* no negative rates yet */
406 if (format != wav->segment.format) {
407 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
408 gst_format_get_name (format),
409 gst_format_get_name (wav->segment.format));
411 if (cur_type != GST_SEEK_TYPE_NONE)
413 gst_pad_query_convert (wav->srcpad, format, cur,
414 wav->segment.format, &cur);
415 if (res && stop_type != GST_SEEK_TYPE_NONE)
417 gst_pad_query_convert (wav->srcpad, format, stop,
418 wav->segment.format, &stop);
422 format = wav->segment.format;
425 GST_DEBUG_OBJECT (wav, "doing seek without event");
428 cur_type = GST_SEEK_TYPE_SET;
429 stop_type = GST_SEEK_TYPE_SET;
432 /* in push mode, we must delegate to upstream */
433 if (wav->streaming) {
434 gboolean res = FALSE;
436 /* if streaming not yet started; only prepare initial newsegment */
437 if (!event || wav->state != GST_WAVPARSE_DATA) {
438 if (wav->start_segment)
439 gst_event_unref (wav->start_segment);
440 wav->start_segment = gst_event_new_segment (&wav->segment);
443 /* convert seek positions to byte positions in data sections */
444 if (format == GST_FORMAT_TIME) {
445 /* should not fail */
446 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
448 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
451 /* mind sample boundary and header */
453 cur -= (cur % wav->bytes_per_sample);
454 cur += wav->datastart;
457 stop -= (stop % wav->bytes_per_sample);
458 stop += wav->datastart;
460 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
461 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
463 /* BYTE seek event */
464 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
466 gst_event_set_seqnum (event, seqnum);
467 res = gst_pad_push_event (wav->sinkpad, event);
473 flush = flags & GST_SEEK_FLAG_FLUSH;
475 /* now we need to make sure the streaming thread is stopped. We do this by
476 * either sending a FLUSH_START event downstream which will cause the
477 * streaming thread to stop with a WRONG_STATE.
478 * For a non-flushing seek we simply pause the task, which will happen as soon
479 * as it completes one iteration (and thus might block when the sink is
480 * blocking in preroll). */
483 GST_DEBUG_OBJECT (wav, "sending flush start");
485 fevent = gst_event_new_flush_start ();
486 gst_event_set_seqnum (fevent, seqnum);
487 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
488 gst_pad_push_event (wav->srcpad, fevent);
490 gst_pad_pause_task (wav->sinkpad);
493 /* we should now be able to grab the streaming thread because we stopped it
494 * with the above flush/pause code */
495 GST_PAD_STREAM_LOCK (wav->sinkpad);
497 /* save current position */
498 last_stop = wav->segment.position;
500 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
502 /* copy segment, we need this because we still need the old
503 * segment when we close the current segment. */
504 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
506 /* configure the seek parameters in the seeksegment. We will then have the
507 * right values in the segment to perform the seek */
509 GST_DEBUG_OBJECT (wav, "configuring seek");
510 gst_segment_do_seek (&seeksegment, rate, format, flags,
511 cur_type, cur, stop_type, stop, &update);
514 /* figure out the last position we need to play. If it's configured (stop !=
515 * -1), use that, else we play until the total duration of the file */
516 if ((stop = seeksegment.stop) == -1)
517 stop = seeksegment.duration;
519 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
520 if ((cur_type != GST_SEEK_TYPE_NONE)) {
521 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
522 * we can just copy the last_stop. If not, we use the bps to convert TIME to
524 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
525 (gint64 *) & wav->offset))
526 wav->offset = seeksegment.position;
527 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
528 wav->offset -= (wav->offset % wav->bytes_per_sample);
529 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
530 wav->offset += wav->datastart;
531 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
533 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
537 if (stop_type != GST_SEEK_TYPE_NONE) {
538 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
539 wav->end_offset = stop;
540 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
541 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
542 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
543 wav->end_offset += wav->datastart;
544 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
546 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
550 /* make sure filesize is not exceeded due to rounding errors or so,
551 * same precaution as in _stream_headers */
552 bformat = GST_FORMAT_BYTES;
553 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
554 wav->end_offset = MIN (wav->end_offset, upstream_size);
556 if (wav->datasize > 0 && wav->end_offset > wav->datastart + wav->datasize)
557 wav->end_offset = wav->datastart + wav->datasize;
559 /* this is the range of bytes we will use for playback */
560 wav->offset = MIN (wav->offset, wav->end_offset);
561 wav->dataleft = wav->end_offset - wav->offset;
563 GST_DEBUG_OBJECT (wav,
564 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
565 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
566 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
568 /* prepare for streaming again */
572 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
573 GST_DEBUG_OBJECT (wav, "sending flush stop");
575 fevent = gst_event_new_flush_stop (TRUE);
576 gst_event_set_seqnum (fevent, seqnum);
577 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
578 gst_pad_push_event (wav->srcpad, fevent);
581 /* now we did the seek and can activate the new segment values */
582 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
584 /* if we're doing a segment seek, post a SEGMENT_START message */
585 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
586 gst_element_post_message (GST_ELEMENT_CAST (wav),
587 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
588 wav->segment.format, wav->segment.position));
591 /* now create the newsegment */
592 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
593 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
595 /* store the newsegment event so it can be sent from the streaming thread. */
596 if (wav->start_segment)
597 gst_event_unref (wav->start_segment);
598 wav->start_segment = gst_event_new_segment (&wav->segment);
599 gst_event_set_seqnum (wav->start_segment, seqnum);
601 /* mark discont if we are going to stream from another position. */
602 if (last_stop != wav->segment.position) {
603 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
607 /* and start the streaming task again */
608 if (!wav->streaming) {
609 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
613 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
620 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
625 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
630 GST_DEBUG_OBJECT (wav,
631 "Could not determine byte position for desired time");
637 * gst_wavparse_peek_chunk_info:
638 * @wav Wavparse object
639 * @tag holder for tag
640 * @size holder for tag size
642 * Peek next chunk info (tag and size)
644 * Returns: %TRUE when the chunk info (header) is available
647 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
649 const guint8 *data = NULL;
651 if (gst_adapter_available (wav->adapter) < 8)
654 data = gst_adapter_map (wav->adapter, 8);
655 *tag = GST_READ_UINT32_LE (data);
656 *size = GST_READ_UINT32_LE (data + 4);
657 gst_adapter_unmap (wav->adapter);
659 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
660 GST_FOURCC_ARGS (*tag));
666 * gst_wavparse_peek_chunk:
667 * @wav Wavparse object
668 * @tag holder for tag
669 * @size holder for tag size
671 * Peek enough data for one full chunk
673 * Returns: %TRUE when the full chunk is available
676 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
678 guint32 peek_size = 0;
681 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
684 /* size 0 -> empty data buffer would surprise most callers,
685 * large size -> do not bother trying to squeeze that into adapter,
686 * so we throw poor man's exception, which can be caught if caller really
687 * wants to handle 0 size chunk */
688 if (!(*size) || (*size) >= (1 << 30)) {
689 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
690 *size, GST_FOURCC_ARGS (*tag));
691 /* chain should give up */
692 wav->abort_buffering = TRUE;
695 peek_size = (*size + 1) & ~1;
696 available = gst_adapter_available (wav->adapter);
698 if (available >= (8 + peek_size)) {
701 GST_LOG ("but only %u bytes available now", available);
707 * gst_wavparse_calculate_duration:
708 * @wav: wavparse object
710 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
713 * Returns: %TRUE if duration is available.
716 gst_wavparse_calculate_duration (GstWavParse * wav)
718 if (wav->duration > 0)
722 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
724 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
726 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
727 GST_TIME_ARGS (wav->duration));
729 } else if (wav->fact) {
731 gst_util_uint64_scale_ceil (GST_SECOND, wav->fact, wav->rate);
732 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
733 GST_TIME_ARGS (wav->duration));
740 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
745 if (wav->streaming) {
746 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
749 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
750 GST_FOURCC_ARGS (tag));
751 flush = 8 + ((size + 1) & ~1);
752 wav->offset += flush;
753 if (wav->streaming) {
754 gst_adapter_flush (wav->adapter, flush);
756 gst_buffer_unref (buf);
763 * gst_wavparse_cue_chunk:
764 * @wav GstWavParse object
765 * @data holder for data
766 * @size holder for data size
768 * Parse cue chunk from @data to wav->cues.
770 * Returns: %TRUE when cue chunk is available
773 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
780 GST_WARNING_OBJECT (wav, "found another cue's");
784 ncues = GST_READ_UINT32_LE (data);
786 if (size < 4 + ncues * 24) {
787 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
793 for (i = 0; i < ncues; i++) {
794 cue = g_new0 (GstWavParseCue, 1);
795 cue->id = GST_READ_UINT32_LE (data);
796 cue->position = GST_READ_UINT32_LE (data + 4);
797 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
798 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
799 cue->block_start = GST_READ_UINT32_LE (data + 16);
800 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
801 cues = g_list_append (cues, cue);
811 * gst_wavparse_labl_chunk:
812 * @wav GstWavParse object
813 * @data holder for data
814 * @size holder for data size
816 * Parse labl from @data to wav->labls.
818 * Returns: %TRUE when labl chunk is available
821 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
823 GstWavParseLabl *labl;
828 labl = g_new0 (GstWavParseLabl, 1);
832 labl->cue_point_id = GST_READ_UINT32_LE (data);
833 labl->text = g_memdup (data + 4, size - 4);
835 wav->labls = g_list_append (wav->labls, labl);
841 * gst_wavparse_note_chunk:
842 * @wav GstWavParse object
843 * @data holder for data
844 * @size holder for data size
846 * Parse note from @data to wav->notes.
848 * Returns: %TRUE when note chunk is available
851 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
853 GstWavParseNote *note;
858 note = g_new0 (GstWavParseNote, 1);
862 note->cue_point_id = GST_READ_UINT32_LE (data);
863 note->text = g_memdup (data + 4, size - 4);
865 wav->notes = g_list_append (wav->notes, note);
871 * gst_wavparse_smpl_chunk:
872 * @wav GstWavParse object
873 * @data holder for data
874 * @size holder for data size
876 * Parse smpl chunk from @data.
878 * Returns: %TRUE when cue chunk is available
881 gst_wavparse_smpl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
886 manufacturer_id = GST_READ_UINT32_LE (data);
887 product_id = GST_READ_UINT32_LE (data + 4);
888 sample_period = GST_READ_UINT32_LE (data + 8);
890 note_number = GST_READ_UINT32_LE (data + 12);
892 pitch_fraction = GST_READ_UINT32_LE (data + 16);
893 SMPTE_format = GST_READ_UINT32_LE (data + 20);
894 SMPTE_offset = GST_READ_UINT32_LE (data + 24);
895 num_sample_loops = GST_READ_UINT32_LE (data + 28);
896 List of Sample Loops, 24 bytes each
900 wav->tags = gst_tag_list_new_empty ();
901 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
902 GST_TAG_MIDI_BASE_NOTE, (guint) note_number, NULL);
907 * gst_wavparse_adtl_chunk:
908 * @wav GstWavParse object
909 * @data holder for data
910 * @size holder for data size
912 * Parse adtl from @data.
914 * Returns: %TRUE when adtl chunk is available
917 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
919 guint32 ltag, lsize, offset = 0;
922 ltag = GST_READ_UINT32_LE (data + offset);
923 lsize = GST_READ_UINT32_LE (data + offset + 4);
925 if (lsize + 8 > size) {
926 GST_WARNING_OBJECT (wav, "Invalid adtl size: %u + 8 > %u", lsize, size);
931 case GST_RIFF_TAG_labl:
932 gst_wavparse_labl_chunk (wav, data + offset, size);
934 case GST_RIFF_TAG_note:
935 gst_wavparse_note_chunk (wav, data + offset, size);
938 GST_WARNING_OBJECT (wav, "Unknowm adtl %" GST_FOURCC_FORMAT,
939 GST_FOURCC_ARGS (ltag));
940 GST_MEMDUMP_OBJECT (wav, "Unknowm adtl", &data[offset], lsize);
943 offset += 8 + GST_ROUND_UP_2 (lsize);
944 size -= 8 + GST_ROUND_UP_2 (lsize);
951 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
953 GstTagList *tags = NULL;
954 GstTocEntry *entry = NULL;
956 entry = gst_toc_find_entry (toc, id);
958 tags = gst_toc_entry_get_tags (entry);
960 tags = gst_tag_list_new_empty ();
961 gst_toc_entry_set_tags (entry, tags);
969 * gst_wavparse_create_toc:
970 * @wav GstWavParse object
972 * Create TOC from wav->cues and wav->labls.
975 gst_wavparse_create_toc (GstWavParse * wav)
981 GstWavParseLabl *labl;
982 GstWavParseNote *note;
985 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
987 GST_OBJECT_LOCK (wav);
989 GST_OBJECT_UNLOCK (wav);
990 GST_WARNING_OBJECT (wav, "found another TOC");
995 GST_OBJECT_UNLOCK (wav);
999 /* FIXME: send CURRENT scope toc too */
1000 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
1002 /* add cue edition */
1003 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
1004 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
1005 gst_toc_append_entry (toc, entry);
1007 /* add tracks in cue edition */
1011 prev_subentry = cur_subentry;
1012 /* previous track stop time = current track start time */
1013 if (prev_subentry != NULL) {
1014 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
1015 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1016 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
1018 id = g_strdup_printf ("%08x", cue->id);
1019 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
1021 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1022 stop = wav->duration;
1023 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
1024 gst_toc_entry_append_sub_entry (entry, cur_subentry);
1025 list = g_list_next (list);
1028 /* add tags in tracks */
1032 id = g_strdup_printf ("%08x", labl->cue_point_id);
1033 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1036 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1039 list = g_list_next (list);
1044 id = g_strdup_printf ("%08x", note->cue_point_id);
1045 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1048 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1051 list = g_list_next (list);
1054 /* send data as TOC */
1057 /* send TOC event */
1059 GST_OBJECT_UNLOCK (wav);
1060 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1066 #define MAX_BUFFER_SIZE 4096
1069 parse_ds64 (GstWavParse * wav, GstBuffer * buf)
1072 guint32 dataSizeLow, dataSizeHigh;
1073 guint32 sampleCountLow, sampleCountHigh;
1075 gst_buffer_map (buf, &map, GST_MAP_READ);
1076 dataSizeLow = GST_READ_UINT32_LE (map.data + 2 * 4);
1077 dataSizeHigh = GST_READ_UINT32_LE (map.data + 3 * 4);
1078 sampleCountLow = GST_READ_UINT32_LE (map.data + 4 * 4);
1079 sampleCountHigh = GST_READ_UINT32_LE (map.data + 5 * 4);
1080 gst_buffer_unmap (buf, &map);
1081 if (dataSizeHigh != 0xFFFFFFFF && dataSizeLow != 0xFFFFFFFF) {
1082 wav->datasize = ((guint64) dataSizeHigh << 32) | dataSizeLow;
1084 if (sampleCountHigh != 0xFFFFFFFF && sampleCountLow != 0xFFFFFFFF) {
1085 wav->fact = ((guint64) sampleCountHigh << 32) | sampleCountLow;
1088 GST_DEBUG_OBJECT (wav, "Got 'ds64' TAG, datasize : %" G_GINT64_FORMAT
1089 " fact: %" G_GINT64_FORMAT, wav->datasize, wav->fact);
1093 static GstFlowReturn
1094 gst_wavparse_stream_headers (GstWavParse * wav)
1096 GstFlowReturn res = GST_FLOW_OK;
1097 GstBuffer *buf = NULL;
1098 gst_riff_strf_auds *header = NULL;
1100 gboolean gotdata = FALSE;
1101 GstCaps *caps = NULL;
1102 gchar *codec_name = NULL;
1103 gint64 upstream_size = 0;
1106 /* search for "_fmt" chunk, which must be before "data" */
1107 while (!wav->got_fmt) {
1110 if (wav->streaming) {
1111 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1114 gst_adapter_flush (wav->adapter, 8);
1118 buf = gst_adapter_take_buffer (wav->adapter, size);
1120 gst_adapter_flush (wav->adapter, 1);
1121 wav->offset += GST_ROUND_UP_2 (size);
1123 buf = gst_buffer_new ();
1126 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1127 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1131 if (tag == GST_RS64_TAG_DS64) {
1132 if (!parse_ds64 (wav, buf))
1138 if (tag != GST_RIFF_TAG_fmt) {
1139 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1140 GST_FOURCC_ARGS (tag));
1141 gst_buffer_unref (buf);
1146 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1148 goto parse_header_error;
1150 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1152 /* do sanity checks of header fields */
1153 if (header->channels == 0)
1155 if (header->rate == 0)
1158 GST_DEBUG_OBJECT (wav, "creating the caps");
1160 /* Note: gst_riff_create_audio_caps might need to fix values in
1161 * the header header depending on the format, so call it first */
1162 /* FIXME: Need to handle the channel reorder map */
1163 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1164 NULL, &codec_name, NULL);
1167 gst_buffer_unref (extra);
1170 goto unknown_format;
1172 /* If we got raw audio from upstream, we remove the codec_data field,
1173 * which may have been added if the wav header included an extended
1174 * chunk. We want to keep it for non raw audio.
1176 s = gst_caps_get_structure (caps, 0);
1177 if (s && gst_structure_has_name (s, "audio/x-raw")) {
1178 gst_structure_remove_field (s, "codec_data");
1181 /* do more sanity checks of header fields
1182 * (these can be sanitized by gst_riff_create_audio_caps()
1184 wav->format = header->format;
1185 wav->rate = header->rate;
1186 wav->channels = header->channels;
1187 wav->blockalign = header->blockalign;
1188 wav->depth = header->bits_per_sample;
1189 wav->av_bps = header->av_bps;
1195 /* do format specific handling */
1196 switch (wav->format) {
1197 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1198 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1200 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1201 * bitrate inside the mpeg stream */
1202 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1206 case GST_RIFF_WAVE_FORMAT_PCM:
1207 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1208 goto invalid_blockalign;
1211 if (wav->av_bps > wav->blockalign * wav->rate)
1213 /* use the configured bps */
1214 wav->bps = wav->av_bps;
1218 wav->width = (wav->blockalign * 8) / wav->channels;
1219 wav->bytes_per_sample = wav->channels * wav->width / 8;
1221 if (wav->bytes_per_sample <= 0)
1222 goto no_bytes_per_sample;
1224 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1225 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1226 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1227 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1228 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1229 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1230 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1232 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1233 * formats). This will make the element output a BYTE format segment and
1234 * will not timestamp the outgoing buffers.
1236 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1238 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1240 /* create pad later so we can sniff the first few bytes
1241 * of the real data and correct our caps if necessary */
1242 gst_caps_replace (&wav->caps, caps);
1243 gst_caps_replace (&caps, NULL);
1245 wav->got_fmt = TRUE;
1247 if (wav->tags == NULL)
1248 wav->tags = gst_tag_list_new_empty ();
1251 GstCaps *templ_caps = gst_pad_get_pad_template_caps (wav->sinkpad);
1252 gst_pb_utils_add_codec_description_to_tag_list (wav->tags,
1253 GST_TAG_CONTAINER_FORMAT, templ_caps);
1254 gst_caps_unref (templ_caps);
1257 /* If bps is nonzero, then we do have a valid bitrate that can be
1258 * announced in a tag list. */
1260 guint bitrate = wav->bps * 8;
1261 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1262 GST_TAG_BITRATE, bitrate, NULL);
1266 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1267 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1269 g_free (codec_name);
1275 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1276 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1278 /* loop headers until we get data */
1280 if (wav->streaming) {
1281 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1288 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1289 &buf)) != GST_FLOW_OK)
1290 goto header_read_error;
1291 gst_buffer_map (buf, &map, GST_MAP_READ);
1292 tag = GST_READ_UINT32_LE (map.data);
1293 size = GST_READ_UINT32_LE (map.data + 4);
1294 gst_buffer_unmap (buf, &map);
1297 GST_INFO_OBJECT (wav,
1298 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT ", size %"
1299 G_GUINT32_FORMAT, GST_FOURCC_ARGS (tag), wav->offset, size);
1301 /* Maximum valid size is INT_MAX */
1302 if (size & 0x80000000) {
1303 GST_WARNING_OBJECT (wav, "Invalid size, clipping to 0x7fffffff");
1307 /* Clip to upstream size if known */
1308 if (upstream_size > 0 && size + wav->offset > upstream_size) {
1309 GST_WARNING_OBJECT (wav, "Clipping chunk size to file size");
1310 g_assert (upstream_size >= wav->offset);
1311 size = upstream_size - wav->offset;
1314 /* wav is a st00pid format, we don't know for sure where data starts.
1315 * So we have to go bit by bit until we find the 'data' header
1318 case GST_RIFF_TAG_data:{
1321 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1323 if (wav->ignore_length) {
1324 GST_DEBUG_OBJECT (wav, "Ignoring length");
1327 if (wav->streaming) {
1328 gst_adapter_flush (wav->adapter, 8);
1331 gst_buffer_unref (buf);
1334 wav->datastart = wav->offset;
1335 /* use size from ds64 chunk if available */
1336 if (size64 == -1 && wav->datasize > 0) {
1337 GST_DEBUG_OBJECT (wav, "Using ds64 datasize");
1338 size64 = wav->datasize;
1340 wav->chunk_size = size64;
1342 /* If size is zero, then the data chunk probably actually extends to
1343 the end of the file */
1344 if (size64 == 0 && upstream_size) {
1345 size64 = upstream_size - wav->datastart;
1347 /* Or the file might be truncated */
1348 else if (upstream_size) {
1349 size64 = MIN (size64, (upstream_size - wav->datastart));
1351 wav->datasize = size64;
1352 wav->dataleft = size64;
1353 wav->end_offset = size64 + wav->datastart;
1354 if (!wav->streaming) {
1355 /* We will continue parsing tags 'till end */
1356 wav->offset += size64;
1358 GST_DEBUG_OBJECT (wav, "datasize = %" G_GUINT64_FORMAT, size64);
1361 case GST_RIFF_TAG_fact:{
1362 if (wav->fact == 0 &&
1363 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1364 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1365 const guint data_size = 4;
1367 GST_INFO_OBJECT (wav, "Have fact 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 /* number of samples (for compressed formats) */
1378 if (wav->streaming) {
1379 const guint8 *data = NULL;
1381 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1384 gst_adapter_flush (wav->adapter, 8);
1385 data = gst_adapter_map (wav->adapter, data_size);
1386 wav->fact = GST_READ_UINT32_LE (data);
1387 gst_adapter_unmap (wav->adapter);
1388 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1390 gst_buffer_unref (buf);
1393 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1394 data_size, &buf)) != GST_FLOW_OK)
1395 goto header_read_error;
1396 gst_buffer_extract (buf, 0, &wav->fact, 4);
1397 wav->fact = GUINT32_FROM_LE (wav->fact);
1398 gst_buffer_unref (buf);
1400 GST_DEBUG_OBJECT (wav, "have fact %" G_GUINT64_FORMAT, wav->fact);
1401 wav->offset += 8 + GST_ROUND_UP_2 (size);
1404 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1405 /* need more data */
1411 case GST_RIFF_TAG_acid:{
1412 const gst_riff_acid *acid = NULL;
1413 const guint data_size = sizeof (gst_riff_acid);
1416 GST_INFO_OBJECT (wav, "Have acid chunk");
1417 if (size < data_size) {
1418 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1419 /* need more data */
1422 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1426 if (wav->streaming) {
1427 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1430 gst_adapter_flush (wav->adapter, 8);
1431 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1433 tempo = acid->tempo;
1434 gst_adapter_unmap (wav->adapter);
1437 gst_buffer_unref (buf);
1440 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1441 size, &buf)) != GST_FLOW_OK)
1442 goto header_read_error;
1443 gst_buffer_map (buf, &map, GST_MAP_READ);
1444 acid = (const gst_riff_acid *) map.data;
1445 tempo = acid->tempo;
1446 gst_buffer_unmap (buf, &map);
1448 /* send data as tags */
1450 wav->tags = gst_tag_list_new_empty ();
1451 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1452 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1454 size = GST_ROUND_UP_2 (size);
1455 if (wav->streaming) {
1456 gst_adapter_flush (wav->adapter, size);
1458 gst_buffer_unref (buf);
1460 wav->offset += 8 + size;
1463 /* FIXME: all list tags after data are ignored in streaming mode */
1464 case GST_RIFF_TAG_LIST:{
1467 if (wav->streaming) {
1468 const guint8 *data = NULL;
1470 if (gst_adapter_available (wav->adapter) < 12) {
1473 data = gst_adapter_map (wav->adapter, 12);
1474 ltag = GST_READ_UINT32_LE (data + 8);
1475 gst_adapter_unmap (wav->adapter);
1477 gst_buffer_unref (buf);
1480 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1481 &buf)) != GST_FLOW_OK)
1482 goto header_read_error;
1483 gst_buffer_extract (buf, 8, <ag, 4);
1484 ltag = GUINT32_FROM_LE (ltag);
1487 case GST_RIFF_LIST_INFO:{
1488 const gint data_size = size - 4;
1491 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1492 if (wav->streaming) {
1493 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1496 gst_adapter_flush (wav->adapter, 12);
1498 if (data_size > 0) {
1499 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1501 gst_adapter_flush (wav->adapter, 1);
1505 gst_buffer_unref (buf);
1507 if (data_size > 0) {
1509 gst_pad_pull_range (wav->sinkpad, wav->offset,
1510 data_size, &buf)) != GST_FLOW_OK)
1511 goto header_read_error;
1514 if (data_size > 0) {
1516 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1518 GstTagList *old = wav->tags;
1520 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1522 gst_tag_list_unref (old);
1523 gst_tag_list_unref (new);
1525 gst_buffer_unref (buf);
1526 wav->offset += GST_ROUND_UP_2 (data_size);
1530 case GST_RIFF_LIST_adtl:{
1531 const gint data_size = size - 4;
1533 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1534 if (wav->streaming) {
1535 const guint8 *data = NULL;
1537 gst_adapter_flush (wav->adapter, 12);
1539 data = gst_adapter_map (wav->adapter, data_size);
1540 gst_wavparse_adtl_chunk (wav, data, data_size);
1541 gst_adapter_unmap (wav->adapter);
1545 gst_buffer_unref (buf);
1549 gst_pad_pull_range (wav->sinkpad, wav->offset,
1550 data_size, &buf)) != GST_FLOW_OK)
1551 goto header_read_error;
1552 gst_buffer_map (buf, &map, GST_MAP_READ);
1553 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1555 gst_buffer_unmap (buf, &map);
1557 wav->offset += GST_ROUND_UP_2 (data_size);
1561 GST_WARNING_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1562 GST_FOURCC_ARGS (ltag));
1563 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1564 /* need more data */
1570 case GST_RIFF_TAG_cue:{
1571 const guint data_size = size;
1573 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1574 if (wav->streaming) {
1575 const guint8 *data = NULL;
1577 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1580 gst_adapter_flush (wav->adapter, 8);
1582 data = gst_adapter_map (wav->adapter, data_size);
1583 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1584 goto header_read_error;
1586 gst_adapter_unmap (wav->adapter);
1591 gst_buffer_unref (buf);
1594 gst_pad_pull_range (wav->sinkpad, wav->offset,
1595 data_size, &buf)) != GST_FLOW_OK)
1596 goto header_read_error;
1597 gst_buffer_map (buf, &map, GST_MAP_READ);
1598 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1600 goto header_read_error;
1602 gst_buffer_unmap (buf, &map);
1604 size = GST_ROUND_UP_2 (size);
1605 if (wav->streaming) {
1606 gst_adapter_flush (wav->adapter, size);
1608 gst_buffer_unref (buf);
1610 size = GST_ROUND_UP_2 (size);
1611 wav->offset += size;
1614 case GST_RIFF_TAG_smpl:{
1615 const gint data_size = size;
1617 GST_DEBUG_OBJECT (wav, "Have 'smpl' TAG, size : %u", data_size);
1618 if (wav->streaming) {
1619 const guint8 *data = NULL;
1621 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1624 gst_adapter_flush (wav->adapter, 8);
1626 data = gst_adapter_map (wav->adapter, data_size);
1627 if (!gst_wavparse_smpl_chunk (wav, data, data_size)) {
1628 goto header_read_error;
1630 gst_adapter_unmap (wav->adapter);
1635 gst_buffer_unref (buf);
1638 gst_pad_pull_range (wav->sinkpad, wav->offset,
1639 data_size, &buf)) != GST_FLOW_OK)
1640 goto header_read_error;
1641 gst_buffer_map (buf, &map, GST_MAP_READ);
1642 if (!gst_wavparse_smpl_chunk (wav, (const guint8 *) map.data,
1644 goto header_read_error;
1646 gst_buffer_unmap (buf, &map);
1648 size = GST_ROUND_UP_2 (size);
1649 if (wav->streaming) {
1650 gst_adapter_flush (wav->adapter, size);
1652 gst_buffer_unref (buf);
1654 size = GST_ROUND_UP_2 (size);
1655 wav->offset += size;
1659 GST_WARNING_OBJECT (wav, "Ignoring chunk %" GST_FOURCC_FORMAT,
1660 GST_FOURCC_ARGS (tag));
1661 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1662 /* need more data */
1667 if (upstream_size && (wav->offset >= upstream_size)) {
1668 /* Now we are gone through the whole file */
1673 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1675 if (wav->bps <= 0 && wav->fact) {
1677 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1679 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1680 (guint64) wav->fact);
1681 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1686 if (gst_wavparse_calculate_duration (wav)) {
1687 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1688 if (!wav->ignore_length)
1689 wav->segment.duration = wav->duration;
1691 gst_wavparse_create_toc (wav);
1693 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1694 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1695 if (!wav->ignore_length)
1696 wav->segment.duration = wav->datasize;
1699 /* now we have all the info to perform a pending seek if any, if no
1700 * event, this will still do the right thing and it will also send
1701 * the right newsegment event downstream. */
1702 gst_wavparse_perform_seek (wav, wav->seek_event);
1703 /* remove pending event */
1704 gst_event_replace (&wav->seek_event, NULL);
1706 /* we just started, we are discont */
1707 wav->discont = TRUE;
1709 wav->state = GST_WAVPARSE_DATA;
1711 /* determine reasonable max buffer size,
1712 * that is, buffers not too small either size or time wise
1713 * so we do not end up with too many of them */
1715 if (gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size))
1716 wav->max_buf_size = upstream_size;
1718 wav->max_buf_size = 0;
1719 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1720 if (wav->blockalign > 0)
1721 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1723 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1730 g_free (codec_name);
1733 gst_caps_unref (caps);
1738 res = GST_FLOW_ERROR;
1743 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1744 ("Couldn't parse audio header"));
1749 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1750 ("Stream claims to contain no channels - invalid data"));
1755 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1756 ("Stream with sample_rate == 0 - invalid data"));
1761 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1762 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1763 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1768 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1769 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1770 wav->av_bps, wav->blockalign * wav->rate));
1773 no_bytes_per_sample:
1775 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1776 ("Could not caluclate bytes per sample - invalid data"));
1781 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1782 ("No caps found for format 0x%x, %u channels, %u Hz",
1783 wav->format, wav->channels, wav->rate));
1788 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1789 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1795 * Read WAV file tag when streaming
1797 static GstFlowReturn
1798 gst_wavparse_parse_stream_init (GstWavParse * wav)
1800 if (gst_adapter_available (wav->adapter) >= 12) {
1803 /* _take flushes the data */
1804 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1806 GST_DEBUG ("Parsing wav header");
1807 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1808 return GST_FLOW_ERROR;
1811 /* Go to next state */
1812 wav->state = GST_WAVPARSE_HEADER;
1817 /* handle an event sent directly to the element.
1819 * This event can be sent either in the READY state or the
1820 * >READY state. The only event of interest really is the seek
1823 * In the READY state we can only store the event and try to
1824 * respect it when going to PAUSED. We assume we are in the
1825 * READY state when our parsing state != GST_WAVPARSE_DATA.
1827 * When we are steaming, we can simply perform the seek right
1831 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1833 GstWavParse *wav = GST_WAVPARSE (element);
1834 gboolean res = FALSE;
1836 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1838 switch (GST_EVENT_TYPE (event)) {
1839 case GST_EVENT_SEEK:
1840 if (wav->state == GST_WAVPARSE_DATA) {
1841 /* we can handle the seek directly when streaming data */
1842 res = gst_wavparse_perform_seek (wav, event);
1844 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1846 gst_event_replace (&wav->seek_event, event);
1848 /* we always return true */
1855 gst_event_unref (event);
1860 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1864 s = gst_caps_get_structure (caps, 0);
1865 if (!gst_structure_has_name (s, "audio/x-dts"))
1867 /* typefind behavior for DTS:
1868 * MAXIMUM: multiple frame syncs detected, certainly DTS
1869 * LIKELY: single frame sync at offset 0. Maybe DTS?
1870 * POSSIBLE: single frame sync, not at offset 0. Highly unlikely
1872 if (prob > GST_TYPE_FIND_LIKELY)
1874 if (prob <= GST_TYPE_FIND_POSSIBLE)
1876 /* for maybe, check for at least a valid-looking rate and channels */
1877 if (!gst_structure_has_field (s, "channels"))
1879 /* and for extra assurance we could also check the rate from the DTS frame
1880 * against the one in the wav header, but for now let's not do that */
1881 return gst_structure_has_field (s, "rate");
1885 gst_wavparse_get_upstream_tags (GstWavParse * wav, GstTagScope scope)
1887 GstTagList *tags = NULL;
1892 while ((ev = gst_pad_get_sticky_event (wav->sinkpad, GST_EVENT_TAG, i++))) {
1893 gst_event_parse_tag (ev, &tags);
1894 if (tags != NULL && gst_tag_list_get_scope (tags) == scope) {
1895 tags = gst_tag_list_copy (tags);
1896 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1897 gst_event_unref (ev);
1901 gst_event_unref (ev);
1907 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1910 GstTagList *tags, *utags;
1912 GST_DEBUG_OBJECT (wav, "adding src pad");
1914 g_assert (wav->caps != NULL);
1916 s = gst_caps_get_structure (wav->caps, 0);
1917 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1918 GstTypeFindProbability prob;
1921 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1922 if (tf_caps != NULL) {
1923 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1924 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1925 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1926 gst_caps_unref (wav->caps);
1927 wav->caps = tf_caps;
1929 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1930 GST_TAG_AUDIO_CODEC, "dts", NULL);
1932 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1933 "marked as raw PCM audio, but ignoring for now", tf_caps);
1934 gst_caps_unref (tf_caps);
1939 gst_pad_set_caps (wav->srcpad, wav->caps);
1941 if (wav->start_segment) {
1942 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1943 gst_pad_push_event (wav->srcpad, wav->start_segment);
1944 wav->start_segment = NULL;
1947 /* upstream tags, e.g. from id3/ape tag before the wav file; assume for now
1948 * that there'll be only one scope/type of tag list from upstream, if any */
1949 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_GLOBAL);
1951 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_STREAM);
1953 /* if there's a tag upstream it's probably been added to override the
1954 * tags from inside the wav header, so keep upstream tags if in doubt */
1955 tags = gst_tag_list_merge (utags, wav->tags, GST_TAG_MERGE_KEEP);
1957 if (wav->tags != NULL) {
1958 gst_tag_list_unref (wav->tags);
1963 gst_tag_list_unref (utags);
1965 /* send tags downstream, if any */
1967 gst_pad_push_event (wav->srcpad, gst_event_new_tag (tags));
1970 static GstFlowReturn
1971 gst_wavparse_stream_data (GstWavParse * wav)
1973 GstBuffer *buf = NULL;
1974 GstFlowReturn res = GST_FLOW_OK;
1975 guint64 desired, obtained;
1976 GstClockTime timestamp, next_timestamp, duration;
1977 guint64 pos, nextpos;
1980 GST_LOG_OBJECT (wav,
1981 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1982 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1984 if ((wav->dataleft == 0 || wav->dataleft < wav->blockalign)) {
1985 /* In case chunk size is not declared in the begining get size from the
1986 * file size directly */
1987 if (wav->chunk_size == 0) {
1988 gint64 upstream_size = 0;
1990 /* Get the size of the file */
1991 if (!gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES,
1995 if (upstream_size < wav->offset + wav->datastart)
1998 /* If file has updated since the beggining continue reading the file */
1999 wav->dataleft = upstream_size - wav->offset - wav->datastart;
2000 wav->end_offset = upstream_size;
2002 /* Get the next n bytes and output them, if we can */
2003 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
2010 /* scale the amount of data by the segment rate so we get equal
2011 * amounts of data regardless of the playback rate */
2013 MIN (gst_guint64_to_gdouble (wav->dataleft),
2014 wav->max_buf_size * ABS (wav->segment.rate));
2016 if (desired >= wav->blockalign && wav->blockalign > 0)
2017 desired -= (desired % wav->blockalign);
2019 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
2020 "from the sinkpad", desired);
2022 if (wav->streaming) {
2023 guint avail = gst_adapter_available (wav->adapter);
2026 /* flush some bytes if evil upstream sends segment that starts
2027 * before data or does is not send sample aligned segment */
2028 if (G_LIKELY (wav->offset >= wav->datastart)) {
2029 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
2031 extra = wav->datastart - wav->offset;
2034 if (G_UNLIKELY (extra)) {
2035 extra = wav->bytes_per_sample - extra;
2036 if (extra <= avail) {
2037 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
2038 gst_adapter_flush (wav->adapter, extra);
2039 wav->offset += extra;
2040 wav->dataleft -= extra;
2041 goto iterate_adapter;
2043 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
2044 gst_adapter_clear (wav->adapter);
2045 wav->offset += avail;
2046 wav->dataleft -= avail;
2051 if (avail < desired) {
2052 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
2056 buf = gst_adapter_take_buffer (wav->adapter, desired);
2058 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
2059 desired, &buf)) != GST_FLOW_OK)
2062 /* we may get a short buffer at the end of the file */
2063 if (gst_buffer_get_size (buf) < desired) {
2064 gsize size = gst_buffer_get_size (buf);
2066 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2067 if (size >= wav->blockalign) {
2068 if (wav->blockalign > 0) {
2069 buf = gst_buffer_make_writable (buf);
2070 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2073 gst_buffer_unref (buf);
2079 obtained = gst_buffer_get_size (buf);
2081 /* our positions in bytes */
2082 pos = wav->offset - wav->datastart;
2083 nextpos = pos + obtained;
2085 /* update offsets, does not overflow. */
2086 buf = gst_buffer_make_writable (buf);
2087 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2088 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2090 /* first chunk of data? create the source pad. We do this only here so
2091 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2092 if (G_UNLIKELY (wav->first)) {
2094 /* this will also push the segment events */
2095 gst_wavparse_add_src_pad (wav, buf);
2097 /* If we have a pending start segment, send it now. */
2098 if (G_UNLIKELY (wav->start_segment != NULL)) {
2099 gst_pad_push_event (wav->srcpad, wav->start_segment);
2100 wav->start_segment = NULL;
2105 /* and timestamps if we have a bitrate, be careful for overflows */
2107 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2109 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2110 duration = next_timestamp - timestamp;
2112 /* update current running segment position */
2113 if (G_LIKELY (next_timestamp >= wav->segment.start))
2114 wav->segment.position = next_timestamp;
2115 } else if (wav->fact) {
2117 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2118 /* and timestamps if we have a bitrate, be careful for overflows */
2119 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2120 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2121 duration = next_timestamp - timestamp;
2123 /* no bitrate, all we know is that the first sample has timestamp 0, all
2124 * other positions and durations have unknown timestamp. */
2128 timestamp = GST_CLOCK_TIME_NONE;
2129 duration = GST_CLOCK_TIME_NONE;
2130 /* update current running segment position with byte offset */
2131 if (G_LIKELY (nextpos >= wav->segment.start))
2132 wav->segment.position = nextpos;
2134 if ((pos > 0) && wav->vbr) {
2135 /* don't set timestamps for VBR files if it's not the first buffer */
2136 timestamp = GST_CLOCK_TIME_NONE;
2137 duration = GST_CLOCK_TIME_NONE;
2140 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2141 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2142 wav->discont = FALSE;
2145 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2146 GST_BUFFER_DURATION (buf) = duration;
2148 GST_LOG_OBJECT (wav,
2149 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2150 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2151 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2153 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2156 if (obtained < wav->dataleft) {
2157 wav->offset += obtained;
2158 wav->dataleft -= obtained;
2160 wav->offset += wav->dataleft;
2164 /* Iterate until need more data, so adapter size won't grow */
2165 if (wav->streaming) {
2166 GST_LOG_OBJECT (wav,
2167 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2169 goto iterate_adapter;
2176 GST_DEBUG_OBJECT (wav, "found EOS");
2177 return GST_FLOW_EOS;
2181 /* check if we got EOS */
2182 if (res == GST_FLOW_EOS)
2185 GST_WARNING_OBJECT (wav,
2186 "Error getting %" G_GINT64_FORMAT " bytes from the "
2187 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2192 GST_INFO_OBJECT (wav,
2193 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2194 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2195 gst_pad_is_linked (wav->srcpad));
2201 gst_wavparse_loop (GstPad * pad)
2204 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2208 GST_LOG_OBJECT (wav, "process data");
2210 switch (wav->state) {
2211 case GST_WAVPARSE_START:
2212 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2213 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2217 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2218 event = gst_event_new_stream_start (stream_id);
2219 gst_event_set_group_id (event, gst_util_group_id_next ());
2220 gst_pad_push_event (wav->srcpad, event);
2223 wav->state = GST_WAVPARSE_HEADER;
2226 case GST_WAVPARSE_HEADER:
2227 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2228 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2231 wav->state = GST_WAVPARSE_DATA;
2232 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2235 case GST_WAVPARSE_DATA:
2236 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2240 g_assert_not_reached ();
2247 const gchar *reason = gst_flow_get_name (ret);
2249 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2250 gst_pad_pause_task (pad);
2252 if (ret == GST_FLOW_EOS) {
2253 /* handle end-of-stream/segment */
2254 /* so align our position with the end of it, if there is one
2255 * this ensures a subsequent will arrive at correct base/acc time */
2256 if (wav->segment.format == GST_FORMAT_TIME) {
2257 if (wav->segment.rate > 0.0 &&
2258 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2259 wav->segment.position = wav->segment.stop;
2260 else if (wav->segment.rate < 0.0)
2261 wav->segment.position = wav->segment.start;
2263 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2264 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2265 ("No valid input found before end of stream"));
2266 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2268 /* add pad before we perform EOS */
2269 if (G_UNLIKELY (wav->first)) {
2271 gst_wavparse_add_src_pad (wav, NULL);
2274 /* perform EOS logic */
2275 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2278 if ((stop = wav->segment.stop) == -1)
2279 stop = wav->segment.duration;
2281 gst_element_post_message (GST_ELEMENT_CAST (wav),
2282 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2283 wav->segment.format, stop));
2284 gst_pad_push_event (wav->srcpad,
2285 gst_event_new_segment_done (wav->segment.format, stop));
2287 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2290 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2291 /* for fatal errors we post an error message, post the error
2292 * first so the app knows about the error first. */
2293 GST_ELEMENT_FLOW_ERROR (wav, ret);
2294 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2300 static GstFlowReturn
2301 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2304 GstWavParse *wav = GST_WAVPARSE (parent);
2306 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2307 gst_buffer_get_size (buf));
2309 gst_adapter_push (wav->adapter, buf);
2311 switch (wav->state) {
2312 case GST_WAVPARSE_START:
2313 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2314 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2317 if (wav->state != GST_WAVPARSE_HEADER)
2320 /* otherwise fall-through */
2321 case GST_WAVPARSE_HEADER:
2322 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2323 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2326 if (!wav->got_fmt || wav->datastart == 0)
2329 wav->state = GST_WAVPARSE_DATA;
2330 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2333 case GST_WAVPARSE_DATA:
2334 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2335 wav->discont = TRUE;
2336 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2340 g_return_val_if_reached (GST_FLOW_ERROR);
2343 if (G_UNLIKELY (wav->abort_buffering)) {
2344 wav->abort_buffering = FALSE;
2345 ret = GST_FLOW_ERROR;
2346 /* sort of demux/parse error */
2347 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2353 static GstFlowReturn
2354 gst_wavparse_flush_data (GstWavParse * wav)
2356 GstFlowReturn ret = GST_FLOW_OK;
2359 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2360 ret = gst_wavparse_stream_data (wav);
2367 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2369 GstWavParse *wav = GST_WAVPARSE (parent);
2370 gboolean ret = TRUE;
2372 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2374 switch (GST_EVENT_TYPE (event)) {
2375 case GST_EVENT_CAPS:
2377 /* discard, we'll come up with proper src caps */
2378 gst_event_unref (event);
2381 case GST_EVENT_SEGMENT:
2383 gint64 start, stop, offset = 0, end_offset = -1;
2386 /* some debug output */
2387 gst_event_copy_segment (event, &segment);
2388 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2391 if (wav->state != GST_WAVPARSE_DATA) {
2392 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2396 /* now we are either committed to TIME or BYTE format,
2397 * and we only expect a BYTE segment, e.g. following a seek */
2398 if (segment.format == GST_FORMAT_BYTES) {
2399 /* handle (un)signed issues */
2400 start = segment.start;
2401 stop = segment.stop;
2404 start -= wav->datastart;
2405 start = MAX (start, 0);
2409 stop -= wav->datastart;
2410 stop = MAX (stop, 0);
2412 if (wav->segment.format == GST_FORMAT_TIME) {
2413 guint64 bps = wav->bps;
2415 /* operating in format TIME, so we can convert */
2416 if (!bps && wav->fact)
2418 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2422 gst_util_uint64_scale_ceil (start, GST_SECOND,
2423 (guint64) wav->bps);
2426 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2427 (guint64) wav->bps);
2431 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2435 segment.start = start;
2436 segment.stop = stop;
2438 /* accept upstream's notion of segment and distribute along */
2439 segment.format = wav->segment.format;
2440 segment.time = segment.position = segment.start;
2441 segment.duration = wav->segment.duration;
2442 segment.base = gst_segment_to_running_time (&wav->segment,
2443 GST_FORMAT_TIME, wav->segment.position);
2445 gst_segment_copy_into (&segment, &wav->segment);
2447 /* also store the newsegment event for the streaming thread */
2448 if (wav->start_segment)
2449 gst_event_unref (wav->start_segment);
2450 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2451 wav->start_segment = gst_event_new_segment (&segment);
2453 /* stream leftover data in current segment */
2454 gst_wavparse_flush_data (wav);
2455 /* and set up streaming thread for next one */
2456 wav->offset = offset;
2457 wav->end_offset = end_offset;
2459 if (wav->datasize > 0 && (wav->end_offset == -1
2460 || wav->end_offset > wav->datastart + wav->datasize))
2461 wav->end_offset = wav->datastart + wav->datasize;
2463 if (wav->end_offset != -1) {
2464 wav->dataleft = wav->end_offset - wav->offset;
2466 /* infinity; upstream will EOS when done */
2467 wav->dataleft = G_MAXUINT64;
2470 gst_event_unref (event);
2474 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2475 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2476 ("No valid input found before end of stream"));
2478 /* add pad if needed so EOS is seen downstream */
2479 if (G_UNLIKELY (wav->first)) {
2481 gst_wavparse_add_src_pad (wav, NULL);
2483 /* stream leftover data in current segment */
2484 gst_wavparse_flush_data (wav);
2489 case GST_EVENT_FLUSH_STOP:
2494 gst_adapter_clear (wav->adapter);
2495 wav->discont = TRUE;
2496 dur = wav->segment.duration;
2497 gst_segment_init (&wav->segment, wav->segment.format);
2498 wav->segment.duration = dur;
2502 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2510 /* convert and query stuff */
2511 static const GstFormat *
2512 gst_wavparse_get_formats (GstPad * pad)
2514 static const GstFormat formats[] = {
2517 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2526 gst_wavparse_pad_convert (GstPad * pad,
2527 GstFormat src_format, gint64 src_value,
2528 GstFormat * dest_format, gint64 * dest_value)
2530 GstWavParse *wavparse;
2531 gboolean res = TRUE;
2533 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2535 if (*dest_format == src_format) {
2536 *dest_value = src_value;
2540 if ((wavparse->bps == 0) && !wavparse->fact)
2543 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2544 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2546 switch (src_format) {
2547 case GST_FORMAT_BYTES:
2548 switch (*dest_format) {
2549 case GST_FORMAT_DEFAULT:
2550 *dest_value = src_value / wavparse->bytes_per_sample;
2551 /* make sure we end up on a sample boundary */
2552 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2554 case GST_FORMAT_TIME:
2555 /* src_value + datastart = offset */
2556 GST_INFO_OBJECT (wavparse,
2557 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2559 if (wavparse->bps > 0)
2560 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2561 (guint64) wavparse->bps);
2562 else if (wavparse->fact) {
2563 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2564 wavparse->rate, wavparse->fact);
2567 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2578 case GST_FORMAT_DEFAULT:
2579 switch (*dest_format) {
2580 case GST_FORMAT_BYTES:
2581 *dest_value = src_value * wavparse->bytes_per_sample;
2583 case GST_FORMAT_TIME:
2584 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2585 (guint64) wavparse->rate);
2593 case GST_FORMAT_TIME:
2594 switch (*dest_format) {
2595 case GST_FORMAT_BYTES:
2596 if (wavparse->bps > 0)
2597 *dest_value = gst_util_uint64_scale (src_value,
2598 (guint64) wavparse->bps, GST_SECOND);
2600 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2601 wavparse->rate, wavparse->fact);
2603 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2605 /* make sure we end up on a sample boundary */
2606 *dest_value -= *dest_value % wavparse->blockalign;
2608 case GST_FORMAT_DEFAULT:
2609 *dest_value = gst_util_uint64_scale (src_value,
2610 (guint64) wavparse->rate, GST_SECOND);
2629 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2635 /* handle queries for location and length in requested format */
2637 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2639 gboolean res = TRUE;
2640 GstWavParse *wav = GST_WAVPARSE (parent);
2642 /* only if we know */
2643 if (wav->state != GST_WAVPARSE_DATA) {
2647 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2649 switch (GST_QUERY_TYPE (query)) {
2650 case GST_QUERY_POSITION:
2656 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2657 curb = wav->offset - wav->datastart;
2658 gst_query_parse_position (query, &format, NULL);
2659 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2662 case GST_FORMAT_BYTES:
2663 format = GST_FORMAT_BYTES;
2667 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2672 gst_query_set_position (query, format, cur);
2675 case GST_QUERY_DURATION:
2677 gint64 duration = 0;
2680 if (wav->ignore_length) {
2685 gst_query_parse_duration (query, &format, NULL);
2688 case GST_FORMAT_BYTES:{
2689 format = GST_FORMAT_BYTES;
2690 duration = wav->datasize;
2693 case GST_FORMAT_TIME:
2694 if ((res = gst_wavparse_calculate_duration (wav))) {
2695 duration = wav->duration;
2703 gst_query_set_duration (query, format, duration);
2706 case GST_QUERY_CONVERT:
2708 gint64 srcvalue, dstvalue;
2709 GstFormat srcformat, dstformat;
2711 gst_query_parse_convert (query, &srcformat, &srcvalue,
2712 &dstformat, &dstvalue);
2713 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2714 &dstformat, &dstvalue);
2716 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2719 case GST_QUERY_SEEKING:{
2721 gboolean seekable = FALSE;
2723 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2724 if (fmt == wav->segment.format) {
2725 if (wav->streaming) {
2728 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2729 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2730 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2731 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2733 gst_query_unref (q);
2735 GST_LOG_OBJECT (wav, "looping => seekable");
2739 } else if (fmt == GST_FORMAT_TIME) {
2743 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2748 res = gst_pad_query_default (pad, parent, query);
2755 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2757 GstWavParse *wavparse = GST_WAVPARSE (parent);
2758 gboolean res = FALSE;
2760 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2762 switch (GST_EVENT_TYPE (event)) {
2763 case GST_EVENT_SEEK:
2764 /* can only handle events when we are in the data state */
2765 if (wavparse->state == GST_WAVPARSE_DATA) {
2766 res = gst_wavparse_perform_seek (wavparse, event);
2768 gst_event_unref (event);
2771 case GST_EVENT_TOC_SELECT:
2774 GstTocEntry *entry = NULL;
2775 GstEvent *seek_event;
2778 if (!wavparse->toc) {
2779 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2782 gst_event_parse_toc_select (event, &uid);
2784 GST_OBJECT_LOCK (wavparse);
2785 entry = gst_toc_find_entry (wavparse->toc, uid);
2786 if (entry == NULL) {
2787 GST_OBJECT_UNLOCK (wavparse);
2788 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2792 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2793 GST_OBJECT_UNLOCK (wavparse);
2794 seek_event = gst_event_new_seek (1.0,
2796 GST_SEEK_FLAG_FLUSH,
2797 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2798 res = gst_wavparse_perform_seek (wavparse, seek_event);
2799 gst_event_unref (seek_event);
2803 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2807 gst_event_unref (event);
2812 res = gst_pad_push_event (wavparse->sinkpad, event);
2819 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2821 GstWavParse *wav = GST_WAVPARSE (parent);
2826 gst_adapter_clear (wav->adapter);
2827 g_object_unref (wav->adapter);
2828 wav->adapter = NULL;
2831 query = gst_query_new_scheduling ();
2833 if (!gst_pad_peer_query (sinkpad, query)) {
2834 gst_query_unref (query);
2838 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2839 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2840 gst_query_unref (query);
2845 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2846 wav->streaming = FALSE;
2847 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2851 GST_DEBUG_OBJECT (sinkpad, "activating push");
2852 wav->streaming = TRUE;
2853 wav->adapter = gst_adapter_new ();
2854 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2860 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2861 GstPadMode mode, gboolean active)
2866 case GST_PAD_MODE_PUSH:
2869 case GST_PAD_MODE_PULL:
2871 /* if we have a scheduler we can start the task */
2872 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2875 res = gst_pad_stop_task (sinkpad);
2885 static GstStateChangeReturn
2886 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2888 GstStateChangeReturn ret;
2889 GstWavParse *wav = GST_WAVPARSE (element);
2891 switch (transition) {
2892 case GST_STATE_CHANGE_NULL_TO_READY:
2894 case GST_STATE_CHANGE_READY_TO_PAUSED:
2895 gst_wavparse_reset (wav);
2897 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2903 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2905 switch (transition) {
2906 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2908 case GST_STATE_CHANGE_PAUSED_TO_READY:
2909 gst_wavparse_reset (wav);
2911 case GST_STATE_CHANGE_READY_TO_NULL:
2920 gst_wavparse_set_property (GObject * object, guint prop_id,
2921 const GValue * value, GParamSpec * pspec)
2925 g_return_if_fail (GST_IS_WAVPARSE (object));
2926 self = GST_WAVPARSE (object);
2929 case PROP_IGNORE_LENGTH:
2930 self->ignore_length = g_value_get_boolean (value);
2933 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2939 gst_wavparse_get_property (GObject * object, guint prop_id,
2940 GValue * value, GParamSpec * pspec)
2944 g_return_if_fail (GST_IS_WAVPARSE (object));
2945 self = GST_WAVPARSE (object);
2948 case PROP_IGNORE_LENGTH:
2949 g_value_set_boolean (value, self->ignore_length);
2952 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2957 plugin_init (GstPlugin * plugin)
2961 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2965 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2968 "Parse a .wav file into raw audio",
2969 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)