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_reset (GstWavParse * wav)
199 wav->state = GST_WAVPARSE_START;
201 /* These will all be set correctly in the fmt chunk */
215 wav->got_fmt = FALSE;
219 gst_event_unref (wav->seek_event);
220 wav->seek_event = NULL;
222 gst_adapter_clear (wav->adapter);
223 g_object_unref (wav->adapter);
227 gst_tag_list_unref (wav->tags);
230 gst_toc_unref (wav->toc);
233 g_list_free_full (wav->cues, g_free);
236 g_list_free_full (wav->labls, g_free);
239 gst_caps_unref (wav->caps);
241 if (wav->start_segment)
242 gst_event_unref (wav->start_segment);
243 wav->start_segment = NULL;
247 gst_wavparse_dispose (GObject * object)
249 GstWavParse *wav = GST_WAVPARSE (object);
251 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
252 gst_wavparse_reset (wav);
254 G_OBJECT_CLASS (parent_class)->dispose (object);
258 gst_wavparse_init (GstWavParse * wavparse)
260 gst_wavparse_reset (wavparse);
264 gst_pad_new_from_static_template (&sink_template_factory, "sink");
265 gst_pad_set_activate_function (wavparse->sinkpad,
266 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
267 gst_pad_set_activatemode_function (wavparse->sinkpad,
268 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
269 gst_pad_set_chain_function (wavparse->sinkpad,
270 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
271 gst_pad_set_event_function (wavparse->sinkpad,
272 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
273 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
277 gst_pad_new_from_template (gst_element_class_get_pad_template
278 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
279 gst_pad_use_fixed_caps (wavparse->srcpad);
280 gst_pad_set_query_function (wavparse->srcpad,
281 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
282 gst_pad_set_event_function (wavparse->srcpad,
283 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
284 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
288 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
292 if (!gst_riff_parse_file_header (element, buf, &doctype))
295 if (doctype != GST_RIFF_RIFF_WAVE)
303 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
304 ("File is not a WAVE file: 0x%" G_GINT32_MODIFIER "x", doctype));
310 gst_wavparse_stream_init (GstWavParse * wav)
313 GstBuffer *buf = NULL;
315 if ((res = gst_pad_pull_range (wav->sinkpad,
316 wav->offset, 12, &buf)) != GST_FLOW_OK)
318 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
319 return GST_FLOW_ERROR;
327 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
329 /* -1 always maps to -1 */
335 /* 0 always maps to 0 */
342 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
344 } else if (wav->fact) {
345 guint64 bps = gst_util_uint64_scale (wav->datasize, wav->rate, wav->fact);
346 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
353 /* This function is used to perform seeks on the element.
355 * It also works when event is NULL, in which case it will just
356 * start from the last configured segment. This technique is
357 * used when activating the element and to perform the seek in
361 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
365 GstFormat format, bformat;
367 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
368 gint64 cur, stop, upstream_size;
371 GstSegment seeksegment = { 0, };
376 GST_DEBUG_OBJECT (wav, "doing seek with event");
378 gst_event_parse_seek (event, &rate, &format, &flags,
379 &cur_type, &cur, &stop_type, &stop);
380 seqnum = gst_event_get_seqnum (event);
382 /* no negative rates yet */
386 if (format != wav->segment.format) {
387 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
388 gst_format_get_name (format),
389 gst_format_get_name (wav->segment.format));
391 if (cur_type != GST_SEEK_TYPE_NONE)
393 gst_pad_query_convert (wav->srcpad, format, cur,
394 wav->segment.format, &cur);
395 if (res && stop_type != GST_SEEK_TYPE_NONE)
397 gst_pad_query_convert (wav->srcpad, format, stop,
398 wav->segment.format, &stop);
402 format = wav->segment.format;
405 GST_DEBUG_OBJECT (wav, "doing seek without event");
408 cur_type = GST_SEEK_TYPE_SET;
409 stop_type = GST_SEEK_TYPE_SET;
412 /* in push mode, we must delegate to upstream */
413 if (wav->streaming) {
414 gboolean res = FALSE;
416 /* if streaming not yet started; only prepare initial newsegment */
417 if (!event || wav->state != GST_WAVPARSE_DATA) {
418 if (wav->start_segment)
419 gst_event_unref (wav->start_segment);
420 wav->start_segment = gst_event_new_segment (&wav->segment);
423 /* convert seek positions to byte positions in data sections */
424 if (format == GST_FORMAT_TIME) {
425 /* should not fail */
426 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
428 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
431 /* mind sample boundary and header */
433 cur -= (cur % wav->bytes_per_sample);
434 cur += wav->datastart;
437 stop -= (stop % wav->bytes_per_sample);
438 stop += wav->datastart;
440 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
441 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
443 /* BYTE seek event */
444 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
446 gst_event_set_seqnum (event, seqnum);
447 res = gst_pad_push_event (wav->sinkpad, event);
453 flush = flags & GST_SEEK_FLAG_FLUSH;
455 /* now we need to make sure the streaming thread is stopped. We do this by
456 * either sending a FLUSH_START event downstream which will cause the
457 * streaming thread to stop with a WRONG_STATE.
458 * For a non-flushing seek we simply pause the task, which will happen as soon
459 * as it completes one iteration (and thus might block when the sink is
460 * blocking in preroll). */
463 GST_DEBUG_OBJECT (wav, "sending flush start");
465 fevent = gst_event_new_flush_start ();
466 gst_event_set_seqnum (fevent, seqnum);
467 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
468 gst_pad_push_event (wav->srcpad, fevent);
470 gst_pad_pause_task (wav->sinkpad);
473 /* we should now be able to grab the streaming thread because we stopped it
474 * with the above flush/pause code */
475 GST_PAD_STREAM_LOCK (wav->sinkpad);
477 /* save current position */
478 last_stop = wav->segment.position;
480 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
482 /* copy segment, we need this because we still need the old
483 * segment when we close the current segment. */
484 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
486 /* configure the seek parameters in the seeksegment. We will then have the
487 * right values in the segment to perform the seek */
489 GST_DEBUG_OBJECT (wav, "configuring seek");
490 gst_segment_do_seek (&seeksegment, rate, format, flags,
491 cur_type, cur, stop_type, stop, &update);
494 /* figure out the last position we need to play. If it's configured (stop !=
495 * -1), use that, else we play until the total duration of the file */
496 if ((stop = seeksegment.stop) == -1)
497 stop = seeksegment.duration;
499 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
500 if ((cur_type != GST_SEEK_TYPE_NONE)) {
501 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
502 * we can just copy the last_stop. If not, we use the bps to convert TIME to
504 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
505 (gint64 *) & wav->offset))
506 wav->offset = seeksegment.position;
507 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
508 wav->offset -= (wav->offset % wav->bytes_per_sample);
509 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
510 wav->offset += wav->datastart;
511 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
513 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
517 if (stop_type != GST_SEEK_TYPE_NONE) {
518 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
519 wav->end_offset = stop;
520 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
521 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
522 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
523 wav->end_offset += wav->datastart;
524 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
526 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
530 /* make sure filesize is not exceeded due to rounding errors or so,
531 * same precaution as in _stream_headers */
532 bformat = GST_FORMAT_BYTES;
533 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
534 wav->end_offset = MIN (wav->end_offset, upstream_size);
536 if (wav->datasize > 0 && wav->end_offset > wav->datastart + wav->datasize)
537 wav->end_offset = wav->datastart + wav->datasize;
539 /* this is the range of bytes we will use for playback */
540 wav->offset = MIN (wav->offset, wav->end_offset);
541 wav->dataleft = wav->end_offset - wav->offset;
543 GST_DEBUG_OBJECT (wav,
544 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
545 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
546 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
548 /* prepare for streaming again */
552 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
553 GST_DEBUG_OBJECT (wav, "sending flush stop");
555 fevent = gst_event_new_flush_stop (TRUE);
556 gst_event_set_seqnum (fevent, seqnum);
557 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
558 gst_pad_push_event (wav->srcpad, fevent);
561 /* now we did the seek and can activate the new segment values */
562 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
564 /* if we're doing a segment seek, post a SEGMENT_START message */
565 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
566 gst_element_post_message (GST_ELEMENT_CAST (wav),
567 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
568 wav->segment.format, wav->segment.position));
571 /* now create the newsegment */
572 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
573 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
575 /* store the newsegment event so it can be sent from the streaming thread. */
576 if (wav->start_segment)
577 gst_event_unref (wav->start_segment);
578 wav->start_segment = gst_event_new_segment (&wav->segment);
579 gst_event_set_seqnum (wav->start_segment, seqnum);
581 /* mark discont if we are going to stream from another position. */
582 if (last_stop != wav->segment.position) {
583 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
587 /* and start the streaming task again */
588 if (!wav->streaming) {
589 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
593 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
600 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
605 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
610 GST_DEBUG_OBJECT (wav,
611 "Could not determine byte position for desired time");
617 * gst_wavparse_peek_chunk_info:
618 * @wav Wavparse object
619 * @tag holder for tag
620 * @size holder for tag size
622 * Peek next chunk info (tag and size)
624 * Returns: %TRUE when the chunk info (header) is available
627 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
629 const guint8 *data = NULL;
631 if (gst_adapter_available (wav->adapter) < 8)
634 data = gst_adapter_map (wav->adapter, 8);
635 *tag = GST_READ_UINT32_LE (data);
636 *size = GST_READ_UINT32_LE (data + 4);
637 gst_adapter_unmap (wav->adapter);
639 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
640 GST_FOURCC_ARGS (*tag));
646 * gst_wavparse_peek_chunk:
647 * @wav Wavparse object
648 * @tag holder for tag
649 * @size holder for tag size
651 * Peek enough data for one full chunk
653 * Returns: %TRUE when the full chunk is available
656 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
658 guint32 peek_size = 0;
661 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
664 /* size 0 -> empty data buffer would surprise most callers,
665 * large size -> do not bother trying to squeeze that into adapter,
666 * so we throw poor man's exception, which can be caught if caller really
667 * wants to handle 0 size chunk */
668 if (!(*size) || (*size) >= (1 << 30)) {
669 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
670 *size, GST_FOURCC_ARGS (*tag));
671 /* chain should give up */
672 wav->abort_buffering = TRUE;
675 peek_size = (*size + 1) & ~1;
676 available = gst_adapter_available (wav->adapter);
678 if (available >= (8 + peek_size)) {
681 GST_LOG ("but only %u bytes available now", available);
687 * gst_wavparse_calculate_duration:
688 * @wav: wavparse object
690 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
693 * Returns: %TRUE if duration is available.
696 gst_wavparse_calculate_duration (GstWavParse * wav)
698 if (wav->duration > 0)
702 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
704 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
706 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
707 GST_TIME_ARGS (wav->duration));
709 } else if (wav->fact) {
711 gst_util_uint64_scale_ceil (GST_SECOND, wav->fact, wav->rate);
712 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
713 GST_TIME_ARGS (wav->duration));
720 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
725 if (wav->streaming) {
726 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
729 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
730 GST_FOURCC_ARGS (tag));
731 flush = 8 + ((size + 1) & ~1);
732 wav->offset += flush;
733 if (wav->streaming) {
734 gst_adapter_flush (wav->adapter, flush);
736 gst_buffer_unref (buf);
743 * gst_wavparse_cue_chunk:
744 * @wav GstWavParse object
745 * @data holder for data
746 * @size holder for data size
748 * Parse cue chunk from @data to wav->cues.
750 * Returns: %TRUE when cue chunk is available
753 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
760 GST_WARNING_OBJECT (wav, "found another cue's");
764 ncues = GST_READ_UINT32_LE (data);
766 if (size < 4 + ncues * 24) {
767 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
773 for (i = 0; i < ncues; i++) {
774 cue = g_new0 (GstWavParseCue, 1);
775 cue->id = GST_READ_UINT32_LE (data);
776 cue->position = GST_READ_UINT32_LE (data + 4);
777 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
778 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
779 cue->block_start = GST_READ_UINT32_LE (data + 16);
780 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
781 cues = g_list_append (cues, cue);
791 * gst_wavparse_labl_chunk:
792 * @wav GstWavParse object
793 * @data holder for data
794 * @size holder for data size
796 * Parse labl from @data to wav->labls.
798 * Returns: %TRUE when labl chunk is available
801 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
803 GstWavParseLabl *labl;
808 labl = g_new0 (GstWavParseLabl, 1);
812 labl->cue_point_id = GST_READ_UINT32_LE (data);
813 labl->text = g_memdup (data + 4, size - 4);
815 wav->labls = g_list_append (wav->labls, labl);
821 * gst_wavparse_note_chunk:
822 * @wav GstWavParse object
823 * @data holder for data
824 * @size holder for data size
826 * Parse note from @data to wav->notes.
828 * Returns: %TRUE when note chunk is available
831 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
833 GstWavParseNote *note;
838 note = g_new0 (GstWavParseNote, 1);
842 note->cue_point_id = GST_READ_UINT32_LE (data);
843 note->text = g_memdup (data + 4, size - 4);
845 wav->notes = g_list_append (wav->notes, note);
851 * gst_wavparse_smpl_chunk:
852 * @wav GstWavParse object
853 * @data holder for data
854 * @size holder for data size
856 * Parse smpl chunk from @data.
858 * Returns: %TRUE when cue chunk is available
861 gst_wavparse_smpl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
866 manufacturer_id = GST_READ_UINT32_LE (data);
867 product_id = GST_READ_UINT32_LE (data + 4);
868 sample_period = GST_READ_UINT32_LE (data + 8);
870 note_number = GST_READ_UINT32_LE (data + 12);
872 pitch_fraction = GST_READ_UINT32_LE (data + 16);
873 SMPTE_format = GST_READ_UINT32_LE (data + 20);
874 SMPTE_offset = GST_READ_UINT32_LE (data + 24);
875 num_sample_loops = GST_READ_UINT32_LE (data + 28);
876 List of Sample Loops, 24 bytes each
880 wav->tags = gst_tag_list_new_empty ();
881 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
882 GST_TAG_MIDI_BASE_NOTE, (guint) note_number, NULL);
887 * gst_wavparse_adtl_chunk:
888 * @wav GstWavParse object
889 * @data holder for data
890 * @size holder for data size
892 * Parse adtl from @data.
894 * Returns: %TRUE when adtl chunk is available
897 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
899 guint32 ltag, lsize, offset = 0;
902 ltag = GST_READ_UINT32_LE (data + offset);
903 lsize = GST_READ_UINT32_LE (data + offset + 4);
905 if (lsize + 8 > size) {
906 GST_WARNING_OBJECT (wav, "Invalid adtl size: %u + 8 > %u", lsize, size);
911 case GST_RIFF_TAG_labl:
912 gst_wavparse_labl_chunk (wav, data + offset, size);
914 case GST_RIFF_TAG_note:
915 gst_wavparse_note_chunk (wav, data + offset, size);
918 GST_WARNING_OBJECT (wav, "Unknowm adtl %" GST_FOURCC_FORMAT,
919 GST_FOURCC_ARGS (ltag));
920 GST_MEMDUMP_OBJECT (wav, "Unknowm adtl", &data[offset], lsize);
923 offset += 8 + GST_ROUND_UP_2 (lsize);
924 size -= 8 + GST_ROUND_UP_2 (lsize);
931 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
933 GstTagList *tags = NULL;
934 GstTocEntry *entry = NULL;
936 entry = gst_toc_find_entry (toc, id);
938 tags = gst_toc_entry_get_tags (entry);
940 tags = gst_tag_list_new_empty ();
941 gst_toc_entry_set_tags (entry, tags);
949 * gst_wavparse_create_toc:
950 * @wav GstWavParse object
952 * Create TOC from wav->cues and wav->labls.
955 gst_wavparse_create_toc (GstWavParse * wav)
961 GstWavParseLabl *labl;
962 GstWavParseNote *note;
965 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
967 GST_OBJECT_LOCK (wav);
969 GST_OBJECT_UNLOCK (wav);
970 GST_WARNING_OBJECT (wav, "found another TOC");
975 GST_OBJECT_UNLOCK (wav);
979 /* FIXME: send CURRENT scope toc too */
980 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
982 /* add cue edition */
983 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
984 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
985 gst_toc_append_entry (toc, entry);
987 /* add tracks in cue edition */
991 prev_subentry = cur_subentry;
992 /* previous track stop time = current track start time */
993 if (prev_subentry != NULL) {
994 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
995 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
996 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
998 id = g_strdup_printf ("%08x", cue->id);
999 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
1001 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1002 stop = wav->duration;
1003 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
1004 gst_toc_entry_append_sub_entry (entry, cur_subentry);
1005 list = g_list_next (list);
1008 /* add tags in tracks */
1012 id = g_strdup_printf ("%08x", labl->cue_point_id);
1013 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1016 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1019 list = g_list_next (list);
1024 id = g_strdup_printf ("%08x", note->cue_point_id);
1025 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1028 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1031 list = g_list_next (list);
1034 /* send data as TOC */
1037 /* send TOC event */
1039 GST_OBJECT_UNLOCK (wav);
1040 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1046 #define MAX_BUFFER_SIZE 4096
1049 parse_ds64 (GstWavParse * wav, GstBuffer * buf)
1052 guint32 dataSizeLow, dataSizeHigh;
1053 guint32 sampleCountLow, sampleCountHigh;
1055 gst_buffer_map (buf, &map, GST_MAP_READ);
1056 dataSizeLow = GST_READ_UINT32_LE (map.data + 2 * 4);
1057 dataSizeHigh = GST_READ_UINT32_LE (map.data + 3 * 4);
1058 sampleCountLow = GST_READ_UINT32_LE (map.data + 4 * 4);
1059 sampleCountHigh = GST_READ_UINT32_LE (map.data + 5 * 4);
1060 gst_buffer_unmap (buf, &map);
1061 if (dataSizeHigh != 0xFFFFFFFF && dataSizeLow != 0xFFFFFFFF) {
1062 wav->datasize = ((guint64) dataSizeHigh << 32) | dataSizeLow;
1064 if (sampleCountHigh != 0xFFFFFFFF && sampleCountLow != 0xFFFFFFFF) {
1065 wav->fact = ((guint64) sampleCountHigh << 32) | sampleCountLow;
1068 GST_DEBUG_OBJECT (wav, "Got 'ds64' TAG, datasize : %" G_GINT64_FORMAT
1069 " fact: %" G_GINT64_FORMAT, wav->datasize, wav->fact);
1073 static GstFlowReturn
1074 gst_wavparse_stream_headers (GstWavParse * wav)
1076 GstFlowReturn res = GST_FLOW_OK;
1077 GstBuffer *buf = NULL;
1078 gst_riff_strf_auds *header = NULL;
1080 gboolean gotdata = FALSE;
1081 GstCaps *caps = NULL;
1082 gchar *codec_name = NULL;
1083 gint64 upstream_size = 0;
1086 /* search for "_fmt" chunk, which must be before "data" */
1087 while (!wav->got_fmt) {
1090 if (wav->streaming) {
1091 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1094 gst_adapter_flush (wav->adapter, 8);
1098 buf = gst_adapter_take_buffer (wav->adapter, size);
1100 gst_adapter_flush (wav->adapter, 1);
1101 wav->offset += GST_ROUND_UP_2 (size);
1103 buf = gst_buffer_new ();
1106 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1107 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1111 if (tag == GST_RS64_TAG_DS64) {
1112 if (!parse_ds64 (wav, buf))
1118 if (tag != GST_RIFF_TAG_fmt) {
1119 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1120 GST_FOURCC_ARGS (tag));
1121 gst_buffer_unref (buf);
1126 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1128 goto parse_header_error;
1130 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1132 /* do sanity checks of header fields */
1133 if (header->channels == 0)
1135 if (header->rate == 0)
1138 GST_DEBUG_OBJECT (wav, "creating the caps");
1140 /* Note: gst_riff_create_audio_caps might need to fix values in
1141 * the header header depending on the format, so call it first */
1142 /* FIXME: Need to handle the channel reorder map */
1143 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1144 NULL, &codec_name, NULL);
1147 gst_buffer_unref (extra);
1150 goto unknown_format;
1152 /* If we got raw audio from upstream, we remove the codec_data field,
1153 * which may have been added if the wav header included an extended
1154 * chunk. We want to keep it for non raw audio.
1156 s = gst_caps_get_structure (caps, 0);
1157 if (s && gst_structure_has_name (s, "audio/x-raw")) {
1158 gst_structure_remove_field (s, "codec_data");
1161 /* do more sanity checks of header fields
1162 * (these can be sanitized by gst_riff_create_audio_caps()
1164 wav->format = header->format;
1165 wav->rate = header->rate;
1166 wav->channels = header->channels;
1167 wav->blockalign = header->blockalign;
1168 wav->depth = header->bits_per_sample;
1169 wav->av_bps = header->av_bps;
1175 /* do format specific handling */
1176 switch (wav->format) {
1177 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1178 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1180 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1181 * bitrate inside the mpeg stream */
1182 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1186 case GST_RIFF_WAVE_FORMAT_PCM:
1187 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1188 goto invalid_blockalign;
1191 if (wav->av_bps > wav->blockalign * wav->rate)
1193 /* use the configured bps */
1194 wav->bps = wav->av_bps;
1198 wav->width = (wav->blockalign * 8) / wav->channels;
1199 wav->bytes_per_sample = wav->channels * wav->width / 8;
1201 if (wav->bytes_per_sample <= 0)
1202 goto no_bytes_per_sample;
1204 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1205 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1206 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1207 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1208 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1209 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1210 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1212 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1213 * formats). This will make the element output a BYTE format segment and
1214 * will not timestamp the outgoing buffers.
1216 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1218 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1220 /* create pad later so we can sniff the first few bytes
1221 * of the real data and correct our caps if necessary */
1222 gst_caps_replace (&wav->caps, caps);
1223 gst_caps_replace (&caps, NULL);
1225 wav->got_fmt = TRUE;
1227 if (wav->tags == NULL)
1228 wav->tags = gst_tag_list_new_empty ();
1231 GstCaps *templ_caps = gst_pad_get_pad_template_caps (wav->sinkpad);
1232 gst_pb_utils_add_codec_description_to_tag_list (wav->tags,
1233 GST_TAG_CONTAINER_FORMAT, templ_caps);
1234 gst_caps_unref (templ_caps);
1237 /* If bps is nonzero, then we do have a valid bitrate that can be
1238 * announced in a tag list. */
1240 guint bitrate = wav->bps * 8;
1241 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1242 GST_TAG_BITRATE, bitrate, NULL);
1246 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1247 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1249 g_free (codec_name);
1255 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1256 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1258 /* loop headers until we get data */
1260 if (wav->streaming) {
1261 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1268 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1269 &buf)) != GST_FLOW_OK)
1270 goto header_read_error;
1271 gst_buffer_map (buf, &map, GST_MAP_READ);
1272 tag = GST_READ_UINT32_LE (map.data);
1273 size = GST_READ_UINT32_LE (map.data + 4);
1274 gst_buffer_unmap (buf, &map);
1277 GST_INFO_OBJECT (wav,
1278 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT ", size %"
1279 G_GUINT32_FORMAT, GST_FOURCC_ARGS (tag), wav->offset, size);
1281 /* Maximum valid size is INT_MAX */
1282 if (size & 0x80000000) {
1283 GST_WARNING_OBJECT (wav, "Invalid size, clipping to 0x7fffffff");
1287 /* Clip to upstream size if known */
1288 if (upstream_size > 0 && size + wav->offset > upstream_size) {
1289 GST_WARNING_OBJECT (wav, "Clipping chunk size to file size");
1290 g_assert (upstream_size >= wav->offset);
1291 size = upstream_size - wav->offset;
1294 /* wav is a st00pid format, we don't know for sure where data starts.
1295 * So we have to go bit by bit until we find the 'data' header
1298 case GST_RIFF_TAG_data:{
1301 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1303 if (wav->ignore_length) {
1304 GST_DEBUG_OBJECT (wav, "Ignoring length");
1307 if (wav->streaming) {
1308 gst_adapter_flush (wav->adapter, 8);
1311 gst_buffer_unref (buf);
1314 wav->datastart = wav->offset;
1315 /* use size from ds64 chunk if available */
1316 if (size64 == -1 && wav->datasize > 0) {
1317 GST_DEBUG_OBJECT (wav, "Using ds64 datasize");
1318 size64 = wav->datasize;
1320 /* If size is zero, then the data chunk probably actually extends to
1321 the end of the file */
1322 if (size64 == 0 && upstream_size) {
1323 size64 = upstream_size - wav->datastart;
1325 /* Or the file might be truncated */
1326 else if (upstream_size) {
1327 size64 = MIN (size64, (upstream_size - wav->datastart));
1329 wav->datasize = size64;
1330 wav->dataleft = size64;
1331 wav->end_offset = size64 + wav->datastart;
1332 if (!wav->streaming) {
1333 /* We will continue parsing tags 'till end */
1334 wav->offset += size64;
1336 GST_DEBUG_OBJECT (wav, "datasize = %" G_GUINT64_FORMAT, size64);
1339 case GST_RIFF_TAG_fact:{
1340 if (wav->fact == 0 &&
1341 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1342 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1343 const guint data_size = 4;
1345 GST_INFO_OBJECT (wav, "Have fact chunk");
1346 if (size < data_size) {
1347 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1348 /* need more data */
1351 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1355 /* number of samples (for compressed formats) */
1356 if (wav->streaming) {
1357 const guint8 *data = NULL;
1359 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1362 gst_adapter_flush (wav->adapter, 8);
1363 data = gst_adapter_map (wav->adapter, data_size);
1364 wav->fact = GST_READ_UINT32_LE (data);
1365 gst_adapter_unmap (wav->adapter);
1366 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1368 gst_buffer_unref (buf);
1371 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1372 data_size, &buf)) != GST_FLOW_OK)
1373 goto header_read_error;
1374 gst_buffer_extract (buf, 0, &wav->fact, 4);
1375 wav->fact = GUINT32_FROM_LE (wav->fact);
1376 gst_buffer_unref (buf);
1378 GST_DEBUG_OBJECT (wav, "have fact %" G_GUINT64_FORMAT, wav->fact);
1379 wav->offset += 8 + GST_ROUND_UP_2 (size);
1382 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1383 /* need more data */
1389 case GST_RIFF_TAG_acid:{
1390 const gst_riff_acid *acid = NULL;
1391 const guint data_size = sizeof (gst_riff_acid);
1394 GST_INFO_OBJECT (wav, "Have acid chunk");
1395 if (size < data_size) {
1396 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1397 /* need more data */
1400 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1404 if (wav->streaming) {
1405 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1408 gst_adapter_flush (wav->adapter, 8);
1409 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1411 tempo = acid->tempo;
1412 gst_adapter_unmap (wav->adapter);
1415 gst_buffer_unref (buf);
1418 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1419 size, &buf)) != GST_FLOW_OK)
1420 goto header_read_error;
1421 gst_buffer_map (buf, &map, GST_MAP_READ);
1422 acid = (const gst_riff_acid *) map.data;
1423 tempo = acid->tempo;
1424 gst_buffer_unmap (buf, &map);
1426 /* send data as tags */
1428 wav->tags = gst_tag_list_new_empty ();
1429 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1430 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1432 size = GST_ROUND_UP_2 (size);
1433 if (wav->streaming) {
1434 gst_adapter_flush (wav->adapter, size);
1436 gst_buffer_unref (buf);
1438 wav->offset += 8 + size;
1441 /* FIXME: all list tags after data are ignored in streaming mode */
1442 case GST_RIFF_TAG_LIST:{
1445 if (wav->streaming) {
1446 const guint8 *data = NULL;
1448 if (gst_adapter_available (wav->adapter) < 12) {
1451 data = gst_adapter_map (wav->adapter, 12);
1452 ltag = GST_READ_UINT32_LE (data + 8);
1453 gst_adapter_unmap (wav->adapter);
1455 gst_buffer_unref (buf);
1458 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1459 &buf)) != GST_FLOW_OK)
1460 goto header_read_error;
1461 gst_buffer_extract (buf, 8, <ag, 4);
1462 ltag = GUINT32_FROM_LE (ltag);
1465 case GST_RIFF_LIST_INFO:{
1466 const gint data_size = size - 4;
1469 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1470 if (wav->streaming) {
1471 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1474 gst_adapter_flush (wav->adapter, 12);
1476 if (data_size > 0) {
1477 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1479 gst_adapter_flush (wav->adapter, 1);
1483 gst_buffer_unref (buf);
1485 if (data_size > 0) {
1487 gst_pad_pull_range (wav->sinkpad, wav->offset,
1488 data_size, &buf)) != GST_FLOW_OK)
1489 goto header_read_error;
1492 if (data_size > 0) {
1494 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1496 GstTagList *old = wav->tags;
1498 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1500 gst_tag_list_unref (old);
1501 gst_tag_list_unref (new);
1503 gst_buffer_unref (buf);
1504 wav->offset += GST_ROUND_UP_2 (data_size);
1508 case GST_RIFF_LIST_adtl:{
1509 const gint data_size = size - 4;
1511 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1512 if (wav->streaming) {
1513 const guint8 *data = NULL;
1515 gst_adapter_flush (wav->adapter, 12);
1517 data = gst_adapter_map (wav->adapter, data_size);
1518 gst_wavparse_adtl_chunk (wav, data, data_size);
1519 gst_adapter_unmap (wav->adapter);
1523 gst_buffer_unref (buf);
1527 gst_pad_pull_range (wav->sinkpad, wav->offset,
1528 data_size, &buf)) != GST_FLOW_OK)
1529 goto header_read_error;
1530 gst_buffer_map (buf, &map, GST_MAP_READ);
1531 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1533 gst_buffer_unmap (buf, &map);
1535 wav->offset += GST_ROUND_UP_2 (data_size);
1539 GST_WARNING_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1540 GST_FOURCC_ARGS (ltag));
1541 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1542 /* need more data */
1548 case GST_RIFF_TAG_cue:{
1549 const guint data_size = size;
1551 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1552 if (wav->streaming) {
1553 const guint8 *data = NULL;
1555 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1558 gst_adapter_flush (wav->adapter, 8);
1560 data = gst_adapter_map (wav->adapter, data_size);
1561 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1562 goto header_read_error;
1564 gst_adapter_unmap (wav->adapter);
1569 gst_buffer_unref (buf);
1572 gst_pad_pull_range (wav->sinkpad, wav->offset,
1573 data_size, &buf)) != GST_FLOW_OK)
1574 goto header_read_error;
1575 gst_buffer_map (buf, &map, GST_MAP_READ);
1576 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1578 goto header_read_error;
1580 gst_buffer_unmap (buf, &map);
1582 size = GST_ROUND_UP_2 (size);
1583 if (wav->streaming) {
1584 gst_adapter_flush (wav->adapter, size);
1586 gst_buffer_unref (buf);
1588 size = GST_ROUND_UP_2 (size);
1589 wav->offset += size;
1592 case GST_RIFF_TAG_smpl:{
1593 const gint data_size = size;
1595 GST_DEBUG_OBJECT (wav, "Have 'smpl' TAG, size : %u", data_size);
1596 if (wav->streaming) {
1597 const guint8 *data = NULL;
1599 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1602 gst_adapter_flush (wav->adapter, 8);
1604 data = gst_adapter_map (wav->adapter, data_size);
1605 if (!gst_wavparse_smpl_chunk (wav, data, data_size)) {
1606 goto header_read_error;
1608 gst_adapter_unmap (wav->adapter);
1613 gst_buffer_unref (buf);
1616 gst_pad_pull_range (wav->sinkpad, wav->offset,
1617 data_size, &buf)) != GST_FLOW_OK)
1618 goto header_read_error;
1619 gst_buffer_map (buf, &map, GST_MAP_READ);
1620 if (!gst_wavparse_smpl_chunk (wav, (const guint8 *) map.data,
1622 goto header_read_error;
1624 gst_buffer_unmap (buf, &map);
1626 size = GST_ROUND_UP_2 (size);
1627 if (wav->streaming) {
1628 gst_adapter_flush (wav->adapter, size);
1630 gst_buffer_unref (buf);
1632 size = GST_ROUND_UP_2 (size);
1633 wav->offset += size;
1637 GST_WARNING_OBJECT (wav, "Ignoring chunk %" GST_FOURCC_FORMAT,
1638 GST_FOURCC_ARGS (tag));
1639 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1640 /* need more data */
1645 if (upstream_size && (wav->offset >= upstream_size)) {
1646 /* Now we are gone through the whole file */
1651 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1653 if (wav->bps <= 0 && wav->fact) {
1655 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1657 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1658 (guint64) wav->fact);
1659 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1664 if (gst_wavparse_calculate_duration (wav)) {
1665 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1666 if (!wav->ignore_length)
1667 wav->segment.duration = wav->duration;
1669 gst_wavparse_create_toc (wav);
1671 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1672 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1673 if (!wav->ignore_length)
1674 wav->segment.duration = wav->datasize;
1677 /* now we have all the info to perform a pending seek if any, if no
1678 * event, this will still do the right thing and it will also send
1679 * the right newsegment event downstream. */
1680 gst_wavparse_perform_seek (wav, wav->seek_event);
1681 /* remove pending event */
1682 gst_event_replace (&wav->seek_event, NULL);
1684 /* we just started, we are discont */
1685 wav->discont = TRUE;
1687 wav->state = GST_WAVPARSE_DATA;
1689 /* determine reasonable max buffer size,
1690 * that is, buffers not too small either size or time wise
1691 * so we do not end up with too many of them */
1693 if (gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size))
1694 wav->max_buf_size = upstream_size;
1696 wav->max_buf_size = 0;
1697 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1698 if (wav->blockalign > 0)
1699 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1701 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1708 g_free (codec_name);
1711 gst_caps_unref (caps);
1716 res = GST_FLOW_ERROR;
1721 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1722 ("Couldn't parse audio header"));
1727 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1728 ("Stream claims to contain no channels - invalid data"));
1733 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1734 ("Stream with sample_rate == 0 - invalid data"));
1739 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1740 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1741 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1746 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1747 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1748 wav->av_bps, wav->blockalign * wav->rate));
1751 no_bytes_per_sample:
1753 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1754 ("Could not caluclate bytes per sample - invalid data"));
1759 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1760 ("No caps found for format 0x%x, %u channels, %u Hz",
1761 wav->format, wav->channels, wav->rate));
1766 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1767 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1773 * Read WAV file tag when streaming
1775 static GstFlowReturn
1776 gst_wavparse_parse_stream_init (GstWavParse * wav)
1778 if (gst_adapter_available (wav->adapter) >= 12) {
1781 /* _take flushes the data */
1782 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1784 GST_DEBUG ("Parsing wav header");
1785 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1786 return GST_FLOW_ERROR;
1789 /* Go to next state */
1790 wav->state = GST_WAVPARSE_HEADER;
1795 /* handle an event sent directly to the element.
1797 * This event can be sent either in the READY state or the
1798 * >READY state. The only event of interest really is the seek
1801 * In the READY state we can only store the event and try to
1802 * respect it when going to PAUSED. We assume we are in the
1803 * READY state when our parsing state != GST_WAVPARSE_DATA.
1805 * When we are steaming, we can simply perform the seek right
1809 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1811 GstWavParse *wav = GST_WAVPARSE (element);
1812 gboolean res = FALSE;
1814 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1816 switch (GST_EVENT_TYPE (event)) {
1817 case GST_EVENT_SEEK:
1818 if (wav->state == GST_WAVPARSE_DATA) {
1819 /* we can handle the seek directly when streaming data */
1820 res = gst_wavparse_perform_seek (wav, event);
1822 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1824 gst_event_replace (&wav->seek_event, event);
1826 /* we always return true */
1833 gst_event_unref (event);
1838 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1842 s = gst_caps_get_structure (caps, 0);
1843 if (!gst_structure_has_name (s, "audio/x-dts"))
1845 /* typefind behavior for DTS:
1846 * MAXIMUM: multiple frame syncs detected, certainly DTS
1847 * LIKELY: single frame sync at offset 0. Maybe DTS?
1848 * POSSIBLE: single frame sync, not at offset 0. Highly unlikely
1850 if (prob > GST_TYPE_FIND_LIKELY)
1852 if (prob <= GST_TYPE_FIND_POSSIBLE)
1854 /* for maybe, check for at least a valid-looking rate and channels */
1855 if (!gst_structure_has_field (s, "channels"))
1857 /* and for extra assurance we could also check the rate from the DTS frame
1858 * against the one in the wav header, but for now let's not do that */
1859 return gst_structure_has_field (s, "rate");
1863 gst_wavparse_get_upstream_tags (GstWavParse * wav, GstTagScope scope)
1865 GstTagList *tags = NULL;
1870 while ((ev = gst_pad_get_sticky_event (wav->sinkpad, GST_EVENT_TAG, i++))) {
1871 gst_event_parse_tag (ev, &tags);
1872 if (tags != NULL && gst_tag_list_get_scope (tags) == scope) {
1873 tags = gst_tag_list_copy (tags);
1874 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1875 gst_event_unref (ev);
1879 gst_event_unref (ev);
1885 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1888 GstTagList *tags, *utags;
1890 GST_DEBUG_OBJECT (wav, "adding src pad");
1892 g_assert (wav->caps != NULL);
1894 s = gst_caps_get_structure (wav->caps, 0);
1895 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1896 GstTypeFindProbability prob;
1899 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1900 if (tf_caps != NULL) {
1901 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1902 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1903 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1904 gst_caps_unref (wav->caps);
1905 wav->caps = tf_caps;
1907 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1908 GST_TAG_AUDIO_CODEC, "dts", NULL);
1910 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1911 "marked as raw PCM audio, but ignoring for now", tf_caps);
1912 gst_caps_unref (tf_caps);
1917 gst_pad_set_caps (wav->srcpad, wav->caps);
1919 if (wav->start_segment) {
1920 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1921 gst_pad_push_event (wav->srcpad, wav->start_segment);
1922 wav->start_segment = NULL;
1925 /* upstream tags, e.g. from id3/ape tag before the wav file; assume for now
1926 * that there'll be only one scope/type of tag list from upstream, if any */
1927 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_GLOBAL);
1929 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_STREAM);
1931 /* if there's a tag upstream it's probably been added to override the
1932 * tags from inside the wav header, so keep upstream tags if in doubt */
1933 tags = gst_tag_list_merge (utags, wav->tags, GST_TAG_MERGE_KEEP);
1935 if (wav->tags != NULL) {
1936 gst_tag_list_unref (wav->tags);
1941 gst_tag_list_unref (utags);
1943 /* send tags downstream, if any */
1945 gst_pad_push_event (wav->srcpad, gst_event_new_tag (tags));
1948 static GstFlowReturn
1949 gst_wavparse_stream_data (GstWavParse * wav)
1951 GstBuffer *buf = NULL;
1952 GstFlowReturn res = GST_FLOW_OK;
1953 guint64 desired, obtained;
1954 GstClockTime timestamp, next_timestamp, duration;
1955 guint64 pos, nextpos;
1958 GST_LOG_OBJECT (wav,
1959 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1960 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1962 /* Get the next n bytes and output them */
1963 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1966 /* scale the amount of data by the segment rate so we get equal
1967 * amounts of data regardless of the playback rate */
1969 MIN (gst_guint64_to_gdouble (wav->dataleft),
1970 wav->max_buf_size * ABS (wav->segment.rate));
1972 if (desired >= wav->blockalign && wav->blockalign > 0)
1973 desired -= (desired % wav->blockalign);
1975 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1976 "from the sinkpad", desired);
1978 if (wav->streaming) {
1979 guint avail = gst_adapter_available (wav->adapter);
1982 /* flush some bytes if evil upstream sends segment that starts
1983 * before data or does is not send sample aligned segment */
1984 if (G_LIKELY (wav->offset >= wav->datastart)) {
1985 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1987 extra = wav->datastart - wav->offset;
1990 if (G_UNLIKELY (extra)) {
1991 extra = wav->bytes_per_sample - extra;
1992 if (extra <= avail) {
1993 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
1994 gst_adapter_flush (wav->adapter, extra);
1995 wav->offset += extra;
1996 wav->dataleft -= extra;
1997 goto iterate_adapter;
1999 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
2000 gst_adapter_clear (wav->adapter);
2001 wav->offset += avail;
2002 wav->dataleft -= avail;
2007 if (avail < desired) {
2008 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
2012 buf = gst_adapter_take_buffer (wav->adapter, desired);
2014 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
2015 desired, &buf)) != GST_FLOW_OK)
2018 /* we may get a short buffer at the end of the file */
2019 if (gst_buffer_get_size (buf) < desired) {
2020 gsize size = gst_buffer_get_size (buf);
2022 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2023 if (size >= wav->blockalign) {
2024 if (wav->blockalign > 0) {
2025 buf = gst_buffer_make_writable (buf);
2026 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2029 gst_buffer_unref (buf);
2035 obtained = gst_buffer_get_size (buf);
2037 /* our positions in bytes */
2038 pos = wav->offset - wav->datastart;
2039 nextpos = pos + obtained;
2041 /* update offsets, does not overflow. */
2042 buf = gst_buffer_make_writable (buf);
2043 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2044 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2046 /* first chunk of data? create the source pad. We do this only here so
2047 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2048 if (G_UNLIKELY (wav->first)) {
2050 /* this will also push the segment events */
2051 gst_wavparse_add_src_pad (wav, buf);
2053 /* If we have a pending start segment, send it now. */
2054 if (G_UNLIKELY (wav->start_segment != NULL)) {
2055 gst_pad_push_event (wav->srcpad, wav->start_segment);
2056 wav->start_segment = NULL;
2061 /* and timestamps if we have a bitrate, be careful for overflows */
2063 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2065 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2066 duration = next_timestamp - timestamp;
2068 /* update current running segment position */
2069 if (G_LIKELY (next_timestamp >= wav->segment.start))
2070 wav->segment.position = next_timestamp;
2071 } else if (wav->fact) {
2073 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2074 /* and timestamps if we have a bitrate, be careful for overflows */
2075 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2076 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2077 duration = next_timestamp - timestamp;
2079 /* no bitrate, all we know is that the first sample has timestamp 0, all
2080 * other positions and durations have unknown timestamp. */
2084 timestamp = GST_CLOCK_TIME_NONE;
2085 duration = GST_CLOCK_TIME_NONE;
2086 /* update current running segment position with byte offset */
2087 if (G_LIKELY (nextpos >= wav->segment.start))
2088 wav->segment.position = nextpos;
2090 if ((pos > 0) && wav->vbr) {
2091 /* don't set timestamps for VBR files if it's not the first buffer */
2092 timestamp = GST_CLOCK_TIME_NONE;
2093 duration = GST_CLOCK_TIME_NONE;
2096 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2097 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2098 wav->discont = FALSE;
2101 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2102 GST_BUFFER_DURATION (buf) = duration;
2104 GST_LOG_OBJECT (wav,
2105 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2106 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2107 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2109 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2112 if (obtained < wav->dataleft) {
2113 wav->offset += obtained;
2114 wav->dataleft -= obtained;
2116 wav->offset += wav->dataleft;
2120 /* Iterate until need more data, so adapter size won't grow */
2121 if (wav->streaming) {
2122 GST_LOG_OBJECT (wav,
2123 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2125 goto iterate_adapter;
2132 GST_DEBUG_OBJECT (wav, "found EOS");
2133 return GST_FLOW_EOS;
2137 /* check if we got EOS */
2138 if (res == GST_FLOW_EOS)
2141 GST_WARNING_OBJECT (wav,
2142 "Error getting %" G_GINT64_FORMAT " bytes from the "
2143 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2148 GST_INFO_OBJECT (wav,
2149 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2150 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2151 gst_pad_is_linked (wav->srcpad));
2157 gst_wavparse_loop (GstPad * pad)
2160 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2164 GST_LOG_OBJECT (wav, "process data");
2166 switch (wav->state) {
2167 case GST_WAVPARSE_START:
2168 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2169 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2173 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2174 event = gst_event_new_stream_start (stream_id);
2175 gst_event_set_group_id (event, gst_util_group_id_next ());
2176 gst_pad_push_event (wav->srcpad, event);
2179 wav->state = GST_WAVPARSE_HEADER;
2182 case GST_WAVPARSE_HEADER:
2183 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2184 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2187 wav->state = GST_WAVPARSE_DATA;
2188 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2191 case GST_WAVPARSE_DATA:
2192 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2196 g_assert_not_reached ();
2203 const gchar *reason = gst_flow_get_name (ret);
2205 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2206 gst_pad_pause_task (pad);
2208 if (ret == GST_FLOW_EOS) {
2209 /* handle end-of-stream/segment */
2210 /* so align our position with the end of it, if there is one
2211 * this ensures a subsequent will arrive at correct base/acc time */
2212 if (wav->segment.format == GST_FORMAT_TIME) {
2213 if (wav->segment.rate > 0.0 &&
2214 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2215 wav->segment.position = wav->segment.stop;
2216 else if (wav->segment.rate < 0.0)
2217 wav->segment.position = wav->segment.start;
2219 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2220 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2221 ("No valid input found before end of stream"));
2222 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2224 /* add pad before we perform EOS */
2225 if (G_UNLIKELY (wav->first)) {
2227 gst_wavparse_add_src_pad (wav, NULL);
2230 /* perform EOS logic */
2231 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2234 if ((stop = wav->segment.stop) == -1)
2235 stop = wav->segment.duration;
2237 gst_element_post_message (GST_ELEMENT_CAST (wav),
2238 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2239 wav->segment.format, stop));
2240 gst_pad_push_event (wav->srcpad,
2241 gst_event_new_segment_done (wav->segment.format, stop));
2243 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2246 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2247 /* for fatal errors we post an error message, post the error
2248 * first so the app knows about the error first. */
2249 GST_ELEMENT_FLOW_ERROR (wav, ret);
2250 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2256 static GstFlowReturn
2257 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2260 GstWavParse *wav = GST_WAVPARSE (parent);
2262 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2263 gst_buffer_get_size (buf));
2265 gst_adapter_push (wav->adapter, buf);
2267 switch (wav->state) {
2268 case GST_WAVPARSE_START:
2269 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2270 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2273 if (wav->state != GST_WAVPARSE_HEADER)
2276 /* otherwise fall-through */
2277 case GST_WAVPARSE_HEADER:
2278 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2279 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2282 if (!wav->got_fmt || wav->datastart == 0)
2285 wav->state = GST_WAVPARSE_DATA;
2286 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2289 case GST_WAVPARSE_DATA:
2290 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2291 wav->discont = TRUE;
2292 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2296 g_return_val_if_reached (GST_FLOW_ERROR);
2299 if (G_UNLIKELY (wav->abort_buffering)) {
2300 wav->abort_buffering = FALSE;
2301 ret = GST_FLOW_ERROR;
2302 /* sort of demux/parse error */
2303 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2309 static GstFlowReturn
2310 gst_wavparse_flush_data (GstWavParse * wav)
2312 GstFlowReturn ret = GST_FLOW_OK;
2315 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2316 ret = gst_wavparse_stream_data (wav);
2323 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2325 GstWavParse *wav = GST_WAVPARSE (parent);
2326 gboolean ret = TRUE;
2328 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2330 switch (GST_EVENT_TYPE (event)) {
2331 case GST_EVENT_CAPS:
2333 /* discard, we'll come up with proper src caps */
2334 gst_event_unref (event);
2337 case GST_EVENT_SEGMENT:
2339 gint64 start, stop, offset = 0, end_offset = -1;
2342 /* some debug output */
2343 gst_event_copy_segment (event, &segment);
2344 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2347 if (wav->state != GST_WAVPARSE_DATA) {
2348 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2352 /* now we are either committed to TIME or BYTE format,
2353 * and we only expect a BYTE segment, e.g. following a seek */
2354 if (segment.format == GST_FORMAT_BYTES) {
2355 /* handle (un)signed issues */
2356 start = segment.start;
2357 stop = segment.stop;
2360 start -= wav->datastart;
2361 start = MAX (start, 0);
2365 stop -= wav->datastart;
2366 stop = MAX (stop, 0);
2368 if (wav->segment.format == GST_FORMAT_TIME) {
2369 guint64 bps = wav->bps;
2371 /* operating in format TIME, so we can convert */
2372 if (!bps && wav->fact)
2374 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2378 gst_util_uint64_scale_ceil (start, GST_SECOND,
2379 (guint64) wav->bps);
2382 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2383 (guint64) wav->bps);
2387 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2391 segment.start = start;
2392 segment.stop = stop;
2394 /* accept upstream's notion of segment and distribute along */
2395 segment.format = wav->segment.format;
2396 segment.time = segment.position = segment.start;
2397 segment.duration = wav->segment.duration;
2398 segment.base = gst_segment_to_running_time (&wav->segment,
2399 GST_FORMAT_TIME, wav->segment.position);
2401 gst_segment_copy_into (&segment, &wav->segment);
2403 /* also store the newsegment event for the streaming thread */
2404 if (wav->start_segment)
2405 gst_event_unref (wav->start_segment);
2406 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2407 wav->start_segment = gst_event_new_segment (&segment);
2409 /* stream leftover data in current segment */
2410 gst_wavparse_flush_data (wav);
2411 /* and set up streaming thread for next one */
2412 wav->offset = offset;
2413 wav->end_offset = end_offset;
2415 if (wav->datasize > 0 && (wav->end_offset == -1
2416 || wav->end_offset > wav->datastart + wav->datasize))
2417 wav->end_offset = wav->datastart + wav->datasize;
2419 if (wav->end_offset != -1) {
2420 wav->dataleft = wav->end_offset - wav->offset;
2422 /* infinity; upstream will EOS when done */
2423 wav->dataleft = G_MAXUINT64;
2426 gst_event_unref (event);
2430 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2431 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2432 ("No valid input found before end of stream"));
2434 /* add pad if needed so EOS is seen downstream */
2435 if (G_UNLIKELY (wav->first)) {
2437 gst_wavparse_add_src_pad (wav, NULL);
2439 /* stream leftover data in current segment */
2440 gst_wavparse_flush_data (wav);
2445 case GST_EVENT_FLUSH_STOP:
2450 gst_adapter_clear (wav->adapter);
2451 wav->discont = TRUE;
2452 dur = wav->segment.duration;
2453 gst_segment_init (&wav->segment, wav->segment.format);
2454 wav->segment.duration = dur;
2458 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2466 /* convert and query stuff */
2467 static const GstFormat *
2468 gst_wavparse_get_formats (GstPad * pad)
2470 static const GstFormat formats[] = {
2473 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2482 gst_wavparse_pad_convert (GstPad * pad,
2483 GstFormat src_format, gint64 src_value,
2484 GstFormat * dest_format, gint64 * dest_value)
2486 GstWavParse *wavparse;
2487 gboolean res = TRUE;
2489 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2491 if (*dest_format == src_format) {
2492 *dest_value = src_value;
2496 if ((wavparse->bps == 0) && !wavparse->fact)
2499 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2500 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2502 switch (src_format) {
2503 case GST_FORMAT_BYTES:
2504 switch (*dest_format) {
2505 case GST_FORMAT_DEFAULT:
2506 *dest_value = src_value / wavparse->bytes_per_sample;
2507 /* make sure we end up on a sample boundary */
2508 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2510 case GST_FORMAT_TIME:
2511 /* src_value + datastart = offset */
2512 GST_INFO_OBJECT (wavparse,
2513 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2515 if (wavparse->bps > 0)
2516 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2517 (guint64) wavparse->bps);
2518 else if (wavparse->fact) {
2519 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2520 wavparse->rate, wavparse->fact);
2523 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2534 case GST_FORMAT_DEFAULT:
2535 switch (*dest_format) {
2536 case GST_FORMAT_BYTES:
2537 *dest_value = src_value * wavparse->bytes_per_sample;
2539 case GST_FORMAT_TIME:
2540 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2541 (guint64) wavparse->rate);
2549 case GST_FORMAT_TIME:
2550 switch (*dest_format) {
2551 case GST_FORMAT_BYTES:
2552 if (wavparse->bps > 0)
2553 *dest_value = gst_util_uint64_scale (src_value,
2554 (guint64) wavparse->bps, GST_SECOND);
2556 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2557 wavparse->rate, wavparse->fact);
2559 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2561 /* make sure we end up on a sample boundary */
2562 *dest_value -= *dest_value % wavparse->blockalign;
2564 case GST_FORMAT_DEFAULT:
2565 *dest_value = gst_util_uint64_scale (src_value,
2566 (guint64) wavparse->rate, GST_SECOND);
2585 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2591 /* handle queries for location and length in requested format */
2593 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2595 gboolean res = TRUE;
2596 GstWavParse *wav = GST_WAVPARSE (parent);
2598 /* only if we know */
2599 if (wav->state != GST_WAVPARSE_DATA) {
2603 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2605 switch (GST_QUERY_TYPE (query)) {
2606 case GST_QUERY_POSITION:
2612 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2613 curb = wav->offset - wav->datastart;
2614 gst_query_parse_position (query, &format, NULL);
2615 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2618 case GST_FORMAT_BYTES:
2619 format = GST_FORMAT_BYTES;
2623 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2628 gst_query_set_position (query, format, cur);
2631 case GST_QUERY_DURATION:
2633 gint64 duration = 0;
2636 if (wav->ignore_length) {
2641 gst_query_parse_duration (query, &format, NULL);
2644 case GST_FORMAT_BYTES:{
2645 format = GST_FORMAT_BYTES;
2646 duration = wav->datasize;
2649 case GST_FORMAT_TIME:
2650 if ((res = gst_wavparse_calculate_duration (wav))) {
2651 duration = wav->duration;
2659 gst_query_set_duration (query, format, duration);
2662 case GST_QUERY_CONVERT:
2664 gint64 srcvalue, dstvalue;
2665 GstFormat srcformat, dstformat;
2667 gst_query_parse_convert (query, &srcformat, &srcvalue,
2668 &dstformat, &dstvalue);
2669 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2670 &dstformat, &dstvalue);
2672 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2675 case GST_QUERY_SEEKING:{
2677 gboolean seekable = FALSE;
2679 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2680 if (fmt == wav->segment.format) {
2681 if (wav->streaming) {
2684 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2685 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2686 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2687 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2689 gst_query_unref (q);
2691 GST_LOG_OBJECT (wav, "looping => seekable");
2695 } else if (fmt == GST_FORMAT_TIME) {
2699 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2704 res = gst_pad_query_default (pad, parent, query);
2711 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2713 GstWavParse *wavparse = GST_WAVPARSE (parent);
2714 gboolean res = FALSE;
2716 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2718 switch (GST_EVENT_TYPE (event)) {
2719 case GST_EVENT_SEEK:
2720 /* can only handle events when we are in the data state */
2721 if (wavparse->state == GST_WAVPARSE_DATA) {
2722 res = gst_wavparse_perform_seek (wavparse, event);
2724 gst_event_unref (event);
2727 case GST_EVENT_TOC_SELECT:
2730 GstTocEntry *entry = NULL;
2731 GstEvent *seek_event;
2734 if (!wavparse->toc) {
2735 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2738 gst_event_parse_toc_select (event, &uid);
2740 GST_OBJECT_LOCK (wavparse);
2741 entry = gst_toc_find_entry (wavparse->toc, uid);
2742 if (entry == NULL) {
2743 GST_OBJECT_UNLOCK (wavparse);
2744 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2748 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2749 GST_OBJECT_UNLOCK (wavparse);
2750 seek_event = gst_event_new_seek (1.0,
2752 GST_SEEK_FLAG_FLUSH,
2753 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2754 res = gst_wavparse_perform_seek (wavparse, seek_event);
2755 gst_event_unref (seek_event);
2759 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2763 gst_event_unref (event);
2768 res = gst_pad_push_event (wavparse->sinkpad, event);
2775 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2777 GstWavParse *wav = GST_WAVPARSE (parent);
2782 gst_adapter_clear (wav->adapter);
2783 g_object_unref (wav->adapter);
2784 wav->adapter = NULL;
2787 query = gst_query_new_scheduling ();
2789 if (!gst_pad_peer_query (sinkpad, query)) {
2790 gst_query_unref (query);
2794 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2795 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2796 gst_query_unref (query);
2801 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2802 wav->streaming = FALSE;
2803 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2807 GST_DEBUG_OBJECT (sinkpad, "activating push");
2808 wav->streaming = TRUE;
2809 wav->adapter = gst_adapter_new ();
2810 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2816 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2817 GstPadMode mode, gboolean active)
2822 case GST_PAD_MODE_PUSH:
2825 case GST_PAD_MODE_PULL:
2827 /* if we have a scheduler we can start the task */
2828 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2831 res = gst_pad_stop_task (sinkpad);
2841 static GstStateChangeReturn
2842 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2844 GstStateChangeReturn ret;
2845 GstWavParse *wav = GST_WAVPARSE (element);
2847 switch (transition) {
2848 case GST_STATE_CHANGE_NULL_TO_READY:
2850 case GST_STATE_CHANGE_READY_TO_PAUSED:
2851 gst_wavparse_reset (wav);
2853 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2859 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2861 switch (transition) {
2862 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2864 case GST_STATE_CHANGE_PAUSED_TO_READY:
2865 gst_wavparse_reset (wav);
2867 case GST_STATE_CHANGE_READY_TO_NULL:
2876 gst_wavparse_set_property (GObject * object, guint prop_id,
2877 const GValue * value, GParamSpec * pspec)
2881 g_return_if_fail (GST_IS_WAVPARSE (object));
2882 self = GST_WAVPARSE (object);
2885 case PROP_IGNORE_LENGTH:
2886 self->ignore_length = g_value_get_boolean (value);
2889 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2895 gst_wavparse_get_property (GObject * object, guint prop_id,
2896 GValue * value, GParamSpec * pspec)
2900 g_return_if_fail (GST_IS_WAVPARSE (object));
2901 self = GST_WAVPARSE (object);
2904 case PROP_IGNORE_LENGTH:
2905 g_value_set_boolean (value, self->ignore_length);
2908 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2913 plugin_init (GstPlugin * plugin)
2917 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2921 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2924 "Parse a .wav file into raw audio",
2925 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)