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, };
393 guint32 seqnum = GST_SEQNUM_INVALID;
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 if (seqnum != GST_SEQNUM_INVALID)
467 gst_event_set_seqnum (event, seqnum);
468 res = gst_pad_push_event (wav->sinkpad, event);
474 flush = flags & GST_SEEK_FLAG_FLUSH;
476 /* now we need to make sure the streaming thread is stopped. We do this by
477 * either sending a FLUSH_START event downstream which will cause the
478 * streaming thread to stop with a WRONG_STATE.
479 * For a non-flushing seek we simply pause the task, which will happen as soon
480 * as it completes one iteration (and thus might block when the sink is
481 * blocking in preroll). */
484 GST_DEBUG_OBJECT (wav, "sending flush start");
486 fevent = gst_event_new_flush_start ();
487 if (seqnum != GST_SEQNUM_INVALID)
488 gst_event_set_seqnum (fevent, seqnum);
489 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
490 gst_pad_push_event (wav->srcpad, fevent);
492 gst_pad_pause_task (wav->sinkpad);
495 /* we should now be able to grab the streaming thread because we stopped it
496 * with the above flush/pause code */
497 GST_PAD_STREAM_LOCK (wav->sinkpad);
499 /* save current position */
500 last_stop = wav->segment.position;
502 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
504 /* copy segment, we need this because we still need the old
505 * segment when we close the current segment. */
506 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
508 /* configure the seek parameters in the seeksegment. We will then have the
509 * right values in the segment to perform the seek */
511 GST_DEBUG_OBJECT (wav, "configuring seek");
512 gst_segment_do_seek (&seeksegment, rate, format, flags,
513 cur_type, cur, stop_type, stop, &update);
516 /* figure out the last position we need to play. If it's configured (stop !=
517 * -1), use that, else we play until the total duration of the file */
518 if ((stop = seeksegment.stop) == -1)
519 stop = seeksegment.duration;
521 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
522 if ((cur_type != GST_SEEK_TYPE_NONE)) {
523 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
524 * we can just copy the last_stop. If not, we use the bps to convert TIME to
526 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
527 (gint64 *) & wav->offset))
528 wav->offset = seeksegment.position;
529 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
530 wav->offset -= (wav->offset % wav->bytes_per_sample);
531 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
532 wav->offset += wav->datastart;
533 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
535 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
539 if (stop_type != GST_SEEK_TYPE_NONE) {
540 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
541 wav->end_offset = stop;
542 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
543 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
544 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
545 wav->end_offset += wav->datastart;
546 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
548 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
552 /* make sure filesize is not exceeded due to rounding errors or so,
553 * same precaution as in _stream_headers */
554 bformat = GST_FORMAT_BYTES;
555 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
556 wav->end_offset = MIN (wav->end_offset, upstream_size);
558 if (wav->datasize > 0 && wav->end_offset > wav->datastart + wav->datasize)
559 wav->end_offset = wav->datastart + wav->datasize;
561 /* this is the range of bytes we will use for playback */
562 wav->offset = MIN (wav->offset, wav->end_offset);
563 wav->dataleft = wav->end_offset - wav->offset;
565 GST_DEBUG_OBJECT (wav,
566 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
567 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
568 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
570 /* prepare for streaming again */
574 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
575 GST_DEBUG_OBJECT (wav, "sending flush stop");
577 fevent = gst_event_new_flush_stop (TRUE);
578 if (seqnum != GST_SEQNUM_INVALID)
579 gst_event_set_seqnum (fevent, seqnum);
580 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
581 gst_pad_push_event (wav->srcpad, fevent);
584 /* now we did the seek and can activate the new segment values */
585 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
587 /* if we're doing a segment seek, post a SEGMENT_START message */
588 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
589 gst_element_post_message (GST_ELEMENT_CAST (wav),
590 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
591 wav->segment.format, wav->segment.position));
594 /* now create the newsegment */
595 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
596 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
598 /* store the newsegment event so it can be sent from the streaming thread. */
599 if (wav->start_segment)
600 gst_event_unref (wav->start_segment);
601 wav->start_segment = gst_event_new_segment (&wav->segment);
602 if (seqnum != GST_SEQNUM_INVALID)
603 gst_event_set_seqnum (wav->start_segment, seqnum);
605 /* mark discont if we are going to stream from another position. */
606 if (last_stop != wav->segment.position) {
607 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
611 /* and start the streaming task again */
612 if (!wav->streaming) {
613 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
617 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
624 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
629 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
634 GST_DEBUG_OBJECT (wav,
635 "Could not determine byte position for desired time");
641 * gst_wavparse_peek_chunk_info:
642 * @wav Wavparse object
643 * @tag holder for tag
644 * @size holder for tag size
646 * Peek next chunk info (tag and size)
648 * Returns: %TRUE when the chunk info (header) is available
651 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
653 const guint8 *data = NULL;
655 if (gst_adapter_available (wav->adapter) < 8)
658 data = gst_adapter_map (wav->adapter, 8);
659 *tag = GST_READ_UINT32_LE (data);
660 *size = GST_READ_UINT32_LE (data + 4);
661 gst_adapter_unmap (wav->adapter);
663 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
664 GST_FOURCC_ARGS (*tag));
670 * gst_wavparse_peek_chunk:
671 * @wav Wavparse object
672 * @tag holder for tag
673 * @size holder for tag size
675 * Peek enough data for one full chunk
677 * Returns: %TRUE when the full chunk is available
680 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
682 guint32 peek_size = 0;
685 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
688 /* size 0 -> empty data buffer would surprise most callers,
689 * large size -> do not bother trying to squeeze that into adapter,
690 * so we throw poor man's exception, which can be caught if caller really
691 * wants to handle 0 size chunk */
692 if (!(*size) || (*size) >= (1 << 30)) {
693 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
694 *size, GST_FOURCC_ARGS (*tag));
695 /* chain should give up */
696 wav->abort_buffering = TRUE;
699 peek_size = (*size + 1) & ~1;
700 available = gst_adapter_available (wav->adapter);
702 if (available >= (8 + peek_size)) {
705 GST_LOG ("but only %u bytes available now", available);
711 * gst_wavparse_calculate_duration:
712 * @wav: wavparse object
714 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
717 * Returns: %TRUE if duration is available.
720 gst_wavparse_calculate_duration (GstWavParse * wav)
722 if (wav->duration > 0)
726 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
728 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
730 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
731 GST_TIME_ARGS (wav->duration));
733 } else if (wav->fact) {
735 gst_util_uint64_scale_ceil (GST_SECOND, wav->fact, wav->rate);
736 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
737 GST_TIME_ARGS (wav->duration));
744 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
749 if (wav->streaming) {
750 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
753 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
754 GST_FOURCC_ARGS (tag));
755 flush = 8 + ((size + 1) & ~1);
756 wav->offset += flush;
757 if (wav->streaming) {
758 gst_adapter_flush (wav->adapter, flush);
760 gst_buffer_unref (buf);
767 * gst_wavparse_cue_chunk:
768 * @wav GstWavParse object
769 * @data holder for data
770 * @size holder for data size
772 * Parse cue chunk from @data to wav->cues.
774 * Returns: %TRUE when cue chunk is available
777 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
784 GST_WARNING_OBJECT (wav, "found another cue's");
788 ncues = GST_READ_UINT32_LE (data);
790 if (size < 4 + ncues * 24) {
791 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
797 for (i = 0; i < ncues; i++) {
798 cue = g_new0 (GstWavParseCue, 1);
799 cue->id = GST_READ_UINT32_LE (data);
800 cue->position = GST_READ_UINT32_LE (data + 4);
801 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
802 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
803 cue->block_start = GST_READ_UINT32_LE (data + 16);
804 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
805 cues = g_list_append (cues, cue);
815 * gst_wavparse_labl_chunk:
816 * @wav GstWavParse object
817 * @data holder for data
818 * @size holder for data size
820 * Parse labl from @data to wav->labls.
822 * Returns: %TRUE when labl chunk is available
825 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
827 GstWavParseLabl *labl;
832 labl = g_new0 (GstWavParseLabl, 1);
836 labl->cue_point_id = GST_READ_UINT32_LE (data);
837 labl->text = g_memdup (data + 4, size - 4);
839 wav->labls = g_list_append (wav->labls, labl);
845 * gst_wavparse_note_chunk:
846 * @wav GstWavParse object
847 * @data holder for data
848 * @size holder for data size
850 * Parse note from @data to wav->notes.
852 * Returns: %TRUE when note chunk is available
855 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
857 GstWavParseNote *note;
862 note = g_new0 (GstWavParseNote, 1);
866 note->cue_point_id = GST_READ_UINT32_LE (data);
867 note->text = g_memdup (data + 4, size - 4);
869 wav->notes = g_list_append (wav->notes, note);
875 * gst_wavparse_smpl_chunk:
876 * @wav GstWavParse object
877 * @data holder for data
878 * @size holder for data size
880 * Parse smpl chunk from @data.
882 * Returns: %TRUE when cue chunk is available
885 gst_wavparse_smpl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
890 manufacturer_id = GST_READ_UINT32_LE (data);
891 product_id = GST_READ_UINT32_LE (data + 4);
892 sample_period = GST_READ_UINT32_LE (data + 8);
894 note_number = GST_READ_UINT32_LE (data + 12);
896 pitch_fraction = GST_READ_UINT32_LE (data + 16);
897 SMPTE_format = GST_READ_UINT32_LE (data + 20);
898 SMPTE_offset = GST_READ_UINT32_LE (data + 24);
899 num_sample_loops = GST_READ_UINT32_LE (data + 28);
900 List of Sample Loops, 24 bytes each
904 wav->tags = gst_tag_list_new_empty ();
905 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
906 GST_TAG_MIDI_BASE_NOTE, (guint) note_number, NULL);
911 * gst_wavparse_adtl_chunk:
912 * @wav GstWavParse object
913 * @data holder for data
914 * @size holder for data size
916 * Parse adtl from @data.
918 * Returns: %TRUE when adtl chunk is available
921 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
923 guint32 ltag, lsize, offset = 0;
926 ltag = GST_READ_UINT32_LE (data + offset);
927 lsize = GST_READ_UINT32_LE (data + offset + 4);
929 if (lsize + 8 > size) {
930 GST_WARNING_OBJECT (wav, "Invalid adtl size: %u + 8 > %u", lsize, size);
935 case GST_RIFF_TAG_labl:
936 gst_wavparse_labl_chunk (wav, data + offset, size);
938 case GST_RIFF_TAG_note:
939 gst_wavparse_note_chunk (wav, data + offset, size);
942 GST_WARNING_OBJECT (wav, "Unknowm adtl %" GST_FOURCC_FORMAT,
943 GST_FOURCC_ARGS (ltag));
944 GST_MEMDUMP_OBJECT (wav, "Unknowm adtl", &data[offset], lsize);
947 offset += 8 + GST_ROUND_UP_2 (lsize);
948 size -= 8 + GST_ROUND_UP_2 (lsize);
955 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
957 GstTagList *tags = NULL;
958 GstTocEntry *entry = NULL;
960 entry = gst_toc_find_entry (toc, id);
962 tags = gst_toc_entry_get_tags (entry);
964 tags = gst_tag_list_new_empty ();
965 gst_toc_entry_set_tags (entry, tags);
973 * gst_wavparse_create_toc:
974 * @wav GstWavParse object
976 * Create TOC from wav->cues and wav->labls.
979 gst_wavparse_create_toc (GstWavParse * wav)
985 GstWavParseLabl *labl;
986 GstWavParseNote *note;
989 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
991 GST_OBJECT_LOCK (wav);
993 GST_OBJECT_UNLOCK (wav);
994 GST_WARNING_OBJECT (wav, "found another TOC");
999 GST_OBJECT_UNLOCK (wav);
1003 /* FIXME: send CURRENT scope toc too */
1004 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
1006 /* add cue edition */
1007 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
1008 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
1009 gst_toc_append_entry (toc, entry);
1011 /* add tracks in cue edition */
1015 prev_subentry = cur_subentry;
1016 /* previous track stop time = current track start time */
1017 if (prev_subentry != NULL) {
1018 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
1019 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1020 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
1022 id = g_strdup_printf ("%08x", cue->id);
1023 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
1025 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1026 stop = wav->duration;
1027 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
1028 gst_toc_entry_append_sub_entry (entry, cur_subentry);
1029 list = g_list_next (list);
1032 /* add tags in tracks */
1036 id = g_strdup_printf ("%08x", labl->cue_point_id);
1037 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1040 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1043 list = g_list_next (list);
1048 id = g_strdup_printf ("%08x", note->cue_point_id);
1049 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1052 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1055 list = g_list_next (list);
1058 /* send data as TOC */
1061 /* send TOC event */
1063 GST_OBJECT_UNLOCK (wav);
1064 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1070 #define MAX_BUFFER_SIZE 4096
1073 parse_ds64 (GstWavParse * wav, GstBuffer * buf)
1076 guint32 dataSizeLow, dataSizeHigh;
1077 guint32 sampleCountLow, sampleCountHigh;
1079 gst_buffer_map (buf, &map, GST_MAP_READ);
1080 dataSizeLow = GST_READ_UINT32_LE (map.data + 2 * 4);
1081 dataSizeHigh = GST_READ_UINT32_LE (map.data + 3 * 4);
1082 sampleCountLow = GST_READ_UINT32_LE (map.data + 4 * 4);
1083 sampleCountHigh = GST_READ_UINT32_LE (map.data + 5 * 4);
1084 gst_buffer_unmap (buf, &map);
1085 if (dataSizeHigh != 0xFFFFFFFF && dataSizeLow != 0xFFFFFFFF) {
1086 wav->datasize = ((guint64) dataSizeHigh << 32) | dataSizeLow;
1088 if (sampleCountHigh != 0xFFFFFFFF && sampleCountLow != 0xFFFFFFFF) {
1089 wav->fact = ((guint64) sampleCountHigh << 32) | sampleCountLow;
1092 GST_DEBUG_OBJECT (wav, "Got 'ds64' TAG, datasize : %" G_GINT64_FORMAT
1093 " fact: %" G_GINT64_FORMAT, wav->datasize, wav->fact);
1097 static GstFlowReturn
1098 gst_wavparse_stream_headers (GstWavParse * wav)
1100 GstFlowReturn res = GST_FLOW_OK;
1101 GstBuffer *buf = NULL;
1102 gst_riff_strf_auds *header = NULL;
1104 gboolean gotdata = FALSE;
1105 GstCaps *caps = NULL;
1106 gchar *codec_name = NULL;
1107 gint64 upstream_size = 0;
1110 /* search for "_fmt" chunk, which must be before "data" */
1111 while (!wav->got_fmt) {
1114 if (wav->streaming) {
1115 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1118 gst_adapter_flush (wav->adapter, 8);
1122 buf = gst_adapter_take_buffer (wav->adapter, size);
1124 gst_adapter_flush (wav->adapter, 1);
1125 wav->offset += GST_ROUND_UP_2 (size);
1127 buf = gst_buffer_new ();
1130 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1131 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1135 if (tag == GST_RS64_TAG_DS64) {
1136 if (!parse_ds64 (wav, buf))
1142 if (tag != GST_RIFF_TAG_fmt) {
1143 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1144 GST_FOURCC_ARGS (tag));
1145 gst_buffer_unref (buf);
1150 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1152 goto parse_header_error;
1154 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1156 /* do sanity checks of header fields */
1157 if (header->channels == 0)
1159 if (header->rate == 0)
1162 GST_DEBUG_OBJECT (wav, "creating the caps");
1164 /* Note: gst_riff_create_audio_caps might need to fix values in
1165 * the header header depending on the format, so call it first */
1166 /* FIXME: Need to handle the channel reorder map */
1167 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1168 NULL, &codec_name, NULL);
1171 gst_buffer_unref (extra);
1174 goto unknown_format;
1176 /* If we got raw audio from upstream, we remove the codec_data field,
1177 * which may have been added if the wav header included an extended
1178 * chunk. We want to keep it for non raw audio.
1180 s = gst_caps_get_structure (caps, 0);
1181 if (s && gst_structure_has_name (s, "audio/x-raw")) {
1182 gst_structure_remove_field (s, "codec_data");
1185 /* do more sanity checks of header fields
1186 * (these can be sanitized by gst_riff_create_audio_caps()
1188 wav->format = header->format;
1189 wav->rate = header->rate;
1190 wav->channels = header->channels;
1191 wav->blockalign = header->blockalign;
1192 wav->depth = header->bits_per_sample;
1193 wav->av_bps = header->av_bps;
1199 /* do format specific handling */
1200 switch (wav->format) {
1201 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1202 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1204 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1205 * bitrate inside the mpeg stream */
1206 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1210 case GST_RIFF_WAVE_FORMAT_PCM:
1211 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1212 goto invalid_blockalign;
1215 if (wav->av_bps > wav->blockalign * wav->rate)
1217 /* use the configured bps */
1218 wav->bps = wav->av_bps;
1222 wav->width = (wav->blockalign * 8) / wav->channels;
1223 wav->bytes_per_sample = wav->channels * wav->width / 8;
1225 if (wav->bytes_per_sample <= 0)
1226 goto no_bytes_per_sample;
1228 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1229 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1230 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1231 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1232 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1233 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1234 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1236 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1237 * formats). This will make the element output a BYTE format segment and
1238 * will not timestamp the outgoing buffers.
1240 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1242 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1244 /* create pad later so we can sniff the first few bytes
1245 * of the real data and correct our caps if necessary */
1246 gst_caps_replace (&wav->caps, caps);
1247 gst_caps_replace (&caps, NULL);
1249 wav->got_fmt = TRUE;
1251 if (wav->tags == NULL)
1252 wav->tags = gst_tag_list_new_empty ();
1255 GstCaps *templ_caps = gst_pad_get_pad_template_caps (wav->sinkpad);
1256 gst_pb_utils_add_codec_description_to_tag_list (wav->tags,
1257 GST_TAG_CONTAINER_FORMAT, templ_caps);
1258 gst_caps_unref (templ_caps);
1261 /* If bps is nonzero, then we do have a valid bitrate that can be
1262 * announced in a tag list. */
1264 guint bitrate = wav->bps * 8;
1265 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1266 GST_TAG_BITRATE, bitrate, NULL);
1270 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1271 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1273 g_free (codec_name);
1279 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1280 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1282 /* loop headers until we get data */
1284 if (wav->streaming) {
1285 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1292 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1293 &buf)) != GST_FLOW_OK)
1294 goto header_read_error;
1295 gst_buffer_map (buf, &map, GST_MAP_READ);
1296 tag = GST_READ_UINT32_LE (map.data);
1297 size = GST_READ_UINT32_LE (map.data + 4);
1298 gst_buffer_unmap (buf, &map);
1301 GST_INFO_OBJECT (wav,
1302 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT ", size %"
1303 G_GUINT32_FORMAT, GST_FOURCC_ARGS (tag), wav->offset, size);
1305 /* Maximum valid size is INT_MAX */
1306 if (size & 0x80000000) {
1307 GST_WARNING_OBJECT (wav, "Invalid size, clipping to 0x7fffffff");
1311 /* Clip to upstream size if known */
1312 if (upstream_size > 0 && size + wav->offset > upstream_size) {
1313 GST_WARNING_OBJECT (wav, "Clipping chunk size to file size");
1314 g_assert (upstream_size >= wav->offset);
1315 size = upstream_size - wav->offset;
1318 /* wav is a st00pid format, we don't know for sure where data starts.
1319 * So we have to go bit by bit until we find the 'data' header
1322 case GST_RIFF_TAG_data:{
1325 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1327 if (wav->ignore_length) {
1328 GST_DEBUG_OBJECT (wav, "Ignoring length");
1331 if (wav->streaming) {
1332 gst_adapter_flush (wav->adapter, 8);
1335 gst_buffer_unref (buf);
1338 wav->datastart = wav->offset;
1339 /* use size from ds64 chunk if available */
1340 if (size64 == -1 && wav->datasize > 0) {
1341 GST_DEBUG_OBJECT (wav, "Using ds64 datasize");
1342 size64 = wav->datasize;
1344 wav->chunk_size = size64;
1346 /* If size is zero, then the data chunk probably actually extends to
1347 the end of the file */
1348 if (size64 == 0 && upstream_size) {
1349 size64 = upstream_size - wav->datastart;
1351 /* Or the file might be truncated */
1352 else if (upstream_size) {
1353 size64 = MIN (size64, (upstream_size - wav->datastart));
1355 wav->datasize = size64;
1356 wav->dataleft = size64;
1357 wav->end_offset = size64 + wav->datastart;
1358 if (!wav->streaming) {
1359 /* We will continue parsing tags 'till end */
1360 wav->offset += size64;
1362 GST_DEBUG_OBJECT (wav, "datasize = %" G_GUINT64_FORMAT, size64);
1365 case GST_RIFF_TAG_fact:{
1366 if (wav->fact == 0 &&
1367 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1368 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1369 const guint data_size = 4;
1371 GST_INFO_OBJECT (wav, "Have fact chunk");
1372 if (size < data_size) {
1373 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1374 /* need more data */
1377 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1381 /* number of samples (for compressed formats) */
1382 if (wav->streaming) {
1383 const guint8 *data = NULL;
1385 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1388 gst_adapter_flush (wav->adapter, 8);
1389 data = gst_adapter_map (wav->adapter, data_size);
1390 wav->fact = GST_READ_UINT32_LE (data);
1391 gst_adapter_unmap (wav->adapter);
1392 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1394 gst_buffer_unref (buf);
1397 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1398 data_size, &buf)) != GST_FLOW_OK)
1399 goto header_read_error;
1400 gst_buffer_extract (buf, 0, &wav->fact, 4);
1401 wav->fact = GUINT32_FROM_LE (wav->fact);
1402 gst_buffer_unref (buf);
1404 GST_DEBUG_OBJECT (wav, "have fact %" G_GUINT64_FORMAT, wav->fact);
1405 wav->offset += 8 + GST_ROUND_UP_2 (size);
1408 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1409 /* need more data */
1415 case GST_RIFF_TAG_acid:{
1416 const gst_riff_acid *acid = NULL;
1417 const guint data_size = sizeof (gst_riff_acid);
1420 GST_INFO_OBJECT (wav, "Have acid chunk");
1421 if (size < data_size) {
1422 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1423 /* need more data */
1426 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1430 if (wav->streaming) {
1431 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1434 gst_adapter_flush (wav->adapter, 8);
1435 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1437 tempo = acid->tempo;
1438 gst_adapter_unmap (wav->adapter);
1441 gst_buffer_unref (buf);
1444 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1445 size, &buf)) != GST_FLOW_OK)
1446 goto header_read_error;
1447 gst_buffer_map (buf, &map, GST_MAP_READ);
1448 acid = (const gst_riff_acid *) map.data;
1449 tempo = acid->tempo;
1450 gst_buffer_unmap (buf, &map);
1452 /* send data as tags */
1454 wav->tags = gst_tag_list_new_empty ();
1455 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1456 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1458 size = GST_ROUND_UP_2 (size);
1459 if (wav->streaming) {
1460 gst_adapter_flush (wav->adapter, size);
1462 gst_buffer_unref (buf);
1464 wav->offset += 8 + size;
1467 /* FIXME: all list tags after data are ignored in streaming mode */
1468 case GST_RIFF_TAG_LIST:{
1471 if (wav->streaming) {
1472 const guint8 *data = NULL;
1474 if (gst_adapter_available (wav->adapter) < 12) {
1477 data = gst_adapter_map (wav->adapter, 12);
1478 ltag = GST_READ_UINT32_LE (data + 8);
1479 gst_adapter_unmap (wav->adapter);
1481 gst_buffer_unref (buf);
1484 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1485 &buf)) != GST_FLOW_OK)
1486 goto header_read_error;
1487 gst_buffer_extract (buf, 8, <ag, 4);
1488 ltag = GUINT32_FROM_LE (ltag);
1491 case GST_RIFF_LIST_INFO:{
1492 const gint data_size = size - 4;
1495 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1496 if (wav->streaming) {
1497 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1500 gst_adapter_flush (wav->adapter, 12);
1502 if (data_size > 0) {
1503 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1505 gst_adapter_flush (wav->adapter, 1);
1509 gst_buffer_unref (buf);
1511 if (data_size > 0) {
1513 gst_pad_pull_range (wav->sinkpad, wav->offset,
1514 data_size, &buf)) != GST_FLOW_OK)
1515 goto header_read_error;
1518 if (data_size > 0) {
1520 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1522 GstTagList *old = wav->tags;
1524 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1526 gst_tag_list_unref (old);
1527 gst_tag_list_unref (new);
1529 gst_buffer_unref (buf);
1530 wav->offset += GST_ROUND_UP_2 (data_size);
1534 case GST_RIFF_LIST_adtl:{
1535 const gint data_size = size - 4;
1537 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1538 if (wav->streaming) {
1539 const guint8 *data = NULL;
1541 gst_adapter_flush (wav->adapter, 12);
1543 data = gst_adapter_map (wav->adapter, data_size);
1544 gst_wavparse_adtl_chunk (wav, data, data_size);
1545 gst_adapter_unmap (wav->adapter);
1549 gst_buffer_unref (buf);
1553 gst_pad_pull_range (wav->sinkpad, wav->offset,
1554 data_size, &buf)) != GST_FLOW_OK)
1555 goto header_read_error;
1556 gst_buffer_map (buf, &map, GST_MAP_READ);
1557 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1559 gst_buffer_unmap (buf, &map);
1561 wav->offset += GST_ROUND_UP_2 (data_size);
1565 GST_WARNING_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1566 GST_FOURCC_ARGS (ltag));
1567 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1568 /* need more data */
1574 case GST_RIFF_TAG_cue:{
1575 const guint data_size = size;
1577 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1578 if (wav->streaming) {
1579 const guint8 *data = NULL;
1581 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1584 gst_adapter_flush (wav->adapter, 8);
1586 data = gst_adapter_map (wav->adapter, data_size);
1587 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1588 goto header_read_error;
1590 gst_adapter_unmap (wav->adapter);
1595 gst_buffer_unref (buf);
1598 gst_pad_pull_range (wav->sinkpad, wav->offset,
1599 data_size, &buf)) != GST_FLOW_OK)
1600 goto header_read_error;
1601 gst_buffer_map (buf, &map, GST_MAP_READ);
1602 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1604 goto header_read_error;
1606 gst_buffer_unmap (buf, &map);
1608 size = GST_ROUND_UP_2 (size);
1609 if (wav->streaming) {
1610 gst_adapter_flush (wav->adapter, size);
1612 gst_buffer_unref (buf);
1614 size = GST_ROUND_UP_2 (size);
1615 wav->offset += size;
1618 case GST_RIFF_TAG_smpl:{
1619 const gint data_size = size;
1621 GST_DEBUG_OBJECT (wav, "Have 'smpl' TAG, size : %u", data_size);
1622 if (wav->streaming) {
1623 const guint8 *data = NULL;
1625 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1628 gst_adapter_flush (wav->adapter, 8);
1630 data = gst_adapter_map (wav->adapter, data_size);
1631 if (!gst_wavparse_smpl_chunk (wav, data, data_size)) {
1632 goto header_read_error;
1634 gst_adapter_unmap (wav->adapter);
1639 gst_buffer_unref (buf);
1642 gst_pad_pull_range (wav->sinkpad, wav->offset,
1643 data_size, &buf)) != GST_FLOW_OK)
1644 goto header_read_error;
1645 gst_buffer_map (buf, &map, GST_MAP_READ);
1646 if (!gst_wavparse_smpl_chunk (wav, (const guint8 *) map.data,
1648 goto header_read_error;
1650 gst_buffer_unmap (buf, &map);
1652 size = GST_ROUND_UP_2 (size);
1653 if (wav->streaming) {
1654 gst_adapter_flush (wav->adapter, size);
1656 gst_buffer_unref (buf);
1658 size = GST_ROUND_UP_2 (size);
1659 wav->offset += size;
1663 GST_WARNING_OBJECT (wav, "Ignoring chunk %" GST_FOURCC_FORMAT,
1664 GST_FOURCC_ARGS (tag));
1665 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1666 /* need more data */
1671 if (upstream_size && (wav->offset >= upstream_size)) {
1672 /* Now we are gone through the whole file */
1677 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1679 if (wav->bps <= 0 && wav->fact) {
1681 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1683 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1684 (guint64) wav->fact);
1685 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1690 if (gst_wavparse_calculate_duration (wav)) {
1691 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1692 if (!wav->ignore_length)
1693 wav->segment.duration = wav->duration;
1695 gst_wavparse_create_toc (wav);
1697 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1698 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1699 if (!wav->ignore_length)
1700 wav->segment.duration = wav->datasize;
1703 /* now we have all the info to perform a pending seek if any, if no
1704 * event, this will still do the right thing and it will also send
1705 * the right newsegment event downstream. */
1706 gst_wavparse_perform_seek (wav, wav->seek_event);
1707 /* remove pending event */
1708 gst_event_replace (&wav->seek_event, NULL);
1710 /* we just started, we are discont */
1711 wav->discont = TRUE;
1713 wav->state = GST_WAVPARSE_DATA;
1715 /* determine reasonable max buffer size,
1716 * that is, buffers not too small either size or time wise
1717 * so we do not end up with too many of them */
1719 if (gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size))
1720 wav->max_buf_size = upstream_size;
1722 wav->max_buf_size = 0;
1723 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1724 if (wav->blockalign > 0)
1725 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1727 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1734 g_free (codec_name);
1737 gst_caps_unref (caps);
1742 res = GST_FLOW_ERROR;
1747 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1748 ("Couldn't parse audio header"));
1753 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1754 ("Stream claims to contain no channels - invalid data"));
1759 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1760 ("Stream with sample_rate == 0 - invalid data"));
1765 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1766 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1767 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1772 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1773 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1774 wav->av_bps, wav->blockalign * wav->rate));
1777 no_bytes_per_sample:
1779 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1780 ("Could not caluclate bytes per sample - invalid data"));
1785 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1786 ("No caps found for format 0x%x, %u channels, %u Hz",
1787 wav->format, wav->channels, wav->rate));
1792 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1793 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1799 * Read WAV file tag when streaming
1801 static GstFlowReturn
1802 gst_wavparse_parse_stream_init (GstWavParse * wav)
1804 if (gst_adapter_available (wav->adapter) >= 12) {
1807 /* _take flushes the data */
1808 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1810 GST_DEBUG ("Parsing wav header");
1811 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1812 return GST_FLOW_ERROR;
1815 /* Go to next state */
1816 wav->state = GST_WAVPARSE_HEADER;
1821 /* handle an event sent directly to the element.
1823 * This event can be sent either in the READY state or the
1824 * >READY state. The only event of interest really is the seek
1827 * In the READY state we can only store the event and try to
1828 * respect it when going to PAUSED. We assume we are in the
1829 * READY state when our parsing state != GST_WAVPARSE_DATA.
1831 * When we are steaming, we can simply perform the seek right
1835 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1837 GstWavParse *wav = GST_WAVPARSE (element);
1838 gboolean res = FALSE;
1840 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1842 switch (GST_EVENT_TYPE (event)) {
1843 case GST_EVENT_SEEK:
1844 if (wav->state == GST_WAVPARSE_DATA) {
1845 /* we can handle the seek directly when streaming data */
1846 res = gst_wavparse_perform_seek (wav, event);
1848 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1850 gst_event_replace (&wav->seek_event, event);
1852 /* we always return true */
1859 gst_event_unref (event);
1864 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1868 s = gst_caps_get_structure (caps, 0);
1869 if (!gst_structure_has_name (s, "audio/x-dts"))
1871 /* typefind behavior for DTS:
1872 * MAXIMUM: multiple frame syncs detected, certainly DTS
1873 * LIKELY: single frame sync at offset 0. Maybe DTS?
1874 * POSSIBLE: single frame sync, not at offset 0. Highly unlikely
1876 if (prob > GST_TYPE_FIND_LIKELY)
1878 if (prob <= GST_TYPE_FIND_POSSIBLE)
1880 /* for maybe, check for at least a valid-looking rate and channels */
1881 if (!gst_structure_has_field (s, "channels"))
1883 /* and for extra assurance we could also check the rate from the DTS frame
1884 * against the one in the wav header, but for now let's not do that */
1885 return gst_structure_has_field (s, "rate");
1889 gst_wavparse_get_upstream_tags (GstWavParse * wav, GstTagScope scope)
1891 GstTagList *tags = NULL;
1896 while ((ev = gst_pad_get_sticky_event (wav->sinkpad, GST_EVENT_TAG, i++))) {
1897 gst_event_parse_tag (ev, &tags);
1898 if (tags != NULL && gst_tag_list_get_scope (tags) == scope) {
1899 tags = gst_tag_list_copy (tags);
1900 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1901 gst_event_unref (ev);
1905 gst_event_unref (ev);
1911 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1914 GstTagList *tags, *utags;
1916 GST_DEBUG_OBJECT (wav, "adding src pad");
1918 g_assert (wav->caps != NULL);
1920 s = gst_caps_get_structure (wav->caps, 0);
1921 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1922 GstTypeFindProbability prob;
1925 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1926 if (tf_caps != NULL) {
1927 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1928 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1929 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1930 gst_caps_unref (wav->caps);
1931 wav->caps = tf_caps;
1933 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1934 GST_TAG_AUDIO_CODEC, "dts", NULL);
1936 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1937 "marked as raw PCM audio, but ignoring for now", tf_caps);
1938 gst_caps_unref (tf_caps);
1943 gst_pad_set_caps (wav->srcpad, wav->caps);
1945 if (wav->start_segment) {
1946 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1947 gst_pad_push_event (wav->srcpad, wav->start_segment);
1948 wav->start_segment = NULL;
1951 /* upstream tags, e.g. from id3/ape tag before the wav file; assume for now
1952 * that there'll be only one scope/type of tag list from upstream, if any */
1953 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_GLOBAL);
1955 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_STREAM);
1957 /* if there's a tag upstream it's probably been added to override the
1958 * tags from inside the wav header, so keep upstream tags if in doubt */
1959 tags = gst_tag_list_merge (utags, wav->tags, GST_TAG_MERGE_KEEP);
1961 if (wav->tags != NULL) {
1962 gst_tag_list_unref (wav->tags);
1967 gst_tag_list_unref (utags);
1969 /* send tags downstream, if any */
1971 gst_pad_push_event (wav->srcpad, gst_event_new_tag (tags));
1974 static GstFlowReturn
1975 gst_wavparse_stream_data (GstWavParse * wav)
1977 GstBuffer *buf = NULL;
1978 GstFlowReturn res = GST_FLOW_OK;
1979 guint64 desired, obtained;
1980 GstClockTime timestamp, next_timestamp, duration;
1981 guint64 pos, nextpos;
1984 GST_LOG_OBJECT (wav,
1985 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1986 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1988 if ((wav->dataleft == 0 || wav->dataleft < wav->blockalign)) {
1989 /* In case chunk size is not declared in the begining get size from the
1990 * file size directly */
1991 if (wav->chunk_size == 0) {
1992 gint64 upstream_size = 0;
1994 /* Get the size of the file */
1995 if (!gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES,
1999 if (upstream_size < wav->offset + wav->datastart)
2002 /* If file has updated since the beggining continue reading the file */
2003 wav->dataleft = upstream_size - wav->offset - wav->datastart;
2004 wav->end_offset = upstream_size;
2006 /* Get the next n bytes and output them, if we can */
2007 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
2014 /* scale the amount of data by the segment rate so we get equal
2015 * amounts of data regardless of the playback rate */
2017 MIN (gst_guint64_to_gdouble (wav->dataleft),
2018 wav->max_buf_size * ABS (wav->segment.rate));
2020 if (desired >= wav->blockalign && wav->blockalign > 0)
2021 desired -= (desired % wav->blockalign);
2023 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
2024 "from the sinkpad", desired);
2026 if (wav->streaming) {
2027 guint avail = gst_adapter_available (wav->adapter);
2030 /* flush some bytes if evil upstream sends segment that starts
2031 * before data or does is not send sample aligned segment */
2032 if (G_LIKELY (wav->offset >= wav->datastart)) {
2033 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
2035 extra = wav->datastart - wav->offset;
2038 if (G_UNLIKELY (extra)) {
2039 extra = wav->bytes_per_sample - extra;
2040 if (extra <= avail) {
2041 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
2042 gst_adapter_flush (wav->adapter, extra);
2043 wav->offset += extra;
2044 wav->dataleft -= extra;
2045 goto iterate_adapter;
2047 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
2048 gst_adapter_clear (wav->adapter);
2049 wav->offset += avail;
2050 wav->dataleft -= avail;
2055 if (avail < desired) {
2056 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
2060 buf = gst_adapter_take_buffer (wav->adapter, desired);
2062 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
2063 desired, &buf)) != GST_FLOW_OK)
2066 /* we may get a short buffer at the end of the file */
2067 if (gst_buffer_get_size (buf) < desired) {
2068 gsize size = gst_buffer_get_size (buf);
2070 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2071 if (size >= wav->blockalign) {
2072 if (wav->blockalign > 0) {
2073 buf = gst_buffer_make_writable (buf);
2074 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2077 gst_buffer_unref (buf);
2083 obtained = gst_buffer_get_size (buf);
2085 /* our positions in bytes */
2086 pos = wav->offset - wav->datastart;
2087 nextpos = pos + obtained;
2089 /* update offsets, does not overflow. */
2090 buf = gst_buffer_make_writable (buf);
2091 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2092 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2094 /* first chunk of data? create the source pad. We do this only here so
2095 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2096 if (G_UNLIKELY (wav->first)) {
2098 /* this will also push the segment events */
2099 gst_wavparse_add_src_pad (wav, buf);
2101 /* If we have a pending start segment, send it now. */
2102 if (G_UNLIKELY (wav->start_segment != NULL)) {
2103 gst_pad_push_event (wav->srcpad, wav->start_segment);
2104 wav->start_segment = NULL;
2109 /* and timestamps if we have a bitrate, be careful for overflows */
2111 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2113 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2114 duration = next_timestamp - timestamp;
2116 /* update current running segment position */
2117 if (G_LIKELY (next_timestamp >= wav->segment.start))
2118 wav->segment.position = next_timestamp;
2119 } else if (wav->fact) {
2121 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2122 /* and timestamps if we have a bitrate, be careful for overflows */
2123 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2124 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2125 duration = next_timestamp - timestamp;
2127 /* no bitrate, all we know is that the first sample has timestamp 0, all
2128 * other positions and durations have unknown timestamp. */
2132 timestamp = GST_CLOCK_TIME_NONE;
2133 duration = GST_CLOCK_TIME_NONE;
2134 /* update current running segment position with byte offset */
2135 if (G_LIKELY (nextpos >= wav->segment.start))
2136 wav->segment.position = nextpos;
2138 if ((pos > 0) && wav->vbr) {
2139 /* don't set timestamps for VBR files if it's not the first buffer */
2140 timestamp = GST_CLOCK_TIME_NONE;
2141 duration = GST_CLOCK_TIME_NONE;
2144 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2145 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2146 wav->discont = FALSE;
2149 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2150 GST_BUFFER_DURATION (buf) = duration;
2152 GST_LOG_OBJECT (wav,
2153 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2154 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2155 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2157 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2160 if (obtained < wav->dataleft) {
2161 wav->offset += obtained;
2162 wav->dataleft -= obtained;
2164 wav->offset += wav->dataleft;
2168 /* Iterate until need more data, so adapter size won't grow */
2169 if (wav->streaming) {
2170 GST_LOG_OBJECT (wav,
2171 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2173 goto iterate_adapter;
2180 GST_DEBUG_OBJECT (wav, "found EOS");
2181 return GST_FLOW_EOS;
2185 /* check if we got EOS */
2186 if (res == GST_FLOW_EOS)
2189 GST_WARNING_OBJECT (wav,
2190 "Error getting %" G_GINT64_FORMAT " bytes from the "
2191 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2196 GST_INFO_OBJECT (wav,
2197 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2198 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2199 gst_pad_is_linked (wav->srcpad));
2205 gst_wavparse_loop (GstPad * pad)
2208 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2212 GST_LOG_OBJECT (wav, "process data");
2214 switch (wav->state) {
2215 case GST_WAVPARSE_START:
2216 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2217 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2221 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2222 event = gst_event_new_stream_start (stream_id);
2223 gst_event_set_group_id (event, gst_util_group_id_next ());
2224 gst_pad_push_event (wav->srcpad, event);
2227 wav->state = GST_WAVPARSE_HEADER;
2230 case GST_WAVPARSE_HEADER:
2231 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2232 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2235 wav->state = GST_WAVPARSE_DATA;
2236 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2239 case GST_WAVPARSE_DATA:
2240 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2244 g_assert_not_reached ();
2251 const gchar *reason = gst_flow_get_name (ret);
2253 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2254 gst_pad_pause_task (pad);
2256 if (ret == GST_FLOW_EOS) {
2257 /* handle end-of-stream/segment */
2258 /* so align our position with the end of it, if there is one
2259 * this ensures a subsequent will arrive at correct base/acc time */
2260 if (wav->segment.format == GST_FORMAT_TIME) {
2261 if (wav->segment.rate > 0.0 &&
2262 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2263 wav->segment.position = wav->segment.stop;
2264 else if (wav->segment.rate < 0.0)
2265 wav->segment.position = wav->segment.start;
2267 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2268 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2269 ("No valid input found before end of stream"));
2270 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2272 /* add pad before we perform EOS */
2273 if (G_UNLIKELY (wav->first)) {
2275 gst_wavparse_add_src_pad (wav, NULL);
2278 /* perform EOS logic */
2279 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2282 if ((stop = wav->segment.stop) == -1)
2283 stop = wav->segment.duration;
2285 gst_element_post_message (GST_ELEMENT_CAST (wav),
2286 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2287 wav->segment.format, stop));
2288 gst_pad_push_event (wav->srcpad,
2289 gst_event_new_segment_done (wav->segment.format, stop));
2291 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2294 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2295 /* for fatal errors we post an error message, post the error
2296 * first so the app knows about the error first. */
2297 GST_ELEMENT_FLOW_ERROR (wav, ret);
2298 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2304 static GstFlowReturn
2305 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2308 GstWavParse *wav = GST_WAVPARSE (parent);
2310 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2311 gst_buffer_get_size (buf));
2313 gst_adapter_push (wav->adapter, buf);
2315 switch (wav->state) {
2316 case GST_WAVPARSE_START:
2317 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2318 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2321 if (wav->state != GST_WAVPARSE_HEADER)
2324 /* otherwise fall-through */
2325 case GST_WAVPARSE_HEADER:
2326 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2327 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2330 if (!wav->got_fmt || wav->datastart == 0)
2333 wav->state = GST_WAVPARSE_DATA;
2334 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2337 case GST_WAVPARSE_DATA:
2338 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2339 wav->discont = TRUE;
2340 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2344 g_return_val_if_reached (GST_FLOW_ERROR);
2347 if (G_UNLIKELY (wav->abort_buffering)) {
2348 wav->abort_buffering = FALSE;
2349 ret = GST_FLOW_ERROR;
2350 /* sort of demux/parse error */
2351 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2357 static GstFlowReturn
2358 gst_wavparse_flush_data (GstWavParse * wav)
2360 GstFlowReturn ret = GST_FLOW_OK;
2363 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2364 ret = gst_wavparse_stream_data (wav);
2371 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2373 GstWavParse *wav = GST_WAVPARSE (parent);
2374 gboolean ret = TRUE;
2376 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2378 switch (GST_EVENT_TYPE (event)) {
2379 case GST_EVENT_CAPS:
2381 /* discard, we'll come up with proper src caps */
2382 gst_event_unref (event);
2385 case GST_EVENT_SEGMENT:
2387 gint64 start, stop, offset = 0, end_offset = -1;
2390 /* some debug output */
2391 gst_event_copy_segment (event, &segment);
2392 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2395 if (wav->state != GST_WAVPARSE_DATA) {
2396 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2400 /* now we are either committed to TIME or BYTE format,
2401 * and we only expect a BYTE segment, e.g. following a seek */
2402 if (segment.format == GST_FORMAT_BYTES) {
2403 /* handle (un)signed issues */
2404 start = segment.start;
2405 stop = segment.stop;
2408 start -= wav->datastart;
2409 start = MAX (start, 0);
2413 stop -= wav->datastart;
2414 stop = MAX (stop, 0);
2416 if (wav->segment.format == GST_FORMAT_TIME) {
2417 guint64 bps = wav->bps;
2419 /* operating in format TIME, so we can convert */
2420 if (!bps && wav->fact)
2422 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2426 gst_util_uint64_scale_ceil (start, GST_SECOND,
2427 (guint64) wav->bps);
2430 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2431 (guint64) wav->bps);
2435 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2439 segment.start = start;
2440 segment.stop = stop;
2442 /* accept upstream's notion of segment and distribute along */
2443 segment.format = wav->segment.format;
2444 segment.time = segment.position = segment.start;
2445 segment.duration = wav->segment.duration;
2446 segment.base = gst_segment_to_running_time (&wav->segment,
2447 GST_FORMAT_TIME, wav->segment.position);
2449 gst_segment_copy_into (&segment, &wav->segment);
2451 /* also store the newsegment event for the streaming thread */
2452 if (wav->start_segment)
2453 gst_event_unref (wav->start_segment);
2454 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2455 wav->start_segment = gst_event_new_segment (&segment);
2457 /* stream leftover data in current segment */
2458 gst_wavparse_flush_data (wav);
2459 /* and set up streaming thread for next one */
2460 wav->offset = offset;
2461 wav->end_offset = end_offset;
2463 if (wav->datasize > 0 && (wav->end_offset == -1
2464 || wav->end_offset > wav->datastart + wav->datasize))
2465 wav->end_offset = wav->datastart + wav->datasize;
2467 if (wav->end_offset != -1) {
2468 wav->dataleft = wav->end_offset - wav->offset;
2470 /* infinity; upstream will EOS when done */
2471 wav->dataleft = G_MAXUINT64;
2474 gst_event_unref (event);
2478 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2479 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2480 ("No valid input found before end of stream"));
2482 /* add pad if needed so EOS is seen downstream */
2483 if (G_UNLIKELY (wav->first)) {
2485 gst_wavparse_add_src_pad (wav, NULL);
2487 /* stream leftover data in current segment */
2488 gst_wavparse_flush_data (wav);
2493 case GST_EVENT_FLUSH_STOP:
2498 gst_adapter_clear (wav->adapter);
2499 wav->discont = TRUE;
2500 dur = wav->segment.duration;
2501 gst_segment_init (&wav->segment, wav->segment.format);
2502 wav->segment.duration = dur;
2506 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2514 /* convert and query stuff */
2515 static const GstFormat *
2516 gst_wavparse_get_formats (GstPad * pad)
2518 static const GstFormat formats[] = {
2521 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2530 gst_wavparse_pad_convert (GstPad * pad,
2531 GstFormat src_format, gint64 src_value,
2532 GstFormat * dest_format, gint64 * dest_value)
2534 GstWavParse *wavparse;
2535 gboolean res = TRUE;
2537 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2539 if (*dest_format == src_format) {
2540 *dest_value = src_value;
2544 if ((wavparse->bps == 0) && !wavparse->fact)
2547 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2548 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2550 switch (src_format) {
2551 case GST_FORMAT_BYTES:
2552 switch (*dest_format) {
2553 case GST_FORMAT_DEFAULT:
2554 *dest_value = src_value / wavparse->bytes_per_sample;
2555 /* make sure we end up on a sample boundary */
2556 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2558 case GST_FORMAT_TIME:
2559 /* src_value + datastart = offset */
2560 GST_INFO_OBJECT (wavparse,
2561 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2563 if (wavparse->bps > 0)
2564 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2565 (guint64) wavparse->bps);
2566 else if (wavparse->fact) {
2567 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2568 wavparse->rate, wavparse->fact);
2571 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2582 case GST_FORMAT_DEFAULT:
2583 switch (*dest_format) {
2584 case GST_FORMAT_BYTES:
2585 *dest_value = src_value * wavparse->bytes_per_sample;
2587 case GST_FORMAT_TIME:
2588 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2589 (guint64) wavparse->rate);
2597 case GST_FORMAT_TIME:
2598 switch (*dest_format) {
2599 case GST_FORMAT_BYTES:
2600 if (wavparse->bps > 0)
2601 *dest_value = gst_util_uint64_scale (src_value,
2602 (guint64) wavparse->bps, GST_SECOND);
2604 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2605 wavparse->rate, wavparse->fact);
2607 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2609 /* make sure we end up on a sample boundary */
2610 *dest_value -= *dest_value % wavparse->blockalign;
2612 case GST_FORMAT_DEFAULT:
2613 *dest_value = gst_util_uint64_scale (src_value,
2614 (guint64) wavparse->rate, GST_SECOND);
2633 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2639 /* handle queries for location and length in requested format */
2641 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2643 gboolean res = TRUE;
2644 GstWavParse *wav = GST_WAVPARSE (parent);
2646 /* only if we know */
2647 if (wav->state != GST_WAVPARSE_DATA) {
2651 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2653 switch (GST_QUERY_TYPE (query)) {
2654 case GST_QUERY_POSITION:
2660 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2661 curb = wav->offset - wav->datastart;
2662 gst_query_parse_position (query, &format, NULL);
2663 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2666 case GST_FORMAT_BYTES:
2667 format = GST_FORMAT_BYTES;
2671 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2676 gst_query_set_position (query, format, cur);
2679 case GST_QUERY_DURATION:
2681 gint64 duration = 0;
2684 if (wav->ignore_length) {
2689 gst_query_parse_duration (query, &format, NULL);
2692 case GST_FORMAT_BYTES:{
2693 format = GST_FORMAT_BYTES;
2694 duration = wav->datasize;
2697 case GST_FORMAT_TIME:
2698 if ((res = gst_wavparse_calculate_duration (wav))) {
2699 duration = wav->duration;
2707 gst_query_set_duration (query, format, duration);
2710 case GST_QUERY_CONVERT:
2712 gint64 srcvalue, dstvalue;
2713 GstFormat srcformat, dstformat;
2715 gst_query_parse_convert (query, &srcformat, &srcvalue,
2716 &dstformat, &dstvalue);
2717 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2718 &dstformat, &dstvalue);
2720 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2723 case GST_QUERY_SEEKING:{
2725 gboolean seekable = FALSE;
2727 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2728 if (fmt == wav->segment.format) {
2729 if (wav->streaming) {
2732 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2733 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2734 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2735 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2737 gst_query_unref (q);
2739 GST_LOG_OBJECT (wav, "looping => seekable");
2743 } else if (fmt == GST_FORMAT_TIME) {
2747 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2751 case GST_QUERY_SEGMENT:
2756 format = wav->segment.format;
2759 gst_segment_to_stream_time (&wav->segment, format,
2760 wav->segment.start);
2761 if ((stop = wav->segment.stop) == -1)
2762 stop = wav->segment.duration;
2764 stop = gst_segment_to_stream_time (&wav->segment, format, stop);
2766 gst_query_set_segment (query, wav->segment.rate, format, start, stop);
2771 res = gst_pad_query_default (pad, parent, query);
2778 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2780 GstWavParse *wavparse = GST_WAVPARSE (parent);
2781 gboolean res = FALSE;
2783 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2785 switch (GST_EVENT_TYPE (event)) {
2786 case GST_EVENT_SEEK:
2787 /* can only handle events when we are in the data state */
2788 if (wavparse->state == GST_WAVPARSE_DATA) {
2789 res = gst_wavparse_perform_seek (wavparse, event);
2791 gst_event_unref (event);
2794 case GST_EVENT_TOC_SELECT:
2797 GstTocEntry *entry = NULL;
2798 GstEvent *seek_event;
2801 if (!wavparse->toc) {
2802 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2805 gst_event_parse_toc_select (event, &uid);
2807 GST_OBJECT_LOCK (wavparse);
2808 entry = gst_toc_find_entry (wavparse->toc, uid);
2809 if (entry == NULL) {
2810 GST_OBJECT_UNLOCK (wavparse);
2811 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2815 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2816 GST_OBJECT_UNLOCK (wavparse);
2817 seek_event = gst_event_new_seek (1.0,
2819 GST_SEEK_FLAG_FLUSH,
2820 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2821 res = gst_wavparse_perform_seek (wavparse, seek_event);
2822 gst_event_unref (seek_event);
2826 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2830 gst_event_unref (event);
2835 res = gst_pad_push_event (wavparse->sinkpad, event);
2842 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2844 GstWavParse *wav = GST_WAVPARSE (parent);
2849 gst_adapter_clear (wav->adapter);
2850 g_object_unref (wav->adapter);
2851 wav->adapter = NULL;
2854 query = gst_query_new_scheduling ();
2856 if (!gst_pad_peer_query (sinkpad, query)) {
2857 gst_query_unref (query);
2861 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2862 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2863 gst_query_unref (query);
2868 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2869 wav->streaming = FALSE;
2870 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2874 GST_DEBUG_OBJECT (sinkpad, "activating push");
2875 wav->streaming = TRUE;
2876 wav->adapter = gst_adapter_new ();
2877 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2883 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2884 GstPadMode mode, gboolean active)
2889 case GST_PAD_MODE_PUSH:
2892 case GST_PAD_MODE_PULL:
2894 /* if we have a scheduler we can start the task */
2895 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2898 res = gst_pad_stop_task (sinkpad);
2908 static GstStateChangeReturn
2909 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2911 GstStateChangeReturn ret;
2912 GstWavParse *wav = GST_WAVPARSE (element);
2914 switch (transition) {
2915 case GST_STATE_CHANGE_NULL_TO_READY:
2917 case GST_STATE_CHANGE_READY_TO_PAUSED:
2918 gst_wavparse_reset (wav);
2920 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2926 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2928 switch (transition) {
2929 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2931 case GST_STATE_CHANGE_PAUSED_TO_READY:
2932 gst_wavparse_reset (wav);
2934 case GST_STATE_CHANGE_READY_TO_NULL:
2943 gst_wavparse_set_property (GObject * object, guint prop_id,
2944 const GValue * value, GParamSpec * pspec)
2948 g_return_if_fail (GST_IS_WAVPARSE (object));
2949 self = GST_WAVPARSE (object);
2952 case PROP_IGNORE_LENGTH:
2953 self->ignore_length = g_value_get_boolean (value);
2956 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2962 gst_wavparse_get_property (GObject * object, guint prop_id,
2963 GValue * value, GParamSpec * pspec)
2967 g_return_if_fail (GST_IS_WAVPARSE (object));
2968 self = GST_WAVPARSE (object);
2971 case PROP_IGNORE_LENGTH:
2972 g_value_set_boolean (value, self->ignore_length);
2975 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2980 plugin_init (GstPlugin * plugin)
2984 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2988 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2991 "Parse a .wav file into raw audio",
2992 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)