1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) <2006> Nokia Corporation, Stefan Kost <stefan.kost@nokia.com>.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-wavparse
25 * Parse a .wav file into raw or compressed audio.
27 * Wavparse supports both push and pull mode operations, making it possible to
28 * stream from a network source.
31 * <title>Example launch line</title>
33 * gst-launch-1.0 filesrc location=sine.wav ! wavparse ! audioconvert ! alsasink
34 * ]| Read a wav file and output to the soundcard using the ALSA element. The
35 * wav file is assumed to contain raw uncompressed samples.
37 * gst-launch-1.0 gnomevfssrc location=http://www.example.org/sine.wav ! queue ! wavparse ! audioconvert ! alsasink
38 * ]| Stream data from a network url.
44 * http://replaygain.hydrogenaudio.org/file_format_wav.html
54 #include "gstwavparse.h"
55 #include "gst/riff/riff-media.h"
56 #include <gst/base/gsttypefindhelper.h>
57 #include <gst/gst-i18n-plugin.h>
59 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
60 #define GST_CAT_DEFAULT (wavparse_debug)
62 #define GST_BWF_TAG_iXML GST_MAKE_FOURCC ('i','X','M','L')
63 #define GST_BWF_TAG_qlty GST_MAKE_FOURCC ('q','l','t','y')
64 #define GST_BWF_TAG_mext GST_MAKE_FOURCC ('m','e','x','t')
65 #define GST_BWF_TAG_levl GST_MAKE_FOURCC ('l','e','v','l')
66 #define GST_BWF_TAG_link GST_MAKE_FOURCC ('l','i','n','k')
67 #define GST_BWF_TAG_axml GST_MAKE_FOURCC ('a','x','m','l')
69 static void gst_wavparse_dispose (GObject * object);
71 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad,
73 static gboolean gst_wavparse_sink_activate_mode (GstPad * sinkpad,
74 GstObject * parent, GstPadMode mode, gboolean active);
75 static gboolean gst_wavparse_send_event (GstElement * element,
77 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
78 GstStateChange transition);
80 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
82 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
83 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
85 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
87 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
89 static void gst_wavparse_loop (GstPad * pad);
90 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
93 static void gst_wavparse_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_wavparse_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
98 #define DEFAULT_IGNORE_LENGTH FALSE
106 static GstStaticPadTemplate sink_template_factory =
107 GST_STATIC_PAD_TEMPLATE ("sink",
110 GST_STATIC_CAPS ("audio/x-wav")
114 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
116 #define gst_wavparse_parent_class parent_class
117 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
122 /* Offset Size Description Value
123 * 0x00 4 ID unique identification value
124 * 0x04 4 Position play order position
125 * 0x08 4 Data Chunk ID RIFF ID of corresponding data chunk
126 * 0x0c 4 Chunk Start Byte Offset of Data Chunk *
127 * 0x10 4 Block Start Byte Offset to sample of First Channel
128 * 0x14 4 Sample Offset Byte Offset to sample byte of First Channel
132 guint32 data_chunk_id;
135 guint32 sample_offset;
140 /* Offset Size Description Value
141 * 0x08 4 Cue Point ID 0 - 0xFFFFFFFF
144 guint32 cue_point_id;
146 } GstWavParseLabl, GstWavParseNote;
149 gst_wavparse_class_init (GstWavParseClass * klass)
151 GstElementClass *gstelement_class;
152 GObjectClass *object_class;
153 GstPadTemplate *src_template;
155 gstelement_class = (GstElementClass *) klass;
156 object_class = (GObjectClass *) klass;
158 parent_class = g_type_class_peek_parent (klass);
160 object_class->dispose = gst_wavparse_dispose;
162 object_class->set_property = gst_wavparse_set_property;
163 object_class->get_property = gst_wavparse_get_property;
166 * GstWavParse:ignore-length:
168 * This selects whether the length found in a data chunk
169 * should be ignored. This may be useful for streamed audio
170 * where the length is unknown until the end of streaming,
171 * and various software/hardware just puts some random value
172 * in there and hopes it doesn't break too much.
174 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
175 g_param_spec_boolean ("ignore-length",
177 "Ignore length from the Wave header",
178 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
181 gstelement_class->change_state = gst_wavparse_change_state;
182 gstelement_class->send_event = gst_wavparse_send_event;
185 gst_element_class_add_pad_template (gstelement_class,
186 gst_static_pad_template_get (&sink_template_factory));
188 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
189 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
190 gst_element_class_add_pad_template (gstelement_class, src_template);
192 gst_element_class_set_static_metadata (gstelement_class, "WAV audio demuxer",
193 "Codec/Demuxer/Audio",
194 "Parse a .wav file into raw audio",
195 "Erik Walthinsen <omega@cse.ogi.edu>");
199 gst_wavparse_reset (GstWavParse * wav)
201 wav->state = GST_WAVPARSE_START;
203 /* These will all be set correctly in the fmt chunk */
217 wav->got_fmt = FALSE;
221 gst_event_unref (wav->seek_event);
222 wav->seek_event = NULL;
224 gst_adapter_clear (wav->adapter);
225 g_object_unref (wav->adapter);
229 gst_tag_list_unref (wav->tags);
232 gst_toc_unref (wav->toc);
235 g_list_free_full (wav->cues, g_free);
238 g_list_free_full (wav->labls, g_free);
241 gst_caps_unref (wav->caps);
243 if (wav->start_segment)
244 gst_event_unref (wav->start_segment);
245 wav->start_segment = NULL;
249 gst_wavparse_dispose (GObject * object)
251 GstWavParse *wav = GST_WAVPARSE (object);
253 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
254 gst_wavparse_reset (wav);
256 G_OBJECT_CLASS (parent_class)->dispose (object);
260 gst_wavparse_init (GstWavParse * wavparse)
262 gst_wavparse_reset (wavparse);
266 gst_pad_new_from_static_template (&sink_template_factory, "sink");
267 gst_pad_set_activate_function (wavparse->sinkpad,
268 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
269 gst_pad_set_activatemode_function (wavparse->sinkpad,
270 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
271 gst_pad_set_chain_function (wavparse->sinkpad,
272 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
273 gst_pad_set_event_function (wavparse->sinkpad,
274 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
275 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
279 gst_pad_new_from_template (gst_element_class_get_pad_template
280 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
281 gst_pad_use_fixed_caps (wavparse->srcpad);
282 gst_pad_set_query_function (wavparse->srcpad,
283 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
284 gst_pad_set_event_function (wavparse->srcpad,
285 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
286 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
290 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
294 if (!gst_riff_parse_file_header (element, buf, &doctype))
297 if (doctype != GST_RIFF_RIFF_WAVE)
305 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
306 ("File is not a WAVE file: 0x%" G_GINT32_MODIFIER "x", doctype));
312 gst_wavparse_stream_init (GstWavParse * wav)
315 GstBuffer *buf = NULL;
317 if ((res = gst_pad_pull_range (wav->sinkpad,
318 wav->offset, 12, &buf)) != GST_FLOW_OK)
320 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
321 return GST_FLOW_ERROR;
329 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
331 /* -1 always maps to -1 */
337 /* 0 always maps to 0 */
344 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
346 } else if (wav->fact) {
348 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
349 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
356 /* This function is used to perform seeks on the element.
358 * It also works when event is NULL, in which case it will just
359 * start from the last configured segment. This technique is
360 * used when activating the element and to perform the seek in
364 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
368 GstFormat format, bformat;
370 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
371 gint64 cur, stop, upstream_size;
374 GstSegment seeksegment = { 0, };
378 GST_DEBUG_OBJECT (wav, "doing seek with event");
380 gst_event_parse_seek (event, &rate, &format, &flags,
381 &cur_type, &cur, &stop_type, &stop);
383 /* no negative rates yet */
387 if (format != wav->segment.format) {
388 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
389 gst_format_get_name (format),
390 gst_format_get_name (wav->segment.format));
392 if (cur_type != GST_SEEK_TYPE_NONE)
394 gst_pad_query_convert (wav->srcpad, format, cur,
395 wav->segment.format, &cur);
396 if (res && stop_type != GST_SEEK_TYPE_NONE)
398 gst_pad_query_convert (wav->srcpad, format, stop,
399 wav->segment.format, &stop);
403 format = wav->segment.format;
406 GST_DEBUG_OBJECT (wav, "doing seek without event");
409 cur_type = GST_SEEK_TYPE_SET;
410 stop_type = GST_SEEK_TYPE_SET;
413 /* in push mode, we must delegate to upstream */
414 if (wav->streaming) {
415 gboolean res = FALSE;
417 /* if streaming not yet started; only prepare initial newsegment */
418 if (!event || wav->state != GST_WAVPARSE_DATA) {
419 if (wav->start_segment)
420 gst_event_unref (wav->start_segment);
421 wav->start_segment = gst_event_new_segment (&wav->segment);
424 /* convert seek positions to byte positions in data sections */
425 if (format == GST_FORMAT_TIME) {
426 /* should not fail */
427 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
429 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
432 /* mind sample boundary and header */
434 cur -= (cur % wav->bytes_per_sample);
435 cur += wav->datastart;
438 stop -= (stop % wav->bytes_per_sample);
439 stop += wav->datastart;
441 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
442 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
444 /* BYTE seek event */
445 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
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). */
462 GST_DEBUG_OBJECT (wav, "sending flush start");
463 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
465 gst_pad_pause_task (wav->sinkpad);
468 /* we should now be able to grab the streaming thread because we stopped it
469 * with the above flush/pause code */
470 GST_PAD_STREAM_LOCK (wav->sinkpad);
472 /* save current position */
473 last_stop = wav->segment.position;
475 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
477 /* copy segment, we need this because we still need the old
478 * segment when we close the current segment. */
479 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
481 /* configure the seek parameters in the seeksegment. We will then have the
482 * right values in the segment to perform the seek */
484 GST_DEBUG_OBJECT (wav, "configuring seek");
485 gst_segment_do_seek (&seeksegment, rate, format, flags,
486 cur_type, cur, stop_type, stop, &update);
489 /* figure out the last position we need to play. If it's configured (stop !=
490 * -1), use that, else we play until the total duration of the file */
491 if ((stop = seeksegment.stop) == -1)
492 stop = seeksegment.duration;
494 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
495 if ((cur_type != GST_SEEK_TYPE_NONE)) {
496 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
497 * we can just copy the last_stop. If not, we use the bps to convert TIME to
499 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
500 (gint64 *) & wav->offset))
501 wav->offset = seeksegment.position;
502 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
503 wav->offset -= (wav->offset % wav->bytes_per_sample);
504 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
505 wav->offset += wav->datastart;
506 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
508 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
512 if (stop_type != GST_SEEK_TYPE_NONE) {
513 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
514 wav->end_offset = stop;
515 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
516 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
517 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
518 wav->end_offset += wav->datastart;
519 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
521 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
525 /* make sure filesize is not exceeded due to rounding errors or so,
526 * same precaution as in _stream_headers */
527 bformat = GST_FORMAT_BYTES;
528 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
529 wav->end_offset = MIN (wav->end_offset, upstream_size);
531 /* this is the range of bytes we will use for playback */
532 wav->offset = MIN (wav->offset, wav->end_offset);
533 wav->dataleft = wav->end_offset - wav->offset;
535 GST_DEBUG_OBJECT (wav,
536 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
537 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
538 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
540 /* prepare for streaming again */
542 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
543 GST_DEBUG_OBJECT (wav, "sending flush stop");
544 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
547 /* now we did the seek and can activate the new segment values */
548 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
550 /* if we're doing a segment seek, post a SEGMENT_START message */
551 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
552 gst_element_post_message (GST_ELEMENT_CAST (wav),
553 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
554 wav->segment.format, wav->segment.position));
557 /* now create the newsegment */
558 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
559 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
561 /* store the newsegment event so it can be sent from the streaming thread. */
562 if (wav->start_segment)
563 gst_event_unref (wav->start_segment);
564 wav->start_segment = gst_event_new_segment (&wav->segment);
566 /* mark discont if we are going to stream from another position. */
567 if (last_stop != wav->segment.position) {
568 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
572 /* and start the streaming task again */
573 if (!wav->streaming) {
574 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
578 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
585 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
590 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
595 GST_DEBUG_OBJECT (wav,
596 "Could not determine byte position for desired time");
602 * gst_wavparse_peek_chunk_info:
603 * @wav Wavparse object
604 * @tag holder for tag
605 * @size holder for tag size
607 * Peek next chunk info (tag and size)
609 * Returns: %TRUE when the chunk info (header) is available
612 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
614 const guint8 *data = NULL;
616 if (gst_adapter_available (wav->adapter) < 8)
619 data = gst_adapter_map (wav->adapter, 8);
620 *tag = GST_READ_UINT32_LE (data);
621 *size = GST_READ_UINT32_LE (data + 4);
622 gst_adapter_unmap (wav->adapter);
624 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
625 GST_FOURCC_ARGS (*tag));
631 * gst_wavparse_peek_chunk:
632 * @wav Wavparse object
633 * @tag holder for tag
634 * @size holder for tag size
636 * Peek enough data for one full chunk
638 * Returns: %TRUE when the full chunk is available
641 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
643 guint32 peek_size = 0;
646 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
649 /* size 0 -> empty data buffer would surprise most callers,
650 * large size -> do not bother trying to squeeze that into adapter,
651 * so we throw poor man's exception, which can be caught if caller really
652 * wants to handle 0 size chunk */
653 if (!(*size) || (*size) >= (1 << 30)) {
654 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
655 *size, GST_FOURCC_ARGS (*tag));
656 /* chain should give up */
657 wav->abort_buffering = TRUE;
660 peek_size = (*size + 1) & ~1;
661 available = gst_adapter_available (wav->adapter);
663 if (available >= (8 + peek_size)) {
666 GST_LOG ("but only %u bytes available now", available);
672 * gst_wavparse_calculate_duration:
673 * @wav: wavparse object
675 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
678 * Returns: %TRUE if duration is available.
681 gst_wavparse_calculate_duration (GstWavParse * wav)
683 if (wav->duration > 0)
687 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
689 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
691 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
692 GST_TIME_ARGS (wav->duration));
694 } else if (wav->fact) {
696 gst_util_uint64_scale_int_ceil (GST_SECOND, wav->fact, wav->rate);
697 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
698 GST_TIME_ARGS (wav->duration));
705 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
710 if (wav->streaming) {
711 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
714 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
715 GST_FOURCC_ARGS (tag));
716 flush = 8 + ((size + 1) & ~1);
717 wav->offset += flush;
718 if (wav->streaming) {
719 gst_adapter_flush (wav->adapter, flush);
721 gst_buffer_unref (buf);
728 * gst_wavparse_cue_chunk:
729 * @wav GstWavParse object
730 * @data holder for data
731 * @size holder for data size
733 * Parse cue chunk from @data to wav->cues.
735 * Returns: %TRUE when cue chunk is available
738 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
745 GST_WARNING_OBJECT (wav, "found another cue's");
749 ncues = GST_READ_UINT32_LE (data);
751 if (size < 4 + ncues * 24) {
752 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
758 for (i = 0; i < ncues; i++) {
759 cue = g_new0 (GstWavParseCue, 1);
760 cue->id = GST_READ_UINT32_LE (data);
761 cue->position = GST_READ_UINT32_LE (data + 4);
762 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
763 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
764 cue->block_start = GST_READ_UINT32_LE (data + 16);
765 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
766 cues = g_list_append (cues, cue);
776 * gst_wavparse_labl_chunk:
777 * @wav GstWavParse object
778 * @data holder for data
779 * @size holder for data size
781 * Parse labl from @data to wav->labls.
783 * Returns: %TRUE when labl chunk is available
786 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
788 GstWavParseLabl *labl;
793 labl = g_new0 (GstWavParseLabl, 1);
797 labl->cue_point_id = GST_READ_UINT32_LE (data);
798 labl->text = g_memdup (data + 4, size - 4);
800 wav->labls = g_list_append (wav->labls, labl);
806 * gst_wavparse_note_chunk:
807 * @wav GstWavParse object
808 * @data holder for data
809 * @size holder for data size
811 * Parse note from @data to wav->notes.
813 * Returns: %TRUE when note chunk is available
816 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
818 GstWavParseNote *note;
823 note = g_new0 (GstWavParseNote, 1);
827 note->cue_point_id = GST_READ_UINT32_LE (data);
828 note->text = g_memdup (data + 4, size - 4);
830 wav->notes = g_list_append (wav->notes, note);
836 * gst_wavparse_smpl_chunk:
837 * @wav GstWavParse object
838 * @data holder for data
839 * @size holder for data size
841 * Parse smpl chunk from @data.
843 * Returns: %TRUE when cue chunk is available
846 gst_wavparse_smpl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
851 manufacturer_id = GST_READ_UINT32_LE (data);
852 product_id = GST_READ_UINT32_LE (data + 4);
853 sample_period = GST_READ_UINT32_LE (data + 8);
855 note_number = GST_READ_UINT32_LE (data + 12);
857 pitch_fraction = GST_READ_UINT32_LE (data + 16);
858 SMPTE_format = GST_READ_UINT32_LE (data + 20);
859 SMPTE_offset = GST_READ_UINT32_LE (data + 24);
860 num_sample_loops = GST_READ_UINT32_LE (data + 28);
861 List of Sample Loops, 24 bytes each
865 wav->tags = gst_tag_list_new_empty ();
866 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
867 GST_TAG_MIDI_BASE_NOTE, (guint) note_number, NULL);
872 * gst_wavparse_adtl_chunk:
873 * @wav GstWavParse object
874 * @data holder for data
875 * @size holder for data size
877 * Parse adtl from @data.
879 * Returns: %TRUE when adtl chunk is available
882 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
884 guint32 ltag, lsize, offset = 0;
887 ltag = GST_READ_UINT32_LE (data + offset);
888 lsize = GST_READ_UINT32_LE (data + offset + 4);
890 case GST_RIFF_TAG_labl:
891 gst_wavparse_labl_chunk (wav, data + offset, size);
893 case GST_RIFF_TAG_note:
894 gst_wavparse_note_chunk (wav, data + offset, size);
897 GST_WARNING_OBJECT (wav, "Unknowm adtl %" GST_FOURCC_FORMAT,
898 GST_FOURCC_ARGS (ltag));
899 GST_MEMDUMP_OBJECT (wav, "Unknowm adtl", &data[offset], lsize);
902 offset += 8 + GST_ROUND_UP_2 (lsize);
903 size -= 8 + GST_ROUND_UP_2 (lsize);
910 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
912 GstTagList *tags = NULL;
913 GstTocEntry *entry = NULL;
915 entry = gst_toc_find_entry (toc, id);
917 tags = gst_toc_entry_get_tags (entry);
919 tags = gst_tag_list_new_empty ();
920 gst_toc_entry_set_tags (entry, tags);
928 * gst_wavparse_create_toc:
929 * @wav GstWavParse object
931 * Create TOC from wav->cues and wav->labls.
934 gst_wavparse_create_toc (GstWavParse * wav)
940 GstWavParseLabl *labl;
941 GstWavParseNote *note;
944 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
946 GST_OBJECT_LOCK (wav);
948 GST_OBJECT_UNLOCK (wav);
949 GST_WARNING_OBJECT (wav, "found another TOC");
954 GST_OBJECT_UNLOCK (wav);
958 /* FIXME: send CURRENT scope toc too */
959 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
961 /* add cue edition */
962 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
963 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
964 gst_toc_append_entry (toc, entry);
966 /* add tracks in cue edition */
970 prev_subentry = cur_subentry;
971 /* previous track stop time = current track start time */
972 if (prev_subentry != NULL) {
973 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
974 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
975 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
977 id = g_strdup_printf ("%08x", cue->id);
978 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
980 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
981 stop = wav->duration;
982 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
983 gst_toc_entry_append_sub_entry (entry, cur_subentry);
984 list = g_list_next (list);
987 /* add tags in tracks */
991 id = g_strdup_printf ("%08x", labl->cue_point_id);
992 tags = gst_wavparse_get_tags_toc_entry (toc, id);
995 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
998 list = g_list_next (list);
1003 id = g_strdup_printf ("%08x", note->cue_point_id);
1004 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1007 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1010 list = g_list_next (list);
1013 /* send data as TOC */
1016 /* send TOC event */
1018 GST_OBJECT_UNLOCK (wav);
1019 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1025 #define MAX_BUFFER_SIZE 4096
1027 static GstFlowReturn
1028 gst_wavparse_stream_headers (GstWavParse * wav)
1030 GstFlowReturn res = GST_FLOW_OK;
1031 GstBuffer *buf = NULL;
1032 gst_riff_strf_auds *header = NULL;
1034 gboolean gotdata = FALSE;
1035 GstCaps *caps = NULL;
1036 gchar *codec_name = NULL;
1038 gint64 upstream_size = 0;
1041 /* search for "_fmt" chunk, which should be first */
1042 while (!wav->got_fmt) {
1045 /* The header starts with a 'fmt ' tag */
1046 if (wav->streaming) {
1047 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1050 gst_adapter_flush (wav->adapter, 8);
1054 buf = gst_adapter_take_buffer (wav->adapter, size);
1056 gst_adapter_flush (wav->adapter, 1);
1057 wav->offset += GST_ROUND_UP_2 (size);
1059 buf = gst_buffer_new ();
1062 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1063 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1067 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1068 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1069 tag == GST_RIFF_TAG_LIST || tag == GST_RIFF_TAG_ID32 ||
1070 tag == GST_RIFF_TAG_id3 || tag == GST_RIFF_TAG_IDVX ||
1071 tag == GST_BWF_TAG_iXML || tag == GST_BWF_TAG_qlty ||
1072 tag == GST_BWF_TAG_mext || tag == GST_BWF_TAG_levl ||
1073 tag == GST_BWF_TAG_link || tag == GST_BWF_TAG_axml) {
1074 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1075 GST_FOURCC_ARGS (tag));
1076 gst_buffer_unref (buf);
1081 if (tag != GST_RIFF_TAG_fmt)
1084 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1086 goto parse_header_error;
1088 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1090 /* do sanity checks of header fields */
1091 if (header->channels == 0)
1093 if (header->rate == 0)
1096 GST_DEBUG_OBJECT (wav, "creating the caps");
1098 /* Note: gst_riff_create_audio_caps might need to fix values in
1099 * the header header depending on the format, so call it first */
1100 /* FIXME: Need to handle the channel reorder map */
1101 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1102 NULL, &codec_name, NULL);
1105 gst_buffer_unref (extra);
1108 goto unknown_format;
1110 /* If we got raw audio from upstream, we remove the codec_data field,
1111 * which may have been added if the wav header included an extended
1112 * chunk. We want to keep it for non raw audio.
1114 s = gst_caps_get_structure (caps, 0);
1115 if (s && gst_structure_has_name (s, "audio/x-raw")) {
1116 gst_structure_remove_field (s, "codec_data");
1119 /* do more sanity checks of header fields
1120 * (these can be sanitized by gst_riff_create_audio_caps()
1122 wav->format = header->format;
1123 wav->rate = header->rate;
1124 wav->channels = header->channels;
1125 wav->blockalign = header->blockalign;
1126 wav->depth = header->bits_per_sample;
1127 wav->av_bps = header->av_bps;
1133 /* do format specific handling */
1134 switch (wav->format) {
1135 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1136 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1138 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1139 * bitrate inside the mpeg stream */
1140 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1144 case GST_RIFF_WAVE_FORMAT_PCM:
1145 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1146 goto invalid_blockalign;
1149 if (wav->av_bps > wav->blockalign * wav->rate)
1151 /* use the configured bps */
1152 wav->bps = wav->av_bps;
1156 wav->width = (wav->blockalign * 8) / wav->channels;
1157 wav->bytes_per_sample = wav->channels * wav->width / 8;
1159 if (wav->bytes_per_sample <= 0)
1160 goto no_bytes_per_sample;
1162 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1163 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1164 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1165 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1166 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1167 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1168 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1170 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1171 * formats). This will make the element output a BYTE format segment and
1172 * will not timestamp the outgoing buffers.
1174 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1176 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1178 /* create pad later so we can sniff the first few bytes
1179 * of the real data and correct our caps if necessary */
1180 gst_caps_replace (&wav->caps, caps);
1181 gst_caps_replace (&caps, NULL);
1183 wav->got_fmt = TRUE;
1186 wav->tags = gst_tag_list_new_empty ();
1188 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1189 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1191 g_free (codec_name);
1197 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1198 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1200 /* loop headers until we get data */
1202 if (wav->streaming) {
1203 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1210 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1211 &buf)) != GST_FLOW_OK)
1212 goto header_read_error;
1213 gst_buffer_map (buf, &map, GST_MAP_READ);
1214 tag = GST_READ_UINT32_LE (map.data);
1215 size = GST_READ_UINT32_LE (map.data + 4);
1216 gst_buffer_unmap (buf, &map);
1219 GST_INFO_OBJECT (wav,
1220 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1221 GST_FOURCC_ARGS (tag), wav->offset);
1223 /* wav is a st00pid format, we don't know for sure where data starts.
1224 * So we have to go bit by bit until we find the 'data' header
1227 case GST_RIFF_TAG_data:{
1228 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1229 if (wav->ignore_length) {
1230 GST_DEBUG_OBJECT (wav, "Ignoring length");
1233 if (wav->streaming) {
1234 gst_adapter_flush (wav->adapter, 8);
1237 gst_buffer_unref (buf);
1240 wav->datastart = wav->offset;
1241 /* If size is zero, then the data chunk probably actually extends to
1242 the end of the file */
1243 if (size == 0 && upstream_size) {
1244 size = upstream_size - wav->datastart;
1246 /* Or the file might be truncated */
1247 else if (upstream_size) {
1248 size = MIN (size, (upstream_size - wav->datastart));
1250 wav->datasize = (guint64) size;
1251 wav->dataleft = (guint64) size;
1252 wav->end_offset = size + wav->datastart;
1253 if (!wav->streaming) {
1254 /* We will continue parsing tags 'till end */
1255 wav->offset += size;
1257 GST_DEBUG_OBJECT (wav, "datasize = %u", size);
1260 case GST_RIFF_TAG_fact:{
1261 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1262 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1263 const guint data_size = 4;
1265 GST_INFO_OBJECT (wav, "Have fact chunk");
1266 if (size < data_size) {
1267 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1268 /* need more data */
1271 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1275 /* number of samples (for compressed formats) */
1276 if (wav->streaming) {
1277 const guint8 *data = NULL;
1279 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1282 gst_adapter_flush (wav->adapter, 8);
1283 data = gst_adapter_map (wav->adapter, data_size);
1284 wav->fact = GST_READ_UINT32_LE (data);
1285 gst_adapter_unmap (wav->adapter);
1286 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1288 gst_buffer_unref (buf);
1291 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1292 data_size, &buf)) != GST_FLOW_OK)
1293 goto header_read_error;
1294 gst_buffer_extract (buf, 0, &wav->fact, 4);
1295 wav->fact = GUINT32_FROM_LE (wav->fact);
1296 gst_buffer_unref (buf);
1298 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1299 wav->offset += 8 + GST_ROUND_UP_2 (size);
1302 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1303 /* need more data */
1309 case GST_RIFF_TAG_acid:{
1310 const gst_riff_acid *acid = NULL;
1311 const guint data_size = sizeof (gst_riff_acid);
1314 GST_INFO_OBJECT (wav, "Have acid chunk");
1315 if (size < data_size) {
1316 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1317 /* need more data */
1320 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1324 if (wav->streaming) {
1325 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1328 gst_adapter_flush (wav->adapter, 8);
1329 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1331 tempo = acid->tempo;
1332 gst_adapter_unmap (wav->adapter);
1335 gst_buffer_unref (buf);
1338 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1339 size, &buf)) != GST_FLOW_OK)
1340 goto header_read_error;
1341 gst_buffer_map (buf, &map, GST_MAP_READ);
1342 acid = (const gst_riff_acid *) map.data;
1343 tempo = acid->tempo;
1344 gst_buffer_unmap (buf, &map);
1346 /* send data as tags */
1348 wav->tags = gst_tag_list_new_empty ();
1349 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1350 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1352 size = GST_ROUND_UP_2 (size);
1353 if (wav->streaming) {
1354 gst_adapter_flush (wav->adapter, size);
1356 gst_buffer_unref (buf);
1358 wav->offset += 8 + size;
1361 /* FIXME: all list tags after data are ignored in streaming mode */
1362 case GST_RIFF_TAG_LIST:{
1365 if (wav->streaming) {
1366 const guint8 *data = NULL;
1368 if (gst_adapter_available (wav->adapter) < 12) {
1371 data = gst_adapter_map (wav->adapter, 12);
1372 ltag = GST_READ_UINT32_LE (data + 8);
1373 gst_adapter_unmap (wav->adapter);
1375 gst_buffer_unref (buf);
1378 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1379 &buf)) != GST_FLOW_OK)
1380 goto header_read_error;
1381 gst_buffer_extract (buf, 8, <ag, 4);
1382 ltag = GUINT32_FROM_LE (ltag);
1385 case GST_RIFF_LIST_INFO:{
1386 const gint data_size = size - 4;
1389 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1390 if (wav->streaming) {
1391 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1394 gst_adapter_flush (wav->adapter, 12);
1396 if (data_size > 0) {
1397 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1399 gst_adapter_flush (wav->adapter, 1);
1403 gst_buffer_unref (buf);
1405 if (data_size > 0) {
1407 gst_pad_pull_range (wav->sinkpad, wav->offset,
1408 data_size, &buf)) != GST_FLOW_OK)
1409 goto header_read_error;
1412 if (data_size > 0) {
1414 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1416 GstTagList *old = wav->tags;
1418 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1420 gst_tag_list_unref (old);
1421 gst_tag_list_unref (new);
1423 gst_buffer_unref (buf);
1424 wav->offset += GST_ROUND_UP_2 (data_size);
1428 case GST_RIFF_LIST_adtl:{
1429 const gint data_size = size;
1431 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1432 if (wav->streaming) {
1433 const guint8 *data = NULL;
1435 gst_adapter_flush (wav->adapter, 12);
1436 data = gst_adapter_map (wav->adapter, data_size);
1437 gst_wavparse_adtl_chunk (wav, data, data_size);
1438 gst_adapter_unmap (wav->adapter);
1442 gst_buffer_unref (buf);
1445 gst_pad_pull_range (wav->sinkpad, wav->offset + 12,
1446 data_size, &buf)) != GST_FLOW_OK)
1447 goto header_read_error;
1448 gst_buffer_map (buf, &map, GST_MAP_READ);
1449 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1451 gst_buffer_unmap (buf, &map);
1453 wav->offset += GST_ROUND_UP_2 (data_size);
1457 GST_WARNING_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1458 GST_FOURCC_ARGS (ltag));
1459 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1460 /* need more data */
1466 case GST_RIFF_TAG_cue:{
1467 const guint data_size = size;
1469 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1470 if (wav->streaming) {
1471 const guint8 *data = NULL;
1473 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1476 gst_adapter_flush (wav->adapter, 8);
1478 data = gst_adapter_map (wav->adapter, data_size);
1479 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1480 goto header_read_error;
1482 gst_adapter_unmap (wav->adapter);
1487 gst_buffer_unref (buf);
1490 gst_pad_pull_range (wav->sinkpad, wav->offset,
1491 data_size, &buf)) != GST_FLOW_OK)
1492 goto header_read_error;
1493 gst_buffer_map (buf, &map, GST_MAP_READ);
1494 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1496 goto header_read_error;
1498 gst_buffer_unmap (buf, &map);
1500 size = GST_ROUND_UP_2 (size);
1501 if (wav->streaming) {
1502 gst_adapter_flush (wav->adapter, size);
1504 gst_buffer_unref (buf);
1506 size = GST_ROUND_UP_2 (size);
1507 wav->offset += size;
1510 case GST_RIFF_TAG_smpl:{
1511 const gint data_size = size;
1513 GST_DEBUG_OBJECT (wav, "Have 'smpl' TAG, size : %u", data_size);
1514 if (wav->streaming) {
1515 const guint8 *data = NULL;
1517 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1520 gst_adapter_flush (wav->adapter, 8);
1522 data = gst_adapter_map (wav->adapter, data_size);
1523 if (!gst_wavparse_smpl_chunk (wav, data, data_size)) {
1524 goto header_read_error;
1526 gst_adapter_unmap (wav->adapter);
1531 gst_buffer_unref (buf);
1534 gst_pad_pull_range (wav->sinkpad, wav->offset,
1535 data_size, &buf)) != GST_FLOW_OK)
1536 goto header_read_error;
1537 gst_buffer_map (buf, &map, GST_MAP_READ);
1538 if (!gst_wavparse_smpl_chunk (wav, (const guint8 *) map.data,
1540 goto header_read_error;
1542 gst_buffer_unmap (buf, &map);
1544 size = GST_ROUND_UP_2 (size);
1545 if (wav->streaming) {
1546 gst_adapter_flush (wav->adapter, size);
1548 gst_buffer_unref (buf);
1550 size = GST_ROUND_UP_2 (size);
1551 wav->offset += size;
1555 GST_WARNING_OBJECT (wav, "Ignoring chunk %" GST_FOURCC_FORMAT,
1556 GST_FOURCC_ARGS (tag));
1557 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1558 /* need more data */
1563 if (upstream_size && (wav->offset >= upstream_size)) {
1564 /* Now we are gone through the whole file */
1569 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1571 if (wav->bps <= 0 && wav->fact) {
1573 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1575 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1576 (guint64) wav->fact);
1577 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1582 if (gst_wavparse_calculate_duration (wav)) {
1583 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1584 if (!wav->ignore_length)
1585 wav->segment.duration = wav->duration;
1587 gst_wavparse_create_toc (wav);
1589 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1590 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1591 if (!wav->ignore_length)
1592 wav->segment.duration = wav->datasize;
1595 /* now we have all the info to perform a pending seek if any, if no
1596 * event, this will still do the right thing and it will also send
1597 * the right newsegment event downstream. */
1598 gst_wavparse_perform_seek (wav, wav->seek_event);
1599 /* remove pending event */
1600 event_p = &wav->seek_event;
1601 gst_event_replace (event_p, NULL);
1603 /* we just started, we are discont */
1604 wav->discont = TRUE;
1606 wav->state = GST_WAVPARSE_DATA;
1608 /* determine reasonable max buffer size,
1609 * that is, buffers not too small either size or time wise
1610 * so we do not end up with too many of them */
1612 if (gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size))
1613 wav->max_buf_size = upstream_size;
1615 wav->max_buf_size = 0;
1616 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1617 if (wav->blockalign > 0)
1618 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1620 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1628 g_free (codec_name);
1632 gst_caps_unref (caps);
1637 res = GST_FLOW_ERROR;
1642 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1643 ("Invalid WAV header (no fmt at start): %"
1644 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1649 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1650 ("Couldn't parse audio header"));
1655 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1656 ("Stream claims to contain no channels - invalid data"));
1661 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1662 ("Stream with sample_rate == 0 - invalid data"));
1667 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1668 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1669 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1674 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1675 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1676 wav->av_bps, wav->blockalign * wav->rate));
1679 no_bytes_per_sample:
1681 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1682 ("Could not caluclate bytes per sample - invalid data"));
1687 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1688 ("No caps found for format 0x%x, %u channels, %u Hz",
1689 wav->format, wav->channels, wav->rate));
1694 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1695 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1701 * Read WAV file tag when streaming
1703 static GstFlowReturn
1704 gst_wavparse_parse_stream_init (GstWavParse * wav)
1706 if (gst_adapter_available (wav->adapter) >= 12) {
1709 /* _take flushes the data */
1710 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1712 GST_DEBUG ("Parsing wav header");
1713 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1714 return GST_FLOW_ERROR;
1717 /* Go to next state */
1718 wav->state = GST_WAVPARSE_HEADER;
1723 /* handle an event sent directly to the element.
1725 * This event can be sent either in the READY state or the
1726 * >READY state. The only event of interest really is the seek
1729 * In the READY state we can only store the event and try to
1730 * respect it when going to PAUSED. We assume we are in the
1731 * READY state when our parsing state != GST_WAVPARSE_DATA.
1733 * When we are steaming, we can simply perform the seek right
1737 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1739 GstWavParse *wav = GST_WAVPARSE (element);
1740 gboolean res = FALSE;
1743 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1745 switch (GST_EVENT_TYPE (event)) {
1746 case GST_EVENT_SEEK:
1747 if (wav->state == GST_WAVPARSE_DATA) {
1748 /* we can handle the seek directly when streaming data */
1749 res = gst_wavparse_perform_seek (wav, event);
1751 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1753 event_p = &wav->seek_event;
1754 gst_event_replace (event_p, event);
1756 /* we always return true */
1763 gst_event_unref (event);
1768 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1772 s = gst_caps_get_structure (caps, 0);
1773 if (!gst_structure_has_name (s, "audio/x-dts"))
1775 if (prob >= GST_TYPE_FIND_LIKELY)
1777 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1778 if (prob < GST_TYPE_FIND_POSSIBLE)
1780 /* .. in which case we want at least a valid-looking rate and channels */
1781 if (!gst_structure_has_field (s, "channels"))
1783 /* and for extra assurance we could also check the rate from the DTS frame
1784 * against the one in the wav header, but for now let's not do that */
1785 return gst_structure_has_field (s, "rate");
1789 gst_wavparse_get_upstream_tags (GstWavParse * wav, GstTagScope scope)
1791 GstTagList *tags = NULL;
1796 while ((ev = gst_pad_get_sticky_event (wav->sinkpad, GST_EVENT_TAG, i++))) {
1797 gst_event_parse_tag (ev, &tags);
1798 if (tags != NULL && gst_tag_list_get_scope (tags) == scope) {
1799 tags = gst_tag_list_copy (tags);
1800 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1801 gst_event_unref (ev);
1805 gst_event_unref (ev);
1811 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1814 GstTagList *tags, *utags;
1816 GST_DEBUG_OBJECT (wav, "adding src pad");
1818 g_assert (wav->caps != NULL);
1820 s = gst_caps_get_structure (wav->caps, 0);
1821 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1822 GstTypeFindProbability prob;
1825 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1826 if (tf_caps != NULL) {
1827 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1828 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1829 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1830 gst_caps_unref (wav->caps);
1831 wav->caps = tf_caps;
1833 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1834 GST_TAG_AUDIO_CODEC, "dts", NULL);
1836 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1837 "marked as raw PCM audio, but ignoring for now", tf_caps);
1838 gst_caps_unref (tf_caps);
1843 gst_pad_set_caps (wav->srcpad, wav->caps);
1844 gst_caps_replace (&wav->caps, NULL);
1846 if (wav->start_segment) {
1847 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1848 gst_pad_push_event (wav->srcpad, wav->start_segment);
1849 wav->start_segment = NULL;
1852 /* upstream tags, e.g. from id3/ape tag before the wav file; assume for now
1853 * that there'll be only one scope/type of tag list from upstream, if any */
1854 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_GLOBAL);
1856 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_STREAM);
1858 /* if there's a tag upstream it's probably been added to override the
1859 * tags from inside the wav header, so keep upstream tags if in doubt */
1860 tags = gst_tag_list_merge (utags, wav->tags, GST_TAG_MERGE_KEEP);
1862 if (wav->tags != NULL) {
1863 gst_tag_list_unref (wav->tags);
1868 gst_tag_list_unref (utags);
1870 /* send tags downstream, if any */
1872 gst_pad_push_event (wav->srcpad, gst_event_new_tag (tags));
1875 static GstFlowReturn
1876 gst_wavparse_stream_data (GstWavParse * wav)
1878 GstBuffer *buf = NULL;
1879 GstFlowReturn res = GST_FLOW_OK;
1880 guint64 desired, obtained;
1881 GstClockTime timestamp, next_timestamp, duration;
1882 guint64 pos, nextpos;
1885 GST_LOG_OBJECT (wav,
1886 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1887 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1889 /* Get the next n bytes and output them */
1890 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1893 /* scale the amount of data by the segment rate so we get equal
1894 * amounts of data regardless of the playback rate */
1896 MIN (gst_guint64_to_gdouble (wav->dataleft),
1897 wav->max_buf_size * ABS (wav->segment.rate));
1899 if (desired >= wav->blockalign && wav->blockalign > 0)
1900 desired -= (desired % wav->blockalign);
1902 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1903 "from the sinkpad", desired);
1905 if (wav->streaming) {
1906 guint avail = gst_adapter_available (wav->adapter);
1909 /* flush some bytes if evil upstream sends segment that starts
1910 * before data or does is not send sample aligned segment */
1911 if (G_LIKELY (wav->offset >= wav->datastart)) {
1912 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1914 extra = wav->datastart - wav->offset;
1917 if (G_UNLIKELY (extra)) {
1918 extra = wav->bytes_per_sample - extra;
1919 if (extra <= avail) {
1920 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
1921 gst_adapter_flush (wav->adapter, extra);
1922 wav->offset += extra;
1923 wav->dataleft -= extra;
1924 goto iterate_adapter;
1926 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
1927 gst_adapter_clear (wav->adapter);
1928 wav->offset += avail;
1929 wav->dataleft -= avail;
1934 if (avail < desired) {
1935 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
1939 buf = gst_adapter_take_buffer (wav->adapter, desired);
1941 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1942 desired, &buf)) != GST_FLOW_OK)
1945 /* we may get a short buffer at the end of the file */
1946 if (gst_buffer_get_size (buf) < desired) {
1947 gsize size = gst_buffer_get_size (buf);
1949 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
1950 if (size >= wav->blockalign) {
1951 if (wav->blockalign > 0) {
1952 buf = gst_buffer_make_writable (buf);
1953 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
1956 gst_buffer_unref (buf);
1962 obtained = gst_buffer_get_size (buf);
1964 /* our positions in bytes */
1965 pos = wav->offset - wav->datastart;
1966 nextpos = pos + obtained;
1968 /* update offsets, does not overflow. */
1969 buf = gst_buffer_make_writable (buf);
1970 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1971 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1973 /* first chunk of data? create the source pad. We do this only here so
1974 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1975 if (G_UNLIKELY (wav->first)) {
1977 /* this will also push the segment events */
1978 gst_wavparse_add_src_pad (wav, buf);
1980 /* If we have a pending start segment, send it now. */
1981 if (G_UNLIKELY (wav->start_segment != NULL)) {
1982 gst_pad_push_event (wav->srcpad, wav->start_segment);
1983 wav->start_segment = NULL;
1988 /* and timestamps if we have a bitrate, be careful for overflows */
1990 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
1992 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
1993 duration = next_timestamp - timestamp;
1995 /* update current running segment position */
1996 if (G_LIKELY (next_timestamp >= wav->segment.start))
1997 wav->segment.position = next_timestamp;
1998 } else if (wav->fact) {
2000 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2001 /* and timestamps if we have a bitrate, be careful for overflows */
2002 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2003 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2004 duration = next_timestamp - timestamp;
2006 /* no bitrate, all we know is that the first sample has timestamp 0, all
2007 * other positions and durations have unknown timestamp. */
2011 timestamp = GST_CLOCK_TIME_NONE;
2012 duration = GST_CLOCK_TIME_NONE;
2013 /* update current running segment position with byte offset */
2014 if (G_LIKELY (nextpos >= wav->segment.start))
2015 wav->segment.position = nextpos;
2017 if ((pos > 0) && wav->vbr) {
2018 /* don't set timestamps for VBR files if it's not the first buffer */
2019 timestamp = GST_CLOCK_TIME_NONE;
2020 duration = GST_CLOCK_TIME_NONE;
2023 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2024 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2025 wav->discont = FALSE;
2028 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2029 GST_BUFFER_DURATION (buf) = duration;
2031 GST_LOG_OBJECT (wav,
2032 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2033 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2034 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2036 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2039 if (obtained < wav->dataleft) {
2040 wav->offset += obtained;
2041 wav->dataleft -= obtained;
2043 wav->offset += wav->dataleft;
2047 /* Iterate until need more data, so adapter size won't grow */
2048 if (wav->streaming) {
2049 GST_LOG_OBJECT (wav,
2050 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2052 goto iterate_adapter;
2059 GST_DEBUG_OBJECT (wav, "found EOS");
2060 return GST_FLOW_EOS;
2064 /* check if we got EOS */
2065 if (res == GST_FLOW_EOS)
2068 GST_WARNING_OBJECT (wav,
2069 "Error getting %" G_GINT64_FORMAT " bytes from the "
2070 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2075 GST_INFO_OBJECT (wav,
2076 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2077 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2078 gst_pad_is_linked (wav->srcpad));
2084 gst_wavparse_loop (GstPad * pad)
2087 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2091 GST_LOG_OBJECT (wav, "process data");
2093 switch (wav->state) {
2094 case GST_WAVPARSE_START:
2095 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2096 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2100 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2101 event = gst_event_new_stream_start (stream_id);
2102 gst_event_set_group_id (event, gst_util_group_id_next ());
2103 gst_pad_push_event (wav->srcpad, event);
2106 wav->state = GST_WAVPARSE_HEADER;
2109 case GST_WAVPARSE_HEADER:
2110 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2111 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2114 wav->state = GST_WAVPARSE_DATA;
2115 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2118 case GST_WAVPARSE_DATA:
2119 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2123 g_assert_not_reached ();
2130 const gchar *reason = gst_flow_get_name (ret);
2132 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2133 gst_pad_pause_task (pad);
2135 if (ret == GST_FLOW_EOS) {
2136 /* handle end-of-stream/segment */
2137 /* so align our position with the end of it, if there is one
2138 * this ensures a subsequent will arrive at correct base/acc time */
2139 if (wav->segment.format == GST_FORMAT_TIME) {
2140 if (wav->segment.rate > 0.0 &&
2141 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2142 wav->segment.position = wav->segment.stop;
2143 else if (wav->segment.rate < 0.0)
2144 wav->segment.position = wav->segment.start;
2146 if (wav->state == GST_WAVPARSE_START) {
2147 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2148 ("No valid input found before end of stream"));
2149 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2151 /* add pad before we perform EOS */
2152 if (G_UNLIKELY (wav->first)) {
2154 gst_wavparse_add_src_pad (wav, NULL);
2157 /* perform EOS logic */
2158 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2161 if ((stop = wav->segment.stop) == -1)
2162 stop = wav->segment.duration;
2164 gst_element_post_message (GST_ELEMENT_CAST (wav),
2165 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2166 wav->segment.format, stop));
2167 gst_pad_push_event (wav->srcpad,
2168 gst_event_new_segment_done (wav->segment.format, stop));
2170 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2173 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2174 /* for fatal errors we post an error message, post the error
2175 * first so the app knows about the error first. */
2176 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2177 (_("Internal data flow error.")),
2178 ("streaming task paused, reason %s (%d)", reason, ret));
2179 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2185 static GstFlowReturn
2186 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2189 GstWavParse *wav = GST_WAVPARSE (parent);
2191 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2192 gst_buffer_get_size (buf));
2194 gst_adapter_push (wav->adapter, buf);
2196 switch (wav->state) {
2197 case GST_WAVPARSE_START:
2198 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2199 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2202 if (wav->state != GST_WAVPARSE_HEADER)
2205 /* otherwise fall-through */
2206 case GST_WAVPARSE_HEADER:
2207 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2208 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2211 if (!wav->got_fmt || wav->datastart == 0)
2214 wav->state = GST_WAVPARSE_DATA;
2215 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2218 case GST_WAVPARSE_DATA:
2219 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2220 wav->discont = TRUE;
2221 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2225 g_return_val_if_reached (GST_FLOW_ERROR);
2228 if (G_UNLIKELY (wav->abort_buffering)) {
2229 wav->abort_buffering = FALSE;
2230 ret = GST_FLOW_ERROR;
2231 /* sort of demux/parse error */
2232 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2238 static GstFlowReturn
2239 gst_wavparse_flush_data (GstWavParse * wav)
2241 GstFlowReturn ret = GST_FLOW_OK;
2244 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2246 wav->end_offset = wav->offset + av;
2247 ret = gst_wavparse_stream_data (wav);
2254 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2256 GstWavParse *wav = GST_WAVPARSE (parent);
2257 gboolean ret = TRUE;
2259 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2261 switch (GST_EVENT_TYPE (event)) {
2262 case GST_EVENT_CAPS:
2264 /* discard, we'll come up with proper src caps */
2265 gst_event_unref (event);
2268 case GST_EVENT_SEGMENT:
2270 gint64 start, stop, offset = 0, end_offset = -1;
2273 /* some debug output */
2274 gst_event_copy_segment (event, &segment);
2275 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2278 if (wav->state != GST_WAVPARSE_DATA) {
2279 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2283 /* now we are either committed to TIME or BYTE format,
2284 * and we only expect a BYTE segment, e.g. following a seek */
2285 if (segment.format == GST_FORMAT_BYTES) {
2286 /* handle (un)signed issues */
2287 start = segment.start;
2288 stop = segment.stop;
2291 start -= wav->datastart;
2292 start = MAX (start, 0);
2296 segment.stop -= wav->datastart;
2297 segment.stop = MAX (stop, 0);
2299 if (wav->segment.format == GST_FORMAT_TIME) {
2300 guint64 bps = wav->bps;
2302 /* operating in format TIME, so we can convert */
2303 if (!bps && wav->fact)
2305 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2309 gst_util_uint64_scale_ceil (start, GST_SECOND,
2310 (guint64) wav->bps);
2313 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2314 (guint64) wav->bps);
2318 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2322 segment.start = start;
2323 segment.stop = stop;
2325 /* accept upstream's notion of segment and distribute along */
2326 segment.format = wav->segment.format;
2327 segment.time = segment.position = segment.start;
2328 segment.duration = wav->segment.duration;
2329 segment.base = gst_segment_to_running_time (&wav->segment,
2330 GST_FORMAT_TIME, wav->segment.position);
2332 gst_segment_copy_into (&segment, &wav->segment);
2334 /* also store the newsegment event for the streaming thread */
2335 if (wav->start_segment)
2336 gst_event_unref (wav->start_segment);
2337 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2338 wav->start_segment = gst_event_new_segment (&segment);
2340 /* stream leftover data in current segment */
2341 gst_wavparse_flush_data (wav);
2342 /* and set up streaming thread for next one */
2343 wav->offset = offset;
2344 wav->end_offset = end_offset;
2345 if (wav->end_offset > 0) {
2346 wav->dataleft = wav->end_offset - wav->offset;
2348 /* infinity; upstream will EOS when done */
2349 wav->dataleft = G_MAXUINT64;
2352 gst_event_unref (event);
2356 if (wav->state == GST_WAVPARSE_START) {
2357 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2358 ("No valid input found before end of stream"));
2360 /* add pad if needed so EOS is seen downstream */
2361 if (G_UNLIKELY (wav->first)) {
2363 gst_wavparse_add_src_pad (wav, NULL);
2365 /* stream leftover data in current segment */
2366 gst_wavparse_flush_data (wav);
2371 case GST_EVENT_FLUSH_STOP:
2375 gst_adapter_clear (wav->adapter);
2376 wav->discont = TRUE;
2377 dur = wav->segment.duration;
2378 gst_segment_init (&wav->segment, wav->segment.format);
2379 wav->segment.duration = dur;
2383 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2391 /* convert and query stuff */
2392 static const GstFormat *
2393 gst_wavparse_get_formats (GstPad * pad)
2395 static GstFormat formats[] = {
2398 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2407 gst_wavparse_pad_convert (GstPad * pad,
2408 GstFormat src_format, gint64 src_value,
2409 GstFormat * dest_format, gint64 * dest_value)
2411 GstWavParse *wavparse;
2412 gboolean res = TRUE;
2414 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2416 if (*dest_format == src_format) {
2417 *dest_value = src_value;
2421 if ((wavparse->bps == 0) && !wavparse->fact)
2424 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2425 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2427 switch (src_format) {
2428 case GST_FORMAT_BYTES:
2429 switch (*dest_format) {
2430 case GST_FORMAT_DEFAULT:
2431 *dest_value = src_value / wavparse->bytes_per_sample;
2432 /* make sure we end up on a sample boundary */
2433 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2435 case GST_FORMAT_TIME:
2436 /* src_value + datastart = offset */
2437 GST_INFO_OBJECT (wavparse,
2438 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2440 if (wavparse->bps > 0)
2441 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2442 (guint64) wavparse->bps);
2443 else if (wavparse->fact) {
2444 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2445 wavparse->rate, wavparse->fact);
2448 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2459 case GST_FORMAT_DEFAULT:
2460 switch (*dest_format) {
2461 case GST_FORMAT_BYTES:
2462 *dest_value = src_value * wavparse->bytes_per_sample;
2464 case GST_FORMAT_TIME:
2465 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2466 (guint64) wavparse->rate);
2474 case GST_FORMAT_TIME:
2475 switch (*dest_format) {
2476 case GST_FORMAT_BYTES:
2477 if (wavparse->bps > 0)
2478 *dest_value = gst_util_uint64_scale (src_value,
2479 (guint64) wavparse->bps, GST_SECOND);
2481 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2482 wavparse->rate, wavparse->fact);
2484 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2486 /* make sure we end up on a sample boundary */
2487 *dest_value -= *dest_value % wavparse->blockalign;
2489 case GST_FORMAT_DEFAULT:
2490 *dest_value = gst_util_uint64_scale (src_value,
2491 (guint64) wavparse->rate, GST_SECOND);
2510 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2516 /* handle queries for location and length in requested format */
2518 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2520 gboolean res = TRUE;
2521 GstWavParse *wav = GST_WAVPARSE (parent);
2523 /* only if we know */
2524 if (wav->state != GST_WAVPARSE_DATA) {
2528 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2530 switch (GST_QUERY_TYPE (query)) {
2531 case GST_QUERY_POSITION:
2537 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2538 curb = wav->offset - wav->datastart;
2539 gst_query_parse_position (query, &format, NULL);
2540 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2543 case GST_FORMAT_BYTES:
2544 format = GST_FORMAT_BYTES;
2548 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2553 gst_query_set_position (query, format, cur);
2556 case GST_QUERY_DURATION:
2558 gint64 duration = 0;
2561 if (wav->ignore_length) {
2566 gst_query_parse_duration (query, &format, NULL);
2569 case GST_FORMAT_BYTES:{
2570 format = GST_FORMAT_BYTES;
2571 duration = wav->datasize;
2574 case GST_FORMAT_TIME:
2575 if ((res = gst_wavparse_calculate_duration (wav))) {
2576 duration = wav->duration;
2584 gst_query_set_duration (query, format, duration);
2587 case GST_QUERY_CONVERT:
2589 gint64 srcvalue, dstvalue;
2590 GstFormat srcformat, dstformat;
2592 gst_query_parse_convert (query, &srcformat, &srcvalue,
2593 &dstformat, &dstvalue);
2594 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2595 &dstformat, &dstvalue);
2597 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2600 case GST_QUERY_SEEKING:{
2602 gboolean seekable = FALSE;
2604 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2605 if (fmt == wav->segment.format) {
2606 if (wav->streaming) {
2609 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2610 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2611 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2612 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2614 gst_query_unref (q);
2616 GST_LOG_OBJECT (wav, "looping => seekable");
2620 } else if (fmt == GST_FORMAT_TIME) {
2624 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2629 res = gst_pad_query_default (pad, parent, query);
2636 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2638 GstWavParse *wavparse = GST_WAVPARSE (parent);
2639 gboolean res = FALSE;
2641 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2643 switch (GST_EVENT_TYPE (event)) {
2644 case GST_EVENT_SEEK:
2645 /* can only handle events when we are in the data state */
2646 if (wavparse->state == GST_WAVPARSE_DATA) {
2647 res = gst_wavparse_perform_seek (wavparse, event);
2649 gst_event_unref (event);
2652 case GST_EVENT_TOC_SELECT:
2655 GstTocEntry *entry = NULL;
2656 GstEvent *seek_event;
2659 if (!wavparse->toc) {
2660 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2663 gst_event_parse_toc_select (event, &uid);
2665 GST_OBJECT_LOCK (wavparse);
2666 entry = gst_toc_find_entry (wavparse->toc, uid);
2667 if (entry == NULL) {
2668 GST_OBJECT_UNLOCK (wavparse);
2669 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2673 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2674 GST_OBJECT_UNLOCK (wavparse);
2675 seek_event = gst_event_new_seek (1.0,
2677 GST_SEEK_FLAG_FLUSH,
2678 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2679 res = gst_wavparse_perform_seek (wavparse, seek_event);
2680 gst_event_unref (seek_event);
2684 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2688 gst_event_unref (event);
2693 res = gst_pad_push_event (wavparse->sinkpad, event);
2700 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2702 GstWavParse *wav = GST_WAVPARSE (parent);
2707 gst_adapter_clear (wav->adapter);
2708 g_object_unref (wav->adapter);
2709 wav->adapter = NULL;
2712 query = gst_query_new_scheduling ();
2714 if (!gst_pad_peer_query (sinkpad, query)) {
2715 gst_query_unref (query);
2719 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2720 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2721 gst_query_unref (query);
2726 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2727 wav->streaming = FALSE;
2728 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2732 GST_DEBUG_OBJECT (sinkpad, "activating push");
2733 wav->streaming = TRUE;
2734 wav->adapter = gst_adapter_new ();
2735 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2741 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2742 GstPadMode mode, gboolean active)
2747 case GST_PAD_MODE_PUSH:
2750 case GST_PAD_MODE_PULL:
2752 /* if we have a scheduler we can start the task */
2753 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2756 res = gst_pad_stop_task (sinkpad);
2766 static GstStateChangeReturn
2767 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2769 GstStateChangeReturn ret;
2770 GstWavParse *wav = GST_WAVPARSE (element);
2772 switch (transition) {
2773 case GST_STATE_CHANGE_NULL_TO_READY:
2775 case GST_STATE_CHANGE_READY_TO_PAUSED:
2776 gst_wavparse_reset (wav);
2778 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2784 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2786 switch (transition) {
2787 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2789 case GST_STATE_CHANGE_PAUSED_TO_READY:
2790 gst_wavparse_reset (wav);
2792 case GST_STATE_CHANGE_READY_TO_NULL:
2801 gst_wavparse_set_property (GObject * object, guint prop_id,
2802 const GValue * value, GParamSpec * pspec)
2806 g_return_if_fail (GST_IS_WAVPARSE (object));
2807 self = GST_WAVPARSE (object);
2810 case PROP_IGNORE_LENGTH:
2811 self->ignore_length = g_value_get_boolean (value);
2814 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2820 gst_wavparse_get_property (GObject * object, guint prop_id,
2821 GValue * value, GParamSpec * pspec)
2825 g_return_if_fail (GST_IS_WAVPARSE (object));
2826 self = GST_WAVPARSE (object);
2829 case PROP_IGNORE_LENGTH:
2830 g_value_set_boolean (value, self->ignore_length);
2833 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2838 plugin_init (GstPlugin * plugin)
2842 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2846 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2849 "Parse a .wav file into raw audio",
2850 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)