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 /* Data size chunk of RF64,
70 * see http://tech.ebu.ch/docs/tech/tech3306-2009.pdf */
71 #define GST_RS64_TAG_DS64 GST_MAKE_FOURCC ('d','s','6','4')
73 static void gst_wavparse_dispose (GObject * object);
75 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad,
77 static gboolean gst_wavparse_sink_activate_mode (GstPad * sinkpad,
78 GstObject * parent, GstPadMode mode, gboolean active);
79 static gboolean gst_wavparse_send_event (GstElement * element,
81 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
82 GstStateChange transition);
84 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
86 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
87 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
89 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
91 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
93 static void gst_wavparse_loop (GstPad * pad);
94 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
97 static void gst_wavparse_set_property (GObject * object, guint prop_id,
98 const GValue * value, GParamSpec * pspec);
99 static void gst_wavparse_get_property (GObject * object, guint prop_id,
100 GValue * value, GParamSpec * pspec);
102 #define DEFAULT_IGNORE_LENGTH FALSE
110 static GstStaticPadTemplate sink_template_factory =
111 GST_STATIC_PAD_TEMPLATE ("sink",
114 GST_STATIC_CAPS ("audio/x-wav")
118 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
120 #define gst_wavparse_parent_class parent_class
121 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
126 /* Offset Size Description Value
127 * 0x00 4 ID unique identification value
128 * 0x04 4 Position play order position
129 * 0x08 4 Data Chunk ID RIFF ID of corresponding data chunk
130 * 0x0c 4 Chunk Start Byte Offset of Data Chunk *
131 * 0x10 4 Block Start Byte Offset to sample of First Channel
132 * 0x14 4 Sample Offset Byte Offset to sample byte of First Channel
136 guint32 data_chunk_id;
139 guint32 sample_offset;
144 /* Offset Size Description Value
145 * 0x08 4 Cue Point ID 0 - 0xFFFFFFFF
148 guint32 cue_point_id;
150 } GstWavParseLabl, GstWavParseNote;
153 gst_wavparse_class_init (GstWavParseClass * klass)
155 GstElementClass *gstelement_class;
156 GObjectClass *object_class;
157 GstPadTemplate *src_template;
159 gstelement_class = (GstElementClass *) klass;
160 object_class = (GObjectClass *) klass;
162 parent_class = g_type_class_peek_parent (klass);
164 object_class->dispose = gst_wavparse_dispose;
166 object_class->set_property = gst_wavparse_set_property;
167 object_class->get_property = gst_wavparse_get_property;
170 * GstWavParse:ignore-length:
172 * This selects whether the length found in a data chunk
173 * should be ignored. This may be useful for streamed audio
174 * where the length is unknown until the end of streaming,
175 * and various software/hardware just puts some random value
176 * in there and hopes it doesn't break too much.
178 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
179 g_param_spec_boolean ("ignore-length",
181 "Ignore length from the Wave header",
182 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
185 gstelement_class->change_state = gst_wavparse_change_state;
186 gstelement_class->send_event = gst_wavparse_send_event;
189 gst_element_class_add_pad_template (gstelement_class,
190 gst_static_pad_template_get (&sink_template_factory));
192 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
193 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
194 gst_element_class_add_pad_template (gstelement_class, src_template);
196 gst_element_class_set_static_metadata (gstelement_class, "WAV audio demuxer",
197 "Codec/Demuxer/Audio",
198 "Parse a .wav file into raw audio",
199 "Erik Walthinsen <omega@cse.ogi.edu>");
203 gst_wavparse_reset (GstWavParse * wav)
205 wav->state = GST_WAVPARSE_START;
207 /* These will all be set correctly in the fmt chunk */
221 wav->got_fmt = FALSE;
225 gst_event_unref (wav->seek_event);
226 wav->seek_event = NULL;
228 gst_adapter_clear (wav->adapter);
229 g_object_unref (wav->adapter);
233 gst_tag_list_unref (wav->tags);
236 gst_toc_unref (wav->toc);
239 g_list_free_full (wav->cues, g_free);
242 g_list_free_full (wav->labls, g_free);
245 gst_caps_unref (wav->caps);
247 if (wav->start_segment)
248 gst_event_unref (wav->start_segment);
249 wav->start_segment = NULL;
253 gst_wavparse_dispose (GObject * object)
255 GstWavParse *wav = GST_WAVPARSE (object);
257 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
258 gst_wavparse_reset (wav);
260 G_OBJECT_CLASS (parent_class)->dispose (object);
264 gst_wavparse_init (GstWavParse * wavparse)
266 gst_wavparse_reset (wavparse);
270 gst_pad_new_from_static_template (&sink_template_factory, "sink");
271 gst_pad_set_activate_function (wavparse->sinkpad,
272 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
273 gst_pad_set_activatemode_function (wavparse->sinkpad,
274 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
275 gst_pad_set_chain_function (wavparse->sinkpad,
276 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
277 gst_pad_set_event_function (wavparse->sinkpad,
278 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
279 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
283 gst_pad_new_from_template (gst_element_class_get_pad_template
284 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
285 gst_pad_use_fixed_caps (wavparse->srcpad);
286 gst_pad_set_query_function (wavparse->srcpad,
287 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
288 gst_pad_set_event_function (wavparse->srcpad,
289 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
290 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
294 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
298 if (!gst_riff_parse_file_header (element, buf, &doctype))
301 if (doctype != GST_RIFF_RIFF_WAVE)
309 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
310 ("File is not a WAVE file: 0x%" G_GINT32_MODIFIER "x", doctype));
316 gst_wavparse_stream_init (GstWavParse * wav)
319 GstBuffer *buf = NULL;
321 if ((res = gst_pad_pull_range (wav->sinkpad,
322 wav->offset, 12, &buf)) != GST_FLOW_OK)
324 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
325 return GST_FLOW_ERROR;
333 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
335 /* -1 always maps to -1 */
341 /* 0 always maps to 0 */
348 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
350 } else if (wav->fact) {
351 guint64 bps = gst_util_uint64_scale (wav->datasize, wav->rate, wav->fact);
352 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
359 /* This function is used to perform seeks on the element.
361 * It also works when event is NULL, in which case it will just
362 * start from the last configured segment. This technique is
363 * used when activating the element and to perform the seek in
367 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
371 GstFormat format, bformat;
373 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
374 gint64 cur, stop, upstream_size;
377 GstSegment seeksegment = { 0, };
381 GST_DEBUG_OBJECT (wav, "doing seek with event");
383 gst_event_parse_seek (event, &rate, &format, &flags,
384 &cur_type, &cur, &stop_type, &stop);
386 /* no negative rates yet */
390 if (format != wav->segment.format) {
391 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
392 gst_format_get_name (format),
393 gst_format_get_name (wav->segment.format));
395 if (cur_type != GST_SEEK_TYPE_NONE)
397 gst_pad_query_convert (wav->srcpad, format, cur,
398 wav->segment.format, &cur);
399 if (res && stop_type != GST_SEEK_TYPE_NONE)
401 gst_pad_query_convert (wav->srcpad, format, stop,
402 wav->segment.format, &stop);
406 format = wav->segment.format;
409 GST_DEBUG_OBJECT (wav, "doing seek without event");
412 cur_type = GST_SEEK_TYPE_SET;
413 stop_type = GST_SEEK_TYPE_SET;
416 /* in push mode, we must delegate to upstream */
417 if (wav->streaming) {
418 gboolean res = FALSE;
420 /* if streaming not yet started; only prepare initial newsegment */
421 if (!event || wav->state != GST_WAVPARSE_DATA) {
422 if (wav->start_segment)
423 gst_event_unref (wav->start_segment);
424 wav->start_segment = gst_event_new_segment (&wav->segment);
427 /* convert seek positions to byte positions in data sections */
428 if (format == GST_FORMAT_TIME) {
429 /* should not fail */
430 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
432 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
435 /* mind sample boundary and header */
437 cur -= (cur % wav->bytes_per_sample);
438 cur += wav->datastart;
441 stop -= (stop % wav->bytes_per_sample);
442 stop += wav->datastart;
444 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
445 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
447 /* BYTE seek event */
448 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
450 res = gst_pad_push_event (wav->sinkpad, event);
456 flush = flags & GST_SEEK_FLAG_FLUSH;
458 /* now we need to make sure the streaming thread is stopped. We do this by
459 * either sending a FLUSH_START event downstream which will cause the
460 * streaming thread to stop with a WRONG_STATE.
461 * For a non-flushing seek we simply pause the task, which will happen as soon
462 * as it completes one iteration (and thus might block when the sink is
463 * blocking in preroll). */
465 GST_DEBUG_OBJECT (wav, "sending flush start");
466 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
468 gst_pad_pause_task (wav->sinkpad);
471 /* we should now be able to grab the streaming thread because we stopped it
472 * with the above flush/pause code */
473 GST_PAD_STREAM_LOCK (wav->sinkpad);
475 /* save current position */
476 last_stop = wav->segment.position;
478 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
480 /* copy segment, we need this because we still need the old
481 * segment when we close the current segment. */
482 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
484 /* configure the seek parameters in the seeksegment. We will then have the
485 * right values in the segment to perform the seek */
487 GST_DEBUG_OBJECT (wav, "configuring seek");
488 gst_segment_do_seek (&seeksegment, rate, format, flags,
489 cur_type, cur, stop_type, stop, &update);
492 /* figure out the last position we need to play. If it's configured (stop !=
493 * -1), use that, else we play until the total duration of the file */
494 if ((stop = seeksegment.stop) == -1)
495 stop = seeksegment.duration;
497 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
498 if ((cur_type != GST_SEEK_TYPE_NONE)) {
499 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
500 * we can just copy the last_stop. If not, we use the bps to convert TIME to
502 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
503 (gint64 *) & wav->offset))
504 wav->offset = seeksegment.position;
505 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
506 wav->offset -= (wav->offset % wav->bytes_per_sample);
507 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
508 wav->offset += wav->datastart;
509 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
511 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
515 if (stop_type != GST_SEEK_TYPE_NONE) {
516 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
517 wav->end_offset = stop;
518 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
519 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
520 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
521 wav->end_offset += wav->datastart;
522 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
524 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
528 /* make sure filesize is not exceeded due to rounding errors or so,
529 * same precaution as in _stream_headers */
530 bformat = GST_FORMAT_BYTES;
531 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
532 wav->end_offset = MIN (wav->end_offset, upstream_size);
534 /* this is the range of bytes we will use for playback */
535 wav->offset = MIN (wav->offset, wav->end_offset);
536 wav->dataleft = wav->end_offset - wav->offset;
538 GST_DEBUG_OBJECT (wav,
539 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
540 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
541 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
543 /* prepare for streaming again */
545 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
546 GST_DEBUG_OBJECT (wav, "sending flush stop");
547 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
550 /* now we did the seek and can activate the new segment values */
551 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
553 /* if we're doing a segment seek, post a SEGMENT_START message */
554 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
555 gst_element_post_message (GST_ELEMENT_CAST (wav),
556 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
557 wav->segment.format, wav->segment.position));
560 /* now create the newsegment */
561 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
562 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
564 /* store the newsegment event so it can be sent from the streaming thread. */
565 if (wav->start_segment)
566 gst_event_unref (wav->start_segment);
567 wav->start_segment = gst_event_new_segment (&wav->segment);
569 /* mark discont if we are going to stream from another position. */
570 if (last_stop != wav->segment.position) {
571 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
575 /* and start the streaming task again */
576 if (!wav->streaming) {
577 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
581 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
588 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
593 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
598 GST_DEBUG_OBJECT (wav,
599 "Could not determine byte position for desired time");
605 * gst_wavparse_peek_chunk_info:
606 * @wav Wavparse object
607 * @tag holder for tag
608 * @size holder for tag size
610 * Peek next chunk info (tag and size)
612 * Returns: %TRUE when the chunk info (header) is available
615 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
617 const guint8 *data = NULL;
619 if (gst_adapter_available (wav->adapter) < 8)
622 data = gst_adapter_map (wav->adapter, 8);
623 *tag = GST_READ_UINT32_LE (data);
624 *size = GST_READ_UINT32_LE (data + 4);
625 gst_adapter_unmap (wav->adapter);
627 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
628 GST_FOURCC_ARGS (*tag));
634 * gst_wavparse_peek_chunk:
635 * @wav Wavparse object
636 * @tag holder for tag
637 * @size holder for tag size
639 * Peek enough data for one full chunk
641 * Returns: %TRUE when the full chunk is available
644 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
646 guint32 peek_size = 0;
649 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
652 /* size 0 -> empty data buffer would surprise most callers,
653 * large size -> do not bother trying to squeeze that into adapter,
654 * so we throw poor man's exception, which can be caught if caller really
655 * wants to handle 0 size chunk */
656 if (!(*size) || (*size) >= (1 << 30)) {
657 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
658 *size, GST_FOURCC_ARGS (*tag));
659 /* chain should give up */
660 wav->abort_buffering = TRUE;
663 peek_size = (*size + 1) & ~1;
664 available = gst_adapter_available (wav->adapter);
666 if (available >= (8 + peek_size)) {
669 GST_LOG ("but only %u bytes available now", available);
675 * gst_wavparse_calculate_duration:
676 * @wav: wavparse object
678 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
681 * Returns: %TRUE if duration is available.
684 gst_wavparse_calculate_duration (GstWavParse * wav)
686 if (wav->duration > 0)
690 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
692 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
694 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
695 GST_TIME_ARGS (wav->duration));
697 } else if (wav->fact) {
699 gst_util_uint64_scale_ceil (GST_SECOND, wav->fact, wav->rate);
700 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
701 GST_TIME_ARGS (wav->duration));
708 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
713 if (wav->streaming) {
714 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
717 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
718 GST_FOURCC_ARGS (tag));
719 flush = 8 + ((size + 1) & ~1);
720 wav->offset += flush;
721 if (wav->streaming) {
722 gst_adapter_flush (wav->adapter, flush);
724 gst_buffer_unref (buf);
731 * gst_wavparse_cue_chunk:
732 * @wav GstWavParse object
733 * @data holder for data
734 * @size holder for data size
736 * Parse cue chunk from @data to wav->cues.
738 * Returns: %TRUE when cue chunk is available
741 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
748 GST_WARNING_OBJECT (wav, "found another cue's");
752 ncues = GST_READ_UINT32_LE (data);
754 if (size < 4 + ncues * 24) {
755 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
761 for (i = 0; i < ncues; i++) {
762 cue = g_new0 (GstWavParseCue, 1);
763 cue->id = GST_READ_UINT32_LE (data);
764 cue->position = GST_READ_UINT32_LE (data + 4);
765 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
766 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
767 cue->block_start = GST_READ_UINT32_LE (data + 16);
768 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
769 cues = g_list_append (cues, cue);
779 * gst_wavparse_labl_chunk:
780 * @wav GstWavParse object
781 * @data holder for data
782 * @size holder for data size
784 * Parse labl from @data to wav->labls.
786 * Returns: %TRUE when labl chunk is available
789 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
791 GstWavParseLabl *labl;
796 labl = g_new0 (GstWavParseLabl, 1);
800 labl->cue_point_id = GST_READ_UINT32_LE (data);
801 labl->text = g_memdup (data + 4, size - 4);
803 wav->labls = g_list_append (wav->labls, labl);
809 * gst_wavparse_note_chunk:
810 * @wav GstWavParse object
811 * @data holder for data
812 * @size holder for data size
814 * Parse note from @data to wav->notes.
816 * Returns: %TRUE when note chunk is available
819 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
821 GstWavParseNote *note;
826 note = g_new0 (GstWavParseNote, 1);
830 note->cue_point_id = GST_READ_UINT32_LE (data);
831 note->text = g_memdup (data + 4, size - 4);
833 wav->notes = g_list_append (wav->notes, note);
839 * gst_wavparse_smpl_chunk:
840 * @wav GstWavParse object
841 * @data holder for data
842 * @size holder for data size
844 * Parse smpl chunk from @data.
846 * Returns: %TRUE when cue chunk is available
849 gst_wavparse_smpl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
854 manufacturer_id = GST_READ_UINT32_LE (data);
855 product_id = GST_READ_UINT32_LE (data + 4);
856 sample_period = GST_READ_UINT32_LE (data + 8);
858 note_number = GST_READ_UINT32_LE (data + 12);
860 pitch_fraction = GST_READ_UINT32_LE (data + 16);
861 SMPTE_format = GST_READ_UINT32_LE (data + 20);
862 SMPTE_offset = GST_READ_UINT32_LE (data + 24);
863 num_sample_loops = GST_READ_UINT32_LE (data + 28);
864 List of Sample Loops, 24 bytes each
868 wav->tags = gst_tag_list_new_empty ();
869 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
870 GST_TAG_MIDI_BASE_NOTE, (guint) note_number, NULL);
875 * gst_wavparse_adtl_chunk:
876 * @wav GstWavParse object
877 * @data holder for data
878 * @size holder for data size
880 * Parse adtl from @data.
882 * Returns: %TRUE when adtl chunk is available
885 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
887 guint32 ltag, lsize, offset = 0;
890 ltag = GST_READ_UINT32_LE (data + offset);
891 lsize = GST_READ_UINT32_LE (data + offset + 4);
893 if (lsize + 8 > size) {
894 GST_WARNING_OBJECT (wav, "Invalid adtl size: %u + 8 > %u", lsize, size);
899 case GST_RIFF_TAG_labl:
900 gst_wavparse_labl_chunk (wav, data + offset, size);
902 case GST_RIFF_TAG_note:
903 gst_wavparse_note_chunk (wav, data + offset, size);
906 GST_WARNING_OBJECT (wav, "Unknowm adtl %" GST_FOURCC_FORMAT,
907 GST_FOURCC_ARGS (ltag));
908 GST_MEMDUMP_OBJECT (wav, "Unknowm adtl", &data[offset], lsize);
911 offset += 8 + GST_ROUND_UP_2 (lsize);
912 size -= 8 + GST_ROUND_UP_2 (lsize);
919 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
921 GstTagList *tags = NULL;
922 GstTocEntry *entry = NULL;
924 entry = gst_toc_find_entry (toc, id);
926 tags = gst_toc_entry_get_tags (entry);
928 tags = gst_tag_list_new_empty ();
929 gst_toc_entry_set_tags (entry, tags);
937 * gst_wavparse_create_toc:
938 * @wav GstWavParse object
940 * Create TOC from wav->cues and wav->labls.
943 gst_wavparse_create_toc (GstWavParse * wav)
949 GstWavParseLabl *labl;
950 GstWavParseNote *note;
953 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
955 GST_OBJECT_LOCK (wav);
957 GST_OBJECT_UNLOCK (wav);
958 GST_WARNING_OBJECT (wav, "found another TOC");
963 GST_OBJECT_UNLOCK (wav);
967 /* FIXME: send CURRENT scope toc too */
968 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
970 /* add cue edition */
971 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
972 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
973 gst_toc_append_entry (toc, entry);
975 /* add tracks in cue edition */
979 prev_subentry = cur_subentry;
980 /* previous track stop time = current track start time */
981 if (prev_subentry != NULL) {
982 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
983 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
984 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
986 id = g_strdup_printf ("%08x", cue->id);
987 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
989 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
990 stop = wav->duration;
991 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
992 gst_toc_entry_append_sub_entry (entry, cur_subentry);
993 list = g_list_next (list);
996 /* add tags in tracks */
1000 id = g_strdup_printf ("%08x", labl->cue_point_id);
1001 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1004 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1007 list = g_list_next (list);
1012 id = g_strdup_printf ("%08x", note->cue_point_id);
1013 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1016 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1019 list = g_list_next (list);
1022 /* send data as TOC */
1025 /* send TOC event */
1027 GST_OBJECT_UNLOCK (wav);
1028 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1034 #define MAX_BUFFER_SIZE 4096
1037 parse_ds64 (GstWavParse * wav, GstBuffer * buf)
1040 guint32 dataSizeLow, dataSizeHigh;
1041 guint32 sampleCountLow, sampleCountHigh;
1043 gst_buffer_map (buf, &map, GST_MAP_READ);
1044 dataSizeLow = GST_READ_UINT32_LE (map.data + 2 * 4);
1045 dataSizeHigh = GST_READ_UINT32_LE (map.data + 3 * 4);
1046 sampleCountLow = GST_READ_UINT32_LE (map.data + 4 * 4);
1047 sampleCountHigh = GST_READ_UINT32_LE (map.data + 5 * 4);
1048 gst_buffer_unmap (buf, &map);
1049 if (dataSizeHigh != 0xFFFFFFFF && dataSizeLow != 0xFFFFFFFF) {
1050 wav->datasize = ((guint64) dataSizeHigh << 32) | dataSizeLow;
1052 if (sampleCountHigh != 0xFFFFFFFF && sampleCountLow != 0xFFFFFFFF) {
1053 wav->fact = ((guint64) sampleCountHigh << 32) | sampleCountLow;
1056 GST_DEBUG_OBJECT (wav, "Got 'ds64' TAG, datasize : %" G_GINT64_FORMAT
1057 " fact: %" G_GINT64_FORMAT, wav->datasize, wav->fact);
1061 static GstFlowReturn
1062 gst_wavparse_stream_headers (GstWavParse * wav)
1064 GstFlowReturn res = GST_FLOW_OK;
1065 GstBuffer *buf = NULL;
1066 gst_riff_strf_auds *header = NULL;
1068 gboolean gotdata = FALSE;
1069 GstCaps *caps = NULL;
1070 gchar *codec_name = NULL;
1072 gint64 upstream_size = 0;
1075 /* search for "_fmt" chunk, which should be first */
1076 while (!wav->got_fmt) {
1079 /* The header starts with a 'fmt ' tag */
1080 if (wav->streaming) {
1081 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1084 gst_adapter_flush (wav->adapter, 8);
1088 buf = gst_adapter_take_buffer (wav->adapter, size);
1090 gst_adapter_flush (wav->adapter, 1);
1091 wav->offset += GST_ROUND_UP_2 (size);
1093 buf = gst_buffer_new ();
1096 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1097 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1101 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1102 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1103 tag == GST_RIFF_TAG_LIST || tag == GST_RIFF_TAG_ID32 ||
1104 tag == GST_RIFF_TAG_id3 || tag == GST_RIFF_TAG_IDVX ||
1105 tag == GST_BWF_TAG_iXML || tag == GST_BWF_TAG_qlty ||
1106 tag == GST_BWF_TAG_mext || tag == GST_BWF_TAG_levl ||
1107 tag == GST_BWF_TAG_link || tag == GST_BWF_TAG_axml) {
1108 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1109 GST_FOURCC_ARGS (tag));
1110 gst_buffer_unref (buf);
1115 if (tag == GST_RS64_TAG_DS64) {
1116 if (!parse_ds64 (wav, buf))
1122 if (tag != GST_RIFF_TAG_fmt)
1125 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1127 goto parse_header_error;
1129 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1131 /* do sanity checks of header fields */
1132 if (header->channels == 0)
1134 if (header->rate == 0)
1137 GST_DEBUG_OBJECT (wav, "creating the caps");
1139 /* Note: gst_riff_create_audio_caps might need to fix values in
1140 * the header header depending on the format, so call it first */
1141 /* FIXME: Need to handle the channel reorder map */
1142 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1143 NULL, &codec_name, NULL);
1146 gst_buffer_unref (extra);
1149 goto unknown_format;
1151 /* If we got raw audio from upstream, we remove the codec_data field,
1152 * which may have been added if the wav header included an extended
1153 * chunk. We want to keep it for non raw audio.
1155 s = gst_caps_get_structure (caps, 0);
1156 if (s && gst_structure_has_name (s, "audio/x-raw")) {
1157 gst_structure_remove_field (s, "codec_data");
1160 /* do more sanity checks of header fields
1161 * (these can be sanitized by gst_riff_create_audio_caps()
1163 wav->format = header->format;
1164 wav->rate = header->rate;
1165 wav->channels = header->channels;
1166 wav->blockalign = header->blockalign;
1167 wav->depth = header->bits_per_sample;
1168 wav->av_bps = header->av_bps;
1174 /* do format specific handling */
1175 switch (wav->format) {
1176 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1177 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1179 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1180 * bitrate inside the mpeg stream */
1181 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1185 case GST_RIFF_WAVE_FORMAT_PCM:
1186 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1187 goto invalid_blockalign;
1190 if (wav->av_bps > wav->blockalign * wav->rate)
1192 /* use the configured bps */
1193 wav->bps = wav->av_bps;
1197 wav->width = (wav->blockalign * 8) / wav->channels;
1198 wav->bytes_per_sample = wav->channels * wav->width / 8;
1200 if (wav->bytes_per_sample <= 0)
1201 goto no_bytes_per_sample;
1203 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1204 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1205 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1206 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1207 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1208 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1209 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1211 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1212 * formats). This will make the element output a BYTE format segment and
1213 * will not timestamp the outgoing buffers.
1215 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1217 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1219 /* create pad later so we can sniff the first few bytes
1220 * of the real data and correct our caps if necessary */
1221 gst_caps_replace (&wav->caps, caps);
1222 gst_caps_replace (&caps, NULL);
1224 wav->got_fmt = TRUE;
1227 wav->tags = gst_tag_list_new_empty ();
1229 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1230 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1232 g_free (codec_name);
1238 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1239 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1241 /* loop headers until we get data */
1243 if (wav->streaming) {
1244 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1251 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1252 &buf)) != GST_FLOW_OK)
1253 goto header_read_error;
1254 gst_buffer_map (buf, &map, GST_MAP_READ);
1255 tag = GST_READ_UINT32_LE (map.data);
1256 size = GST_READ_UINT32_LE (map.data + 4);
1257 gst_buffer_unmap (buf, &map);
1260 GST_INFO_OBJECT (wav,
1261 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1262 GST_FOURCC_ARGS (tag), wav->offset);
1264 /* wav is a st00pid format, we don't know for sure where data starts.
1265 * So we have to go bit by bit until we find the 'data' header
1268 case GST_RIFF_TAG_data:{
1271 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1273 if (wav->ignore_length) {
1274 GST_DEBUG_OBJECT (wav, "Ignoring length");
1277 if (wav->streaming) {
1278 gst_adapter_flush (wav->adapter, 8);
1281 gst_buffer_unref (buf);
1284 wav->datastart = wav->offset;
1285 /* use size from ds64 chunk if available */
1286 if (size64 == -1 && wav->datasize > 0) {
1287 GST_DEBUG_OBJECT (wav, "Using ds64 datasize");
1288 size64 = wav->datasize;
1290 /* If size is zero, then the data chunk probably actually extends to
1291 the end of the file */
1292 if (size64 == 0 && upstream_size) {
1293 size64 = upstream_size - wav->datastart;
1295 /* Or the file might be truncated */
1296 else if (upstream_size) {
1297 size64 = MIN (size64, (upstream_size - wav->datastart));
1299 wav->datasize = size64;
1300 wav->dataleft = size64;
1301 wav->end_offset = size64 + wav->datastart;
1302 if (!wav->streaming) {
1303 /* We will continue parsing tags 'till end */
1304 wav->offset += size64;
1306 GST_DEBUG_OBJECT (wav, "datasize = %" G_GUINT64_FORMAT, size64);
1309 case GST_RIFF_TAG_fact:{
1310 if (wav->fact == 0 &&
1311 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1312 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1313 const guint data_size = 4;
1315 GST_INFO_OBJECT (wav, "Have fact chunk");
1316 if (size < data_size) {
1317 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1318 /* need more data */
1321 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1325 /* number of samples (for compressed formats) */
1326 if (wav->streaming) {
1327 const guint8 *data = NULL;
1329 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1332 gst_adapter_flush (wav->adapter, 8);
1333 data = gst_adapter_map (wav->adapter, data_size);
1334 wav->fact = GST_READ_UINT32_LE (data);
1335 gst_adapter_unmap (wav->adapter);
1336 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1338 gst_buffer_unref (buf);
1341 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1342 data_size, &buf)) != GST_FLOW_OK)
1343 goto header_read_error;
1344 gst_buffer_extract (buf, 0, &wav->fact, 4);
1345 wav->fact = GUINT32_FROM_LE (wav->fact);
1346 gst_buffer_unref (buf);
1348 GST_DEBUG_OBJECT (wav, "have fact %" G_GUINT64_FORMAT, wav->fact);
1349 wav->offset += 8 + GST_ROUND_UP_2 (size);
1352 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1353 /* need more data */
1359 case GST_RIFF_TAG_acid:{
1360 const gst_riff_acid *acid = NULL;
1361 const guint data_size = sizeof (gst_riff_acid);
1364 GST_INFO_OBJECT (wav, "Have acid chunk");
1365 if (size < data_size) {
1366 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1367 /* need more data */
1370 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1374 if (wav->streaming) {
1375 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1378 gst_adapter_flush (wav->adapter, 8);
1379 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1381 tempo = acid->tempo;
1382 gst_adapter_unmap (wav->adapter);
1385 gst_buffer_unref (buf);
1388 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1389 size, &buf)) != GST_FLOW_OK)
1390 goto header_read_error;
1391 gst_buffer_map (buf, &map, GST_MAP_READ);
1392 acid = (const gst_riff_acid *) map.data;
1393 tempo = acid->tempo;
1394 gst_buffer_unmap (buf, &map);
1396 /* send data as tags */
1398 wav->tags = gst_tag_list_new_empty ();
1399 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1400 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1402 size = GST_ROUND_UP_2 (size);
1403 if (wav->streaming) {
1404 gst_adapter_flush (wav->adapter, size);
1406 gst_buffer_unref (buf);
1408 wav->offset += 8 + size;
1411 /* FIXME: all list tags after data are ignored in streaming mode */
1412 case GST_RIFF_TAG_LIST:{
1415 if (wav->streaming) {
1416 const guint8 *data = NULL;
1418 if (gst_adapter_available (wav->adapter) < 12) {
1421 data = gst_adapter_map (wav->adapter, 12);
1422 ltag = GST_READ_UINT32_LE (data + 8);
1423 gst_adapter_unmap (wav->adapter);
1425 gst_buffer_unref (buf);
1428 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1429 &buf)) != GST_FLOW_OK)
1430 goto header_read_error;
1431 gst_buffer_extract (buf, 8, <ag, 4);
1432 ltag = GUINT32_FROM_LE (ltag);
1435 case GST_RIFF_LIST_INFO:{
1436 const gint data_size = size - 4;
1439 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1440 if (wav->streaming) {
1441 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1444 gst_adapter_flush (wav->adapter, 12);
1446 if (data_size > 0) {
1447 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1449 gst_adapter_flush (wav->adapter, 1);
1453 gst_buffer_unref (buf);
1455 if (data_size > 0) {
1457 gst_pad_pull_range (wav->sinkpad, wav->offset,
1458 data_size, &buf)) != GST_FLOW_OK)
1459 goto header_read_error;
1462 if (data_size > 0) {
1464 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1466 GstTagList *old = wav->tags;
1468 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1470 gst_tag_list_unref (old);
1471 gst_tag_list_unref (new);
1473 gst_buffer_unref (buf);
1474 wav->offset += GST_ROUND_UP_2 (data_size);
1478 case GST_RIFF_LIST_adtl:{
1479 const gint data_size = size - 4;
1481 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1482 if (wav->streaming) {
1483 const guint8 *data = NULL;
1485 gst_adapter_flush (wav->adapter, 12);
1487 data = gst_adapter_map (wav->adapter, data_size);
1488 gst_wavparse_adtl_chunk (wav, data, data_size);
1489 gst_adapter_unmap (wav->adapter);
1493 gst_buffer_unref (buf);
1497 gst_pad_pull_range (wav->sinkpad, wav->offset,
1498 data_size, &buf)) != GST_FLOW_OK)
1499 goto header_read_error;
1500 gst_buffer_map (buf, &map, GST_MAP_READ);
1501 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1503 gst_buffer_unmap (buf, &map);
1505 wav->offset += GST_ROUND_UP_2 (data_size);
1509 GST_WARNING_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1510 GST_FOURCC_ARGS (ltag));
1511 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1512 /* need more data */
1518 case GST_RIFF_TAG_cue:{
1519 const guint data_size = size;
1521 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1522 if (wav->streaming) {
1523 const guint8 *data = NULL;
1525 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1528 gst_adapter_flush (wav->adapter, 8);
1530 data = gst_adapter_map (wav->adapter, data_size);
1531 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1532 goto header_read_error;
1534 gst_adapter_unmap (wav->adapter);
1539 gst_buffer_unref (buf);
1542 gst_pad_pull_range (wav->sinkpad, wav->offset,
1543 data_size, &buf)) != GST_FLOW_OK)
1544 goto header_read_error;
1545 gst_buffer_map (buf, &map, GST_MAP_READ);
1546 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1548 goto header_read_error;
1550 gst_buffer_unmap (buf, &map);
1552 size = GST_ROUND_UP_2 (size);
1553 if (wav->streaming) {
1554 gst_adapter_flush (wav->adapter, size);
1556 gst_buffer_unref (buf);
1558 size = GST_ROUND_UP_2 (size);
1559 wav->offset += size;
1562 case GST_RIFF_TAG_smpl:{
1563 const gint data_size = size;
1565 GST_DEBUG_OBJECT (wav, "Have 'smpl' TAG, size : %u", data_size);
1566 if (wav->streaming) {
1567 const guint8 *data = NULL;
1569 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1572 gst_adapter_flush (wav->adapter, 8);
1574 data = gst_adapter_map (wav->adapter, data_size);
1575 if (!gst_wavparse_smpl_chunk (wav, data, data_size)) {
1576 goto header_read_error;
1578 gst_adapter_unmap (wav->adapter);
1583 gst_buffer_unref (buf);
1586 gst_pad_pull_range (wav->sinkpad, wav->offset,
1587 data_size, &buf)) != GST_FLOW_OK)
1588 goto header_read_error;
1589 gst_buffer_map (buf, &map, GST_MAP_READ);
1590 if (!gst_wavparse_smpl_chunk (wav, (const guint8 *) map.data,
1592 goto header_read_error;
1594 gst_buffer_unmap (buf, &map);
1596 size = GST_ROUND_UP_2 (size);
1597 if (wav->streaming) {
1598 gst_adapter_flush (wav->adapter, size);
1600 gst_buffer_unref (buf);
1602 size = GST_ROUND_UP_2 (size);
1603 wav->offset += size;
1607 GST_WARNING_OBJECT (wav, "Ignoring chunk %" GST_FOURCC_FORMAT,
1608 GST_FOURCC_ARGS (tag));
1609 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1610 /* need more data */
1615 if (upstream_size && (wav->offset >= upstream_size)) {
1616 /* Now we are gone through the whole file */
1621 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1623 if (wav->bps <= 0 && wav->fact) {
1625 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1627 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1628 (guint64) wav->fact);
1629 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1634 if (gst_wavparse_calculate_duration (wav)) {
1635 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1636 if (!wav->ignore_length)
1637 wav->segment.duration = wav->duration;
1639 gst_wavparse_create_toc (wav);
1641 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1642 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1643 if (!wav->ignore_length)
1644 wav->segment.duration = wav->datasize;
1647 /* now we have all the info to perform a pending seek if any, if no
1648 * event, this will still do the right thing and it will also send
1649 * the right newsegment event downstream. */
1650 gst_wavparse_perform_seek (wav, wav->seek_event);
1651 /* remove pending event */
1652 event_p = &wav->seek_event;
1653 gst_event_replace (event_p, NULL);
1655 /* we just started, we are discont */
1656 wav->discont = TRUE;
1658 wav->state = GST_WAVPARSE_DATA;
1660 /* determine reasonable max buffer size,
1661 * that is, buffers not too small either size or time wise
1662 * so we do not end up with too many of them */
1664 if (gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size))
1665 wav->max_buf_size = upstream_size;
1667 wav->max_buf_size = 0;
1668 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1669 if (wav->blockalign > 0)
1670 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1672 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1680 g_free (codec_name);
1684 gst_caps_unref (caps);
1689 res = GST_FLOW_ERROR;
1694 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1695 ("Invalid WAV header (no fmt at start): %"
1696 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1701 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1702 ("Couldn't parse audio header"));
1707 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1708 ("Stream claims to contain no channels - invalid data"));
1713 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1714 ("Stream with sample_rate == 0 - invalid data"));
1719 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1720 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1721 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1726 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1727 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1728 wav->av_bps, wav->blockalign * wav->rate));
1731 no_bytes_per_sample:
1733 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1734 ("Could not caluclate bytes per sample - invalid data"));
1739 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1740 ("No caps found for format 0x%x, %u channels, %u Hz",
1741 wav->format, wav->channels, wav->rate));
1746 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1747 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1753 * Read WAV file tag when streaming
1755 static GstFlowReturn
1756 gst_wavparse_parse_stream_init (GstWavParse * wav)
1758 if (gst_adapter_available (wav->adapter) >= 12) {
1761 /* _take flushes the data */
1762 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1764 GST_DEBUG ("Parsing wav header");
1765 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1766 return GST_FLOW_ERROR;
1769 /* Go to next state */
1770 wav->state = GST_WAVPARSE_HEADER;
1775 /* handle an event sent directly to the element.
1777 * This event can be sent either in the READY state or the
1778 * >READY state. The only event of interest really is the seek
1781 * In the READY state we can only store the event and try to
1782 * respect it when going to PAUSED. We assume we are in the
1783 * READY state when our parsing state != GST_WAVPARSE_DATA.
1785 * When we are steaming, we can simply perform the seek right
1789 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1791 GstWavParse *wav = GST_WAVPARSE (element);
1792 gboolean res = FALSE;
1795 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1797 switch (GST_EVENT_TYPE (event)) {
1798 case GST_EVENT_SEEK:
1799 if (wav->state == GST_WAVPARSE_DATA) {
1800 /* we can handle the seek directly when streaming data */
1801 res = gst_wavparse_perform_seek (wav, event);
1803 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1805 event_p = &wav->seek_event;
1806 gst_event_replace (event_p, event);
1808 /* we always return true */
1815 gst_event_unref (event);
1820 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1824 s = gst_caps_get_structure (caps, 0);
1825 if (!gst_structure_has_name (s, "audio/x-dts"))
1827 if (prob >= GST_TYPE_FIND_LIKELY)
1829 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1830 if (prob < GST_TYPE_FIND_POSSIBLE)
1832 /* .. in which case we want at least a valid-looking rate and channels */
1833 if (!gst_structure_has_field (s, "channels"))
1835 /* and for extra assurance we could also check the rate from the DTS frame
1836 * against the one in the wav header, but for now let's not do that */
1837 return gst_structure_has_field (s, "rate");
1841 gst_wavparse_get_upstream_tags (GstWavParse * wav, GstTagScope scope)
1843 GstTagList *tags = NULL;
1848 while ((ev = gst_pad_get_sticky_event (wav->sinkpad, GST_EVENT_TAG, i++))) {
1849 gst_event_parse_tag (ev, &tags);
1850 if (tags != NULL && gst_tag_list_get_scope (tags) == scope) {
1851 tags = gst_tag_list_copy (tags);
1852 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1853 gst_event_unref (ev);
1857 gst_event_unref (ev);
1863 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1866 GstTagList *tags, *utags;
1868 GST_DEBUG_OBJECT (wav, "adding src pad");
1870 g_assert (wav->caps != NULL);
1872 s = gst_caps_get_structure (wav->caps, 0);
1873 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1874 GstTypeFindProbability prob;
1877 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1878 if (tf_caps != NULL) {
1879 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1880 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1881 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1882 gst_caps_unref (wav->caps);
1883 wav->caps = tf_caps;
1885 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1886 GST_TAG_AUDIO_CODEC, "dts", NULL);
1888 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1889 "marked as raw PCM audio, but ignoring for now", tf_caps);
1890 gst_caps_unref (tf_caps);
1895 gst_pad_set_caps (wav->srcpad, wav->caps);
1896 gst_caps_replace (&wav->caps, NULL);
1898 if (wav->start_segment) {
1899 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1900 gst_pad_push_event (wav->srcpad, wav->start_segment);
1901 wav->start_segment = NULL;
1904 /* upstream tags, e.g. from id3/ape tag before the wav file; assume for now
1905 * that there'll be only one scope/type of tag list from upstream, if any */
1906 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_GLOBAL);
1908 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_STREAM);
1910 /* if there's a tag upstream it's probably been added to override the
1911 * tags from inside the wav header, so keep upstream tags if in doubt */
1912 tags = gst_tag_list_merge (utags, wav->tags, GST_TAG_MERGE_KEEP);
1914 if (wav->tags != NULL) {
1915 gst_tag_list_unref (wav->tags);
1920 gst_tag_list_unref (utags);
1922 /* send tags downstream, if any */
1924 gst_pad_push_event (wav->srcpad, gst_event_new_tag (tags));
1927 static GstFlowReturn
1928 gst_wavparse_stream_data (GstWavParse * wav)
1930 GstBuffer *buf = NULL;
1931 GstFlowReturn res = GST_FLOW_OK;
1932 guint64 desired, obtained;
1933 GstClockTime timestamp, next_timestamp, duration;
1934 guint64 pos, nextpos;
1937 GST_LOG_OBJECT (wav,
1938 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1939 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1941 /* Get the next n bytes and output them */
1942 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1945 /* scale the amount of data by the segment rate so we get equal
1946 * amounts of data regardless of the playback rate */
1948 MIN (gst_guint64_to_gdouble (wav->dataleft),
1949 wav->max_buf_size * ABS (wav->segment.rate));
1951 if (desired >= wav->blockalign && wav->blockalign > 0)
1952 desired -= (desired % wav->blockalign);
1954 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1955 "from the sinkpad", desired);
1957 if (wav->streaming) {
1958 guint avail = gst_adapter_available (wav->adapter);
1961 /* flush some bytes if evil upstream sends segment that starts
1962 * before data or does is not send sample aligned segment */
1963 if (G_LIKELY (wav->offset >= wav->datastart)) {
1964 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1966 extra = wav->datastart - wav->offset;
1969 if (G_UNLIKELY (extra)) {
1970 extra = wav->bytes_per_sample - extra;
1971 if (extra <= avail) {
1972 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
1973 gst_adapter_flush (wav->adapter, extra);
1974 wav->offset += extra;
1975 wav->dataleft -= extra;
1976 goto iterate_adapter;
1978 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
1979 gst_adapter_clear (wav->adapter);
1980 wav->offset += avail;
1981 wav->dataleft -= avail;
1986 if (avail < desired) {
1987 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
1991 buf = gst_adapter_take_buffer (wav->adapter, desired);
1993 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1994 desired, &buf)) != GST_FLOW_OK)
1997 /* we may get a short buffer at the end of the file */
1998 if (gst_buffer_get_size (buf) < desired) {
1999 gsize size = gst_buffer_get_size (buf);
2001 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2002 if (size >= wav->blockalign) {
2003 if (wav->blockalign > 0) {
2004 buf = gst_buffer_make_writable (buf);
2005 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2008 gst_buffer_unref (buf);
2014 obtained = gst_buffer_get_size (buf);
2016 /* our positions in bytes */
2017 pos = wav->offset - wav->datastart;
2018 nextpos = pos + obtained;
2020 /* update offsets, does not overflow. */
2021 buf = gst_buffer_make_writable (buf);
2022 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2023 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2025 /* first chunk of data? create the source pad. We do this only here so
2026 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2027 if (G_UNLIKELY (wav->first)) {
2029 /* this will also push the segment events */
2030 gst_wavparse_add_src_pad (wav, buf);
2032 /* If we have a pending start segment, send it now. */
2033 if (G_UNLIKELY (wav->start_segment != NULL)) {
2034 gst_pad_push_event (wav->srcpad, wav->start_segment);
2035 wav->start_segment = NULL;
2040 /* and timestamps if we have a bitrate, be careful for overflows */
2042 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2044 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2045 duration = next_timestamp - timestamp;
2047 /* update current running segment position */
2048 if (G_LIKELY (next_timestamp >= wav->segment.start))
2049 wav->segment.position = next_timestamp;
2050 } else if (wav->fact) {
2052 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2053 /* and timestamps if we have a bitrate, be careful for overflows */
2054 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2055 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2056 duration = next_timestamp - timestamp;
2058 /* no bitrate, all we know is that the first sample has timestamp 0, all
2059 * other positions and durations have unknown timestamp. */
2063 timestamp = GST_CLOCK_TIME_NONE;
2064 duration = GST_CLOCK_TIME_NONE;
2065 /* update current running segment position with byte offset */
2066 if (G_LIKELY (nextpos >= wav->segment.start))
2067 wav->segment.position = nextpos;
2069 if ((pos > 0) && wav->vbr) {
2070 /* don't set timestamps for VBR files if it's not the first buffer */
2071 timestamp = GST_CLOCK_TIME_NONE;
2072 duration = GST_CLOCK_TIME_NONE;
2075 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2076 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2077 wav->discont = FALSE;
2080 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2081 GST_BUFFER_DURATION (buf) = duration;
2083 GST_LOG_OBJECT (wav,
2084 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2085 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2086 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2088 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2091 if (obtained < wav->dataleft) {
2092 wav->offset += obtained;
2093 wav->dataleft -= obtained;
2095 wav->offset += wav->dataleft;
2099 /* Iterate until need more data, so adapter size won't grow */
2100 if (wav->streaming) {
2101 GST_LOG_OBJECT (wav,
2102 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2104 goto iterate_adapter;
2111 GST_DEBUG_OBJECT (wav, "found EOS");
2112 return GST_FLOW_EOS;
2116 /* check if we got EOS */
2117 if (res == GST_FLOW_EOS)
2120 GST_WARNING_OBJECT (wav,
2121 "Error getting %" G_GINT64_FORMAT " bytes from the "
2122 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2127 GST_INFO_OBJECT (wav,
2128 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2129 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2130 gst_pad_is_linked (wav->srcpad));
2136 gst_wavparse_loop (GstPad * pad)
2139 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2143 GST_LOG_OBJECT (wav, "process data");
2145 switch (wav->state) {
2146 case GST_WAVPARSE_START:
2147 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2148 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2152 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2153 event = gst_event_new_stream_start (stream_id);
2154 gst_event_set_group_id (event, gst_util_group_id_next ());
2155 gst_pad_push_event (wav->srcpad, event);
2158 wav->state = GST_WAVPARSE_HEADER;
2161 case GST_WAVPARSE_HEADER:
2162 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2163 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2166 wav->state = GST_WAVPARSE_DATA;
2167 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2170 case GST_WAVPARSE_DATA:
2171 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2175 g_assert_not_reached ();
2182 const gchar *reason = gst_flow_get_name (ret);
2184 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2185 gst_pad_pause_task (pad);
2187 if (ret == GST_FLOW_EOS) {
2188 /* handle end-of-stream/segment */
2189 /* so align our position with the end of it, if there is one
2190 * this ensures a subsequent will arrive at correct base/acc time */
2191 if (wav->segment.format == GST_FORMAT_TIME) {
2192 if (wav->segment.rate > 0.0 &&
2193 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2194 wav->segment.position = wav->segment.stop;
2195 else if (wav->segment.rate < 0.0)
2196 wav->segment.position = wav->segment.start;
2198 if (wav->state == GST_WAVPARSE_START) {
2199 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2200 ("No valid input found before end of stream"));
2201 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2203 /* add pad before we perform EOS */
2204 if (G_UNLIKELY (wav->first)) {
2206 gst_wavparse_add_src_pad (wav, NULL);
2209 /* perform EOS logic */
2210 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2213 if ((stop = wav->segment.stop) == -1)
2214 stop = wav->segment.duration;
2216 gst_element_post_message (GST_ELEMENT_CAST (wav),
2217 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2218 wav->segment.format, stop));
2219 gst_pad_push_event (wav->srcpad,
2220 gst_event_new_segment_done (wav->segment.format, stop));
2222 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2225 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2226 /* for fatal errors we post an error message, post the error
2227 * first so the app knows about the error first. */
2228 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2229 (_("Internal data flow error.")),
2230 ("streaming task paused, reason %s (%d)", reason, ret));
2231 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2237 static GstFlowReturn
2238 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2241 GstWavParse *wav = GST_WAVPARSE (parent);
2243 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2244 gst_buffer_get_size (buf));
2246 gst_adapter_push (wav->adapter, buf);
2248 switch (wav->state) {
2249 case GST_WAVPARSE_START:
2250 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2251 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2254 if (wav->state != GST_WAVPARSE_HEADER)
2257 /* otherwise fall-through */
2258 case GST_WAVPARSE_HEADER:
2259 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2260 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2263 if (!wav->got_fmt || wav->datastart == 0)
2266 wav->state = GST_WAVPARSE_DATA;
2267 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2270 case GST_WAVPARSE_DATA:
2271 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2272 wav->discont = TRUE;
2273 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2277 g_return_val_if_reached (GST_FLOW_ERROR);
2280 if (G_UNLIKELY (wav->abort_buffering)) {
2281 wav->abort_buffering = FALSE;
2282 ret = GST_FLOW_ERROR;
2283 /* sort of demux/parse error */
2284 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2290 static GstFlowReturn
2291 gst_wavparse_flush_data (GstWavParse * wav)
2293 GstFlowReturn ret = GST_FLOW_OK;
2296 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2298 wav->end_offset = wav->offset + av;
2299 ret = gst_wavparse_stream_data (wav);
2306 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2308 GstWavParse *wav = GST_WAVPARSE (parent);
2309 gboolean ret = TRUE;
2311 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2313 switch (GST_EVENT_TYPE (event)) {
2314 case GST_EVENT_CAPS:
2316 /* discard, we'll come up with proper src caps */
2317 gst_event_unref (event);
2320 case GST_EVENT_SEGMENT:
2322 gint64 start, stop, offset = 0, end_offset = -1;
2325 /* some debug output */
2326 gst_event_copy_segment (event, &segment);
2327 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2330 if (wav->state != GST_WAVPARSE_DATA) {
2331 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2335 /* now we are either committed to TIME or BYTE format,
2336 * and we only expect a BYTE segment, e.g. following a seek */
2337 if (segment.format == GST_FORMAT_BYTES) {
2338 /* handle (un)signed issues */
2339 start = segment.start;
2340 stop = segment.stop;
2343 start -= wav->datastart;
2344 start = MAX (start, 0);
2348 segment.stop -= wav->datastart;
2349 segment.stop = MAX (stop, 0);
2351 if (wav->segment.format == GST_FORMAT_TIME) {
2352 guint64 bps = wav->bps;
2354 /* operating in format TIME, so we can convert */
2355 if (!bps && wav->fact)
2357 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2361 gst_util_uint64_scale_ceil (start, GST_SECOND,
2362 (guint64) wav->bps);
2365 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2366 (guint64) wav->bps);
2370 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2374 segment.start = start;
2375 segment.stop = stop;
2377 /* accept upstream's notion of segment and distribute along */
2378 segment.format = wav->segment.format;
2379 segment.time = segment.position = segment.start;
2380 segment.duration = wav->segment.duration;
2381 segment.base = gst_segment_to_running_time (&wav->segment,
2382 GST_FORMAT_TIME, wav->segment.position);
2384 gst_segment_copy_into (&segment, &wav->segment);
2386 /* also store the newsegment event for the streaming thread */
2387 if (wav->start_segment)
2388 gst_event_unref (wav->start_segment);
2389 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2390 wav->start_segment = gst_event_new_segment (&segment);
2392 /* stream leftover data in current segment */
2393 gst_wavparse_flush_data (wav);
2394 /* and set up streaming thread for next one */
2395 wav->offset = offset;
2396 wav->end_offset = end_offset;
2397 if (wav->end_offset > 0) {
2398 wav->dataleft = wav->end_offset - wav->offset;
2400 /* infinity; upstream will EOS when done */
2401 wav->dataleft = G_MAXUINT64;
2404 gst_event_unref (event);
2408 if (wav->state == GST_WAVPARSE_START) {
2409 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2410 ("No valid input found before end of stream"));
2412 /* add pad if needed so EOS is seen downstream */
2413 if (G_UNLIKELY (wav->first)) {
2415 gst_wavparse_add_src_pad (wav, NULL);
2417 /* stream leftover data in current segment */
2418 gst_wavparse_flush_data (wav);
2423 case GST_EVENT_FLUSH_STOP:
2427 gst_adapter_clear (wav->adapter);
2428 wav->discont = TRUE;
2429 dur = wav->segment.duration;
2430 gst_segment_init (&wav->segment, wav->segment.format);
2431 wav->segment.duration = dur;
2435 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2443 /* convert and query stuff */
2444 static const GstFormat *
2445 gst_wavparse_get_formats (GstPad * pad)
2447 static GstFormat formats[] = {
2450 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2459 gst_wavparse_pad_convert (GstPad * pad,
2460 GstFormat src_format, gint64 src_value,
2461 GstFormat * dest_format, gint64 * dest_value)
2463 GstWavParse *wavparse;
2464 gboolean res = TRUE;
2466 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2468 if (*dest_format == src_format) {
2469 *dest_value = src_value;
2473 if ((wavparse->bps == 0) && !wavparse->fact)
2476 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2477 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2479 switch (src_format) {
2480 case GST_FORMAT_BYTES:
2481 switch (*dest_format) {
2482 case GST_FORMAT_DEFAULT:
2483 *dest_value = src_value / wavparse->bytes_per_sample;
2484 /* make sure we end up on a sample boundary */
2485 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2487 case GST_FORMAT_TIME:
2488 /* src_value + datastart = offset */
2489 GST_INFO_OBJECT (wavparse,
2490 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2492 if (wavparse->bps > 0)
2493 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2494 (guint64) wavparse->bps);
2495 else if (wavparse->fact) {
2496 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2497 wavparse->rate, wavparse->fact);
2500 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2511 case GST_FORMAT_DEFAULT:
2512 switch (*dest_format) {
2513 case GST_FORMAT_BYTES:
2514 *dest_value = src_value * wavparse->bytes_per_sample;
2516 case GST_FORMAT_TIME:
2517 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2518 (guint64) wavparse->rate);
2526 case GST_FORMAT_TIME:
2527 switch (*dest_format) {
2528 case GST_FORMAT_BYTES:
2529 if (wavparse->bps > 0)
2530 *dest_value = gst_util_uint64_scale (src_value,
2531 (guint64) wavparse->bps, GST_SECOND);
2533 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2534 wavparse->rate, wavparse->fact);
2536 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2538 /* make sure we end up on a sample boundary */
2539 *dest_value -= *dest_value % wavparse->blockalign;
2541 case GST_FORMAT_DEFAULT:
2542 *dest_value = gst_util_uint64_scale (src_value,
2543 (guint64) wavparse->rate, GST_SECOND);
2562 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2568 /* handle queries for location and length in requested format */
2570 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2572 gboolean res = TRUE;
2573 GstWavParse *wav = GST_WAVPARSE (parent);
2575 /* only if we know */
2576 if (wav->state != GST_WAVPARSE_DATA) {
2580 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2582 switch (GST_QUERY_TYPE (query)) {
2583 case GST_QUERY_POSITION:
2589 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2590 curb = wav->offset - wav->datastart;
2591 gst_query_parse_position (query, &format, NULL);
2592 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2595 case GST_FORMAT_BYTES:
2596 format = GST_FORMAT_BYTES;
2600 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2605 gst_query_set_position (query, format, cur);
2608 case GST_QUERY_DURATION:
2610 gint64 duration = 0;
2613 if (wav->ignore_length) {
2618 gst_query_parse_duration (query, &format, NULL);
2621 case GST_FORMAT_BYTES:{
2622 format = GST_FORMAT_BYTES;
2623 duration = wav->datasize;
2626 case GST_FORMAT_TIME:
2627 if ((res = gst_wavparse_calculate_duration (wav))) {
2628 duration = wav->duration;
2636 gst_query_set_duration (query, format, duration);
2639 case GST_QUERY_CONVERT:
2641 gint64 srcvalue, dstvalue;
2642 GstFormat srcformat, dstformat;
2644 gst_query_parse_convert (query, &srcformat, &srcvalue,
2645 &dstformat, &dstvalue);
2646 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2647 &dstformat, &dstvalue);
2649 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2652 case GST_QUERY_SEEKING:{
2654 gboolean seekable = FALSE;
2656 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2657 if (fmt == wav->segment.format) {
2658 if (wav->streaming) {
2661 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2662 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2663 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2664 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2666 gst_query_unref (q);
2668 GST_LOG_OBJECT (wav, "looping => seekable");
2672 } else if (fmt == GST_FORMAT_TIME) {
2676 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2681 res = gst_pad_query_default (pad, parent, query);
2688 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2690 GstWavParse *wavparse = GST_WAVPARSE (parent);
2691 gboolean res = FALSE;
2693 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2695 switch (GST_EVENT_TYPE (event)) {
2696 case GST_EVENT_SEEK:
2697 /* can only handle events when we are in the data state */
2698 if (wavparse->state == GST_WAVPARSE_DATA) {
2699 res = gst_wavparse_perform_seek (wavparse, event);
2701 gst_event_unref (event);
2704 case GST_EVENT_TOC_SELECT:
2707 GstTocEntry *entry = NULL;
2708 GstEvent *seek_event;
2711 if (!wavparse->toc) {
2712 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2715 gst_event_parse_toc_select (event, &uid);
2717 GST_OBJECT_LOCK (wavparse);
2718 entry = gst_toc_find_entry (wavparse->toc, uid);
2719 if (entry == NULL) {
2720 GST_OBJECT_UNLOCK (wavparse);
2721 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2725 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2726 GST_OBJECT_UNLOCK (wavparse);
2727 seek_event = gst_event_new_seek (1.0,
2729 GST_SEEK_FLAG_FLUSH,
2730 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2731 res = gst_wavparse_perform_seek (wavparse, seek_event);
2732 gst_event_unref (seek_event);
2736 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2740 gst_event_unref (event);
2745 res = gst_pad_push_event (wavparse->sinkpad, event);
2752 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2754 GstWavParse *wav = GST_WAVPARSE (parent);
2759 gst_adapter_clear (wav->adapter);
2760 g_object_unref (wav->adapter);
2761 wav->adapter = NULL;
2764 query = gst_query_new_scheduling ();
2766 if (!gst_pad_peer_query (sinkpad, query)) {
2767 gst_query_unref (query);
2771 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2772 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2773 gst_query_unref (query);
2778 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2779 wav->streaming = FALSE;
2780 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2784 GST_DEBUG_OBJECT (sinkpad, "activating push");
2785 wav->streaming = TRUE;
2786 wav->adapter = gst_adapter_new ();
2787 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2793 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2794 GstPadMode mode, gboolean active)
2799 case GST_PAD_MODE_PUSH:
2802 case GST_PAD_MODE_PULL:
2804 /* if we have a scheduler we can start the task */
2805 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2808 res = gst_pad_stop_task (sinkpad);
2818 static GstStateChangeReturn
2819 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2821 GstStateChangeReturn ret;
2822 GstWavParse *wav = GST_WAVPARSE (element);
2824 switch (transition) {
2825 case GST_STATE_CHANGE_NULL_TO_READY:
2827 case GST_STATE_CHANGE_READY_TO_PAUSED:
2828 gst_wavparse_reset (wav);
2830 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2836 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2838 switch (transition) {
2839 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2841 case GST_STATE_CHANGE_PAUSED_TO_READY:
2842 gst_wavparse_reset (wav);
2844 case GST_STATE_CHANGE_READY_TO_NULL:
2853 gst_wavparse_set_property (GObject * object, guint prop_id,
2854 const GValue * value, GParamSpec * pspec)
2858 g_return_if_fail (GST_IS_WAVPARSE (object));
2859 self = GST_WAVPARSE (object);
2862 case PROP_IGNORE_LENGTH:
2863 self->ignore_length = g_value_get_boolean (value);
2866 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2872 gst_wavparse_get_property (GObject * object, guint prop_id,
2873 GValue * value, GParamSpec * pspec)
2877 g_return_if_fail (GST_IS_WAVPARSE (object));
2878 self = GST_WAVPARSE (object);
2881 case PROP_IGNORE_LENGTH:
2882 g_value_set_boolean (value, self->ignore_length);
2885 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2890 plugin_init (GstPlugin * plugin)
2894 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2898 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2901 "Parse a .wav file into raw audio",
2902 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)