1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) <2006> Nokia Corporation, Stefan Kost <stefan.kost@nokia.com>.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-wavparse
25 * Parse a .wav file into raw or compressed audio.
27 * Wavparse supports both push and pull mode operations, making it possible to
28 * stream from a network source.
31 * <title>Example launch line</title>
33 * gst-launch-1.0 filesrc location=sine.wav ! wavparse ! audioconvert ! alsasink
34 * ]| Read a wav file and output to the soundcard using the ALSA element. The
35 * wav file is assumed to contain raw uncompressed samples.
37 * gst-launch-1.0 gnomevfssrc location=http://www.example.org/sine.wav ! queue ! wavparse ! audioconvert ! alsasink
38 * ]| Stream data from a network url.
44 * http://replaygain.hydrogenaudio.org/file_format_wav.html
54 #include "gstwavparse.h"
55 #include "gst/riff/riff-media.h"
56 #include <gst/base/gsttypefindhelper.h>
57 #include <gst/pbutils/descriptions.h>
58 #include <gst/gst-i18n-plugin.h>
60 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
61 #define GST_CAT_DEFAULT (wavparse_debug)
63 /* Data size chunk of RF64,
64 * see http://tech.ebu.ch/docs/tech/tech3306-2009.pdf */
65 #define GST_RS64_TAG_DS64 GST_MAKE_FOURCC ('d','s','6','4')
67 static void gst_wavparse_dispose (GObject * object);
69 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad,
71 static gboolean gst_wavparse_sink_activate_mode (GstPad * sinkpad,
72 GstObject * parent, GstPadMode mode, gboolean active);
73 static gboolean gst_wavparse_send_event (GstElement * element,
75 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
76 GstStateChange transition);
78 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
80 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
81 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
83 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
85 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
87 static void gst_wavparse_loop (GstPad * pad);
88 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
91 static void gst_wavparse_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_wavparse_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 #define DEFAULT_IGNORE_LENGTH FALSE
104 static GstStaticPadTemplate sink_template_factory =
105 GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_STATIC_CAPS ("audio/x-wav")
112 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
114 #define gst_wavparse_parent_class parent_class
115 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
120 /* Offset Size Description Value
121 * 0x00 4 ID unique identification value
122 * 0x04 4 Position play order position
123 * 0x08 4 Data Chunk ID RIFF ID of corresponding data chunk
124 * 0x0c 4 Chunk Start Byte Offset of Data Chunk *
125 * 0x10 4 Block Start Byte Offset to sample of First Channel
126 * 0x14 4 Sample Offset Byte Offset to sample byte of First Channel
130 guint32 data_chunk_id;
133 guint32 sample_offset;
138 /* Offset Size Description Value
139 * 0x08 4 Cue Point ID 0 - 0xFFFFFFFF
142 guint32 cue_point_id;
144 } GstWavParseLabl, GstWavParseNote;
147 gst_wavparse_class_init (GstWavParseClass * klass)
149 GstElementClass *gstelement_class;
150 GObjectClass *object_class;
151 GstPadTemplate *src_template;
153 gstelement_class = (GstElementClass *) klass;
154 object_class = (GObjectClass *) klass;
156 parent_class = g_type_class_peek_parent (klass);
158 object_class->dispose = gst_wavparse_dispose;
160 object_class->set_property = gst_wavparse_set_property;
161 object_class->get_property = gst_wavparse_get_property;
164 * GstWavParse:ignore-length:
166 * This selects whether the length found in a data chunk
167 * should be ignored. This may be useful for streamed audio
168 * where the length is unknown until the end of streaming,
169 * and various software/hardware just puts some random value
170 * in there and hopes it doesn't break too much.
172 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
173 g_param_spec_boolean ("ignore-length",
175 "Ignore length from the Wave header",
176 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
179 gstelement_class->change_state = gst_wavparse_change_state;
180 gstelement_class->send_event = gst_wavparse_send_event;
183 gst_element_class_add_static_pad_template (gstelement_class,
184 &sink_template_factory);
186 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
187 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
188 gst_element_class_add_pad_template (gstelement_class, src_template);
190 gst_element_class_set_static_metadata (gstelement_class, "WAV audio demuxer",
191 "Codec/Demuxer/Audio",
192 "Parse a .wav file into raw audio",
193 "Erik Walthinsen <omega@cse.ogi.edu>");
197 gst_wavparse_reset (GstWavParse * wav)
199 wav->state = GST_WAVPARSE_START;
201 /* These will all be set correctly in the fmt chunk */
215 wav->got_fmt = FALSE;
219 gst_event_unref (wav->seek_event);
220 wav->seek_event = NULL;
222 gst_adapter_clear (wav->adapter);
223 g_object_unref (wav->adapter);
227 gst_tag_list_unref (wav->tags);
230 gst_toc_unref (wav->toc);
233 g_list_free_full (wav->cues, g_free);
236 g_list_free_full (wav->labls, g_free);
239 gst_caps_unref (wav->caps);
241 if (wav->start_segment)
242 gst_event_unref (wav->start_segment);
243 wav->start_segment = NULL;
247 gst_wavparse_dispose (GObject * object)
249 GstWavParse *wav = GST_WAVPARSE (object);
251 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
252 gst_wavparse_reset (wav);
254 G_OBJECT_CLASS (parent_class)->dispose (object);
258 gst_wavparse_init (GstWavParse * wavparse)
260 gst_wavparse_reset (wavparse);
264 gst_pad_new_from_static_template (&sink_template_factory, "sink");
265 gst_pad_set_activate_function (wavparse->sinkpad,
266 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
267 gst_pad_set_activatemode_function (wavparse->sinkpad,
268 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
269 gst_pad_set_chain_function (wavparse->sinkpad,
270 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
271 gst_pad_set_event_function (wavparse->sinkpad,
272 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
273 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
277 gst_pad_new_from_template (gst_element_class_get_pad_template
278 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
279 gst_pad_use_fixed_caps (wavparse->srcpad);
280 gst_pad_set_query_function (wavparse->srcpad,
281 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
282 gst_pad_set_event_function (wavparse->srcpad,
283 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
284 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
288 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
292 if (!gst_riff_parse_file_header (element, buf, &doctype))
295 if (doctype != GST_RIFF_RIFF_WAVE)
303 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
304 ("File is not a WAVE file: 0x%" G_GINT32_MODIFIER "x", doctype));
310 gst_wavparse_stream_init (GstWavParse * wav)
313 GstBuffer *buf = NULL;
315 if ((res = gst_pad_pull_range (wav->sinkpad,
316 wav->offset, 12, &buf)) != GST_FLOW_OK)
318 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
319 return GST_FLOW_ERROR;
327 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
329 /* -1 always maps to -1 */
335 /* 0 always maps to 0 */
342 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
344 } else if (wav->fact) {
345 guint64 bps = gst_util_uint64_scale (wav->datasize, wav->rate, wav->fact);
346 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
353 /* This function is used to perform seeks on the element.
355 * It also works when event is NULL, in which case it will just
356 * start from the last configured segment. This technique is
357 * used when activating the element and to perform the seek in
361 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
365 GstFormat format, bformat;
367 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
368 gint64 cur, stop, upstream_size;
371 GstSegment seeksegment = { 0, };
376 GST_DEBUG_OBJECT (wav, "doing seek with event");
378 gst_event_parse_seek (event, &rate, &format, &flags,
379 &cur_type, &cur, &stop_type, &stop);
380 seqnum = gst_event_get_seqnum (event);
382 /* no negative rates yet */
386 if (format != wav->segment.format) {
387 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
388 gst_format_get_name (format),
389 gst_format_get_name (wav->segment.format));
391 if (cur_type != GST_SEEK_TYPE_NONE)
393 gst_pad_query_convert (wav->srcpad, format, cur,
394 wav->segment.format, &cur);
395 if (res && stop_type != GST_SEEK_TYPE_NONE)
397 gst_pad_query_convert (wav->srcpad, format, stop,
398 wav->segment.format, &stop);
402 format = wav->segment.format;
405 GST_DEBUG_OBJECT (wav, "doing seek without event");
408 cur_type = GST_SEEK_TYPE_SET;
409 stop_type = GST_SEEK_TYPE_SET;
412 /* in push mode, we must delegate to upstream */
413 if (wav->streaming) {
414 gboolean res = FALSE;
416 /* if streaming not yet started; only prepare initial newsegment */
417 if (!event || wav->state != GST_WAVPARSE_DATA) {
418 if (wav->start_segment)
419 gst_event_unref (wav->start_segment);
420 wav->start_segment = gst_event_new_segment (&wav->segment);
423 /* convert seek positions to byte positions in data sections */
424 if (format == GST_FORMAT_TIME) {
425 /* should not fail */
426 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
428 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
431 /* mind sample boundary and header */
433 cur -= (cur % wav->bytes_per_sample);
434 cur += wav->datastart;
437 stop -= (stop % wav->bytes_per_sample);
438 stop += wav->datastart;
440 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
441 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
443 /* BYTE seek event */
444 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
446 gst_event_set_seqnum (event, seqnum);
447 res = gst_pad_push_event (wav->sinkpad, event);
453 flush = flags & GST_SEEK_FLAG_FLUSH;
455 /* now we need to make sure the streaming thread is stopped. We do this by
456 * either sending a FLUSH_START event downstream which will cause the
457 * streaming thread to stop with a WRONG_STATE.
458 * For a non-flushing seek we simply pause the task, which will happen as soon
459 * as it completes one iteration (and thus might block when the sink is
460 * blocking in preroll). */
463 GST_DEBUG_OBJECT (wav, "sending flush start");
465 fevent = gst_event_new_flush_start ();
466 gst_event_set_seqnum (fevent, seqnum);
467 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
468 gst_pad_push_event (wav->srcpad, fevent);
470 gst_pad_pause_task (wav->sinkpad);
473 /* we should now be able to grab the streaming thread because we stopped it
474 * with the above flush/pause code */
475 GST_PAD_STREAM_LOCK (wav->sinkpad);
477 /* save current position */
478 last_stop = wav->segment.position;
480 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
482 /* copy segment, we need this because we still need the old
483 * segment when we close the current segment. */
484 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
486 /* configure the seek parameters in the seeksegment. We will then have the
487 * right values in the segment to perform the seek */
489 GST_DEBUG_OBJECT (wav, "configuring seek");
490 gst_segment_do_seek (&seeksegment, rate, format, flags,
491 cur_type, cur, stop_type, stop, &update);
494 /* figure out the last position we need to play. If it's configured (stop !=
495 * -1), use that, else we play until the total duration of the file */
496 if ((stop = seeksegment.stop) == -1)
497 stop = seeksegment.duration;
499 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
500 if ((cur_type != GST_SEEK_TYPE_NONE)) {
501 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
502 * we can just copy the last_stop. If not, we use the bps to convert TIME to
504 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
505 (gint64 *) & wav->offset))
506 wav->offset = seeksegment.position;
507 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
508 wav->offset -= (wav->offset % wav->bytes_per_sample);
509 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
510 wav->offset += wav->datastart;
511 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
513 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
517 if (stop_type != GST_SEEK_TYPE_NONE) {
518 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
519 wav->end_offset = stop;
520 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
521 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
522 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
523 wav->end_offset += wav->datastart;
524 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
526 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
530 /* make sure filesize is not exceeded due to rounding errors or so,
531 * same precaution as in _stream_headers */
532 bformat = GST_FORMAT_BYTES;
533 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
534 wav->end_offset = MIN (wav->end_offset, upstream_size);
536 if (wav->datasize > 0 && wav->end_offset > wav->datastart + wav->datasize)
537 wav->end_offset = wav->datastart + wav->datasize;
539 /* this is the range of bytes we will use for playback */
540 wav->offset = MIN (wav->offset, wav->end_offset);
541 wav->dataleft = wav->end_offset - wav->offset;
543 GST_DEBUG_OBJECT (wav,
544 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
545 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
546 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
548 /* prepare for streaming again */
552 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
553 GST_DEBUG_OBJECT (wav, "sending flush stop");
555 fevent = gst_event_new_flush_stop (TRUE);
556 gst_event_set_seqnum (fevent, seqnum);
557 gst_pad_push_event (wav->sinkpad, gst_event_ref (fevent));
558 gst_pad_push_event (wav->srcpad, fevent);
561 /* now we did the seek and can activate the new segment values */
562 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
564 /* if we're doing a segment seek, post a SEGMENT_START message */
565 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
566 gst_element_post_message (GST_ELEMENT_CAST (wav),
567 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
568 wav->segment.format, wav->segment.position));
571 /* now create the newsegment */
572 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
573 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
575 /* store the newsegment event so it can be sent from the streaming thread. */
576 if (wav->start_segment)
577 gst_event_unref (wav->start_segment);
578 wav->start_segment = gst_event_new_segment (&wav->segment);
579 gst_event_set_seqnum (wav->start_segment, seqnum);
581 /* mark discont if we are going to stream from another position. */
582 if (last_stop != wav->segment.position) {
583 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
587 /* and start the streaming task again */
588 if (!wav->streaming) {
589 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
593 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
600 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
605 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
610 GST_DEBUG_OBJECT (wav,
611 "Could not determine byte position for desired time");
617 * gst_wavparse_peek_chunk_info:
618 * @wav Wavparse object
619 * @tag holder for tag
620 * @size holder for tag size
622 * Peek next chunk info (tag and size)
624 * Returns: %TRUE when the chunk info (header) is available
627 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
629 const guint8 *data = NULL;
631 if (gst_adapter_available (wav->adapter) < 8)
634 data = gst_adapter_map (wav->adapter, 8);
635 *tag = GST_READ_UINT32_LE (data);
636 *size = GST_READ_UINT32_LE (data + 4);
637 gst_adapter_unmap (wav->adapter);
639 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
640 GST_FOURCC_ARGS (*tag));
646 * gst_wavparse_peek_chunk:
647 * @wav Wavparse object
648 * @tag holder for tag
649 * @size holder for tag size
651 * Peek enough data for one full chunk
653 * Returns: %TRUE when the full chunk is available
656 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
658 guint32 peek_size = 0;
661 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
664 /* size 0 -> empty data buffer would surprise most callers,
665 * large size -> do not bother trying to squeeze that into adapter,
666 * so we throw poor man's exception, which can be caught if caller really
667 * wants to handle 0 size chunk */
668 if (!(*size) || (*size) >= (1 << 30)) {
669 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
670 *size, GST_FOURCC_ARGS (*tag));
671 /* chain should give up */
672 wav->abort_buffering = TRUE;
675 peek_size = (*size + 1) & ~1;
676 available = gst_adapter_available (wav->adapter);
678 if (available >= (8 + peek_size)) {
681 GST_LOG ("but only %u bytes available now", available);
687 * gst_wavparse_calculate_duration:
688 * @wav: wavparse object
690 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
693 * Returns: %TRUE if duration is available.
696 gst_wavparse_calculate_duration (GstWavParse * wav)
698 if (wav->duration > 0)
702 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
704 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
706 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
707 GST_TIME_ARGS (wav->duration));
709 } else if (wav->fact) {
711 gst_util_uint64_scale_ceil (GST_SECOND, wav->fact, wav->rate);
712 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
713 GST_TIME_ARGS (wav->duration));
720 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
725 if (wav->streaming) {
726 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
729 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
730 GST_FOURCC_ARGS (tag));
731 flush = 8 + ((size + 1) & ~1);
732 wav->offset += flush;
733 if (wav->streaming) {
734 gst_adapter_flush (wav->adapter, flush);
736 gst_buffer_unref (buf);
743 * gst_wavparse_cue_chunk:
744 * @wav GstWavParse object
745 * @data holder for data
746 * @size holder for data size
748 * Parse cue chunk from @data to wav->cues.
750 * Returns: %TRUE when cue chunk is available
753 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
760 GST_WARNING_OBJECT (wav, "found another cue's");
764 ncues = GST_READ_UINT32_LE (data);
766 if (size < 4 + ncues * 24) {
767 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
773 for (i = 0; i < ncues; i++) {
774 cue = g_new0 (GstWavParseCue, 1);
775 cue->id = GST_READ_UINT32_LE (data);
776 cue->position = GST_READ_UINT32_LE (data + 4);
777 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
778 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
779 cue->block_start = GST_READ_UINT32_LE (data + 16);
780 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
781 cues = g_list_append (cues, cue);
791 * gst_wavparse_labl_chunk:
792 * @wav GstWavParse object
793 * @data holder for data
794 * @size holder for data size
796 * Parse labl from @data to wav->labls.
798 * Returns: %TRUE when labl chunk is available
801 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
803 GstWavParseLabl *labl;
808 labl = g_new0 (GstWavParseLabl, 1);
812 labl->cue_point_id = GST_READ_UINT32_LE (data);
813 labl->text = g_memdup (data + 4, size - 4);
815 wav->labls = g_list_append (wav->labls, labl);
821 * gst_wavparse_note_chunk:
822 * @wav GstWavParse object
823 * @data holder for data
824 * @size holder for data size
826 * Parse note from @data to wav->notes.
828 * Returns: %TRUE when note chunk is available
831 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
833 GstWavParseNote *note;
838 note = g_new0 (GstWavParseNote, 1);
842 note->cue_point_id = GST_READ_UINT32_LE (data);
843 note->text = g_memdup (data + 4, size - 4);
845 wav->notes = g_list_append (wav->notes, note);
851 * gst_wavparse_smpl_chunk:
852 * @wav GstWavParse object
853 * @data holder for data
854 * @size holder for data size
856 * Parse smpl chunk from @data.
858 * Returns: %TRUE when cue chunk is available
861 gst_wavparse_smpl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
866 manufacturer_id = GST_READ_UINT32_LE (data);
867 product_id = GST_READ_UINT32_LE (data + 4);
868 sample_period = GST_READ_UINT32_LE (data + 8);
870 note_number = GST_READ_UINT32_LE (data + 12);
872 pitch_fraction = GST_READ_UINT32_LE (data + 16);
873 SMPTE_format = GST_READ_UINT32_LE (data + 20);
874 SMPTE_offset = GST_READ_UINT32_LE (data + 24);
875 num_sample_loops = GST_READ_UINT32_LE (data + 28);
876 List of Sample Loops, 24 bytes each
880 wav->tags = gst_tag_list_new_empty ();
881 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
882 GST_TAG_MIDI_BASE_NOTE, (guint) note_number, NULL);
887 * gst_wavparse_adtl_chunk:
888 * @wav GstWavParse object
889 * @data holder for data
890 * @size holder for data size
892 * Parse adtl from @data.
894 * Returns: %TRUE when adtl chunk is available
897 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
899 guint32 ltag, lsize, offset = 0;
902 ltag = GST_READ_UINT32_LE (data + offset);
903 lsize = GST_READ_UINT32_LE (data + offset + 4);
905 if (lsize + 8 > size) {
906 GST_WARNING_OBJECT (wav, "Invalid adtl size: %u + 8 > %u", lsize, size);
911 case GST_RIFF_TAG_labl:
912 gst_wavparse_labl_chunk (wav, data + offset, size);
914 case GST_RIFF_TAG_note:
915 gst_wavparse_note_chunk (wav, data + offset, size);
918 GST_WARNING_OBJECT (wav, "Unknowm adtl %" GST_FOURCC_FORMAT,
919 GST_FOURCC_ARGS (ltag));
920 GST_MEMDUMP_OBJECT (wav, "Unknowm adtl", &data[offset], lsize);
923 offset += 8 + GST_ROUND_UP_2 (lsize);
924 size -= 8 + GST_ROUND_UP_2 (lsize);
931 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
933 GstTagList *tags = NULL;
934 GstTocEntry *entry = NULL;
936 entry = gst_toc_find_entry (toc, id);
938 tags = gst_toc_entry_get_tags (entry);
940 tags = gst_tag_list_new_empty ();
941 gst_toc_entry_set_tags (entry, tags);
949 * gst_wavparse_create_toc:
950 * @wav GstWavParse object
952 * Create TOC from wav->cues and wav->labls.
955 gst_wavparse_create_toc (GstWavParse * wav)
961 GstWavParseLabl *labl;
962 GstWavParseNote *note;
965 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
967 GST_OBJECT_LOCK (wav);
969 GST_OBJECT_UNLOCK (wav);
970 GST_WARNING_OBJECT (wav, "found another TOC");
975 GST_OBJECT_UNLOCK (wav);
979 /* FIXME: send CURRENT scope toc too */
980 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
982 /* add cue edition */
983 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
984 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
985 gst_toc_append_entry (toc, entry);
987 /* add tracks in cue edition */
991 prev_subentry = cur_subentry;
992 /* previous track stop time = current track start time */
993 if (prev_subentry != NULL) {
994 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
995 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
996 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
998 id = g_strdup_printf ("%08x", cue->id);
999 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
1001 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1002 stop = wav->duration;
1003 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
1004 gst_toc_entry_append_sub_entry (entry, cur_subentry);
1005 list = g_list_next (list);
1008 /* add tags in tracks */
1012 id = g_strdup_printf ("%08x", labl->cue_point_id);
1013 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1016 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1019 list = g_list_next (list);
1024 id = g_strdup_printf ("%08x", note->cue_point_id);
1025 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1028 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1031 list = g_list_next (list);
1034 /* send data as TOC */
1037 /* send TOC event */
1039 GST_OBJECT_UNLOCK (wav);
1040 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1046 #define MAX_BUFFER_SIZE 4096
1049 parse_ds64 (GstWavParse * wav, GstBuffer * buf)
1052 guint32 dataSizeLow, dataSizeHigh;
1053 guint32 sampleCountLow, sampleCountHigh;
1055 gst_buffer_map (buf, &map, GST_MAP_READ);
1056 dataSizeLow = GST_READ_UINT32_LE (map.data + 2 * 4);
1057 dataSizeHigh = GST_READ_UINT32_LE (map.data + 3 * 4);
1058 sampleCountLow = GST_READ_UINT32_LE (map.data + 4 * 4);
1059 sampleCountHigh = GST_READ_UINT32_LE (map.data + 5 * 4);
1060 gst_buffer_unmap (buf, &map);
1061 if (dataSizeHigh != 0xFFFFFFFF && dataSizeLow != 0xFFFFFFFF) {
1062 wav->datasize = ((guint64) dataSizeHigh << 32) | dataSizeLow;
1064 if (sampleCountHigh != 0xFFFFFFFF && sampleCountLow != 0xFFFFFFFF) {
1065 wav->fact = ((guint64) sampleCountHigh << 32) | sampleCountLow;
1068 GST_DEBUG_OBJECT (wav, "Got 'ds64' TAG, datasize : %" G_GINT64_FORMAT
1069 " fact: %" G_GINT64_FORMAT, wav->datasize, wav->fact);
1073 static GstFlowReturn
1074 gst_wavparse_stream_headers (GstWavParse * wav)
1076 GstFlowReturn res = GST_FLOW_OK;
1077 GstBuffer *buf = NULL;
1078 gst_riff_strf_auds *header = NULL;
1080 gboolean gotdata = FALSE;
1081 GstCaps *caps = NULL;
1082 gchar *codec_name = NULL;
1083 gint64 upstream_size = 0;
1086 /* search for "_fmt" chunk, which must be before "data" */
1087 while (!wav->got_fmt) {
1090 if (wav->streaming) {
1091 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1094 gst_adapter_flush (wav->adapter, 8);
1098 buf = gst_adapter_take_buffer (wav->adapter, size);
1100 gst_adapter_flush (wav->adapter, 1);
1101 wav->offset += GST_ROUND_UP_2 (size);
1103 buf = gst_buffer_new ();
1106 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1107 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1111 if (tag == GST_RS64_TAG_DS64) {
1112 if (!parse_ds64 (wav, buf))
1118 if (tag != GST_RIFF_TAG_fmt) {
1119 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1120 GST_FOURCC_ARGS (tag));
1121 gst_buffer_unref (buf);
1126 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1128 goto parse_header_error;
1130 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1132 /* do sanity checks of header fields */
1133 if (header->channels == 0)
1135 if (header->rate == 0)
1138 GST_DEBUG_OBJECT (wav, "creating the caps");
1140 /* Note: gst_riff_create_audio_caps might need to fix values in
1141 * the header header depending on the format, so call it first */
1142 /* FIXME: Need to handle the channel reorder map */
1143 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1144 NULL, &codec_name, NULL);
1147 gst_buffer_unref (extra);
1150 goto unknown_format;
1152 /* If we got raw audio from upstream, we remove the codec_data field,
1153 * which may have been added if the wav header included an extended
1154 * chunk. We want to keep it for non raw audio.
1156 s = gst_caps_get_structure (caps, 0);
1157 if (s && gst_structure_has_name (s, "audio/x-raw")) {
1158 gst_structure_remove_field (s, "codec_data");
1161 /* do more sanity checks of header fields
1162 * (these can be sanitized by gst_riff_create_audio_caps()
1164 wav->format = header->format;
1165 wav->rate = header->rate;
1166 wav->channels = header->channels;
1167 wav->blockalign = header->blockalign;
1168 wav->depth = header->bits_per_sample;
1169 wav->av_bps = header->av_bps;
1175 /* do format specific handling */
1176 switch (wav->format) {
1177 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1178 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1180 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1181 * bitrate inside the mpeg stream */
1182 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1186 case GST_RIFF_WAVE_FORMAT_PCM:
1187 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1188 goto invalid_blockalign;
1191 if (wav->av_bps > wav->blockalign * wav->rate)
1193 /* use the configured bps */
1194 wav->bps = wav->av_bps;
1198 wav->width = (wav->blockalign * 8) / wav->channels;
1199 wav->bytes_per_sample = wav->channels * wav->width / 8;
1201 if (wav->bytes_per_sample <= 0)
1202 goto no_bytes_per_sample;
1204 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1205 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1206 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1207 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1208 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1209 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1210 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1212 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1213 * formats). This will make the element output a BYTE format segment and
1214 * will not timestamp the outgoing buffers.
1216 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1218 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1220 /* create pad later so we can sniff the first few bytes
1221 * of the real data and correct our caps if necessary */
1222 gst_caps_replace (&wav->caps, caps);
1223 gst_caps_replace (&caps, NULL);
1225 wav->got_fmt = TRUE;
1227 if (wav->tags == NULL)
1228 wav->tags = gst_tag_list_new_empty ();
1231 GstCaps *templ_caps = gst_pad_get_pad_template_caps (wav->sinkpad);
1232 gst_pb_utils_add_codec_description_to_tag_list (wav->tags,
1233 GST_TAG_CONTAINER_FORMAT, templ_caps);
1234 gst_caps_unref (templ_caps);
1237 /* If bps is nonzero, then we do have a valid bitrate that can be
1238 * announced in a tag list. */
1240 guint bitrate = wav->bps * 8;
1241 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1242 GST_TAG_BITRATE, bitrate, NULL);
1246 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1247 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1249 g_free (codec_name);
1255 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1256 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1258 /* loop headers until we get data */
1260 if (wav->streaming) {
1261 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1268 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1269 &buf)) != GST_FLOW_OK)
1270 goto header_read_error;
1271 gst_buffer_map (buf, &map, GST_MAP_READ);
1272 tag = GST_READ_UINT32_LE (map.data);
1273 size = GST_READ_UINT32_LE (map.data + 4);
1274 gst_buffer_unmap (buf, &map);
1277 GST_INFO_OBJECT (wav,
1278 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT ", size %"
1279 G_GUINT32_FORMAT, GST_FOURCC_ARGS (tag), wav->offset, size);
1281 /* Maximum valid size is INT_MAX */
1282 if (size & 0x80000000) {
1283 GST_WARNING_OBJECT (wav, "Invalid size, clipping to 0x7fffffff");
1287 /* Clip to upstream size if known */
1288 if (wav->datasize > 0 && size + wav->offset > wav->datasize) {
1289 GST_WARNING_OBJECT (wav, "Clipping chunk size to file size");
1290 size = wav->datasize - wav->offset;
1293 /* wav is a st00pid format, we don't know for sure where data starts.
1294 * So we have to go bit by bit until we find the 'data' header
1297 case GST_RIFF_TAG_data:{
1300 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1302 if (wav->ignore_length) {
1303 GST_DEBUG_OBJECT (wav, "Ignoring length");
1306 if (wav->streaming) {
1307 gst_adapter_flush (wav->adapter, 8);
1310 gst_buffer_unref (buf);
1313 wav->datastart = wav->offset;
1314 /* use size from ds64 chunk if available */
1315 if (size64 == -1 && wav->datasize > 0) {
1316 GST_DEBUG_OBJECT (wav, "Using ds64 datasize");
1317 size64 = wav->datasize;
1319 /* If size is zero, then the data chunk probably actually extends to
1320 the end of the file */
1321 if (size64 == 0 && upstream_size) {
1322 size64 = upstream_size - wav->datastart;
1324 /* Or the file might be truncated */
1325 else if (upstream_size) {
1326 size64 = MIN (size64, (upstream_size - wav->datastart));
1328 wav->datasize = size64;
1329 wav->dataleft = size64;
1330 wav->end_offset = size64 + wav->datastart;
1331 if (!wav->streaming) {
1332 /* We will continue parsing tags 'till end */
1333 wav->offset += size64;
1335 GST_DEBUG_OBJECT (wav, "datasize = %" G_GUINT64_FORMAT, size64);
1338 case GST_RIFF_TAG_fact:{
1339 if (wav->fact == 0 &&
1340 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1341 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1342 const guint data_size = 4;
1344 GST_INFO_OBJECT (wav, "Have fact chunk");
1345 if (size < data_size) {
1346 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1347 /* need more data */
1350 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1354 /* number of samples (for compressed formats) */
1355 if (wav->streaming) {
1356 const guint8 *data = NULL;
1358 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1361 gst_adapter_flush (wav->adapter, 8);
1362 data = gst_adapter_map (wav->adapter, data_size);
1363 wav->fact = GST_READ_UINT32_LE (data);
1364 gst_adapter_unmap (wav->adapter);
1365 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1367 gst_buffer_unref (buf);
1370 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1371 data_size, &buf)) != GST_FLOW_OK)
1372 goto header_read_error;
1373 gst_buffer_extract (buf, 0, &wav->fact, 4);
1374 wav->fact = GUINT32_FROM_LE (wav->fact);
1375 gst_buffer_unref (buf);
1377 GST_DEBUG_OBJECT (wav, "have fact %" G_GUINT64_FORMAT, wav->fact);
1378 wav->offset += 8 + GST_ROUND_UP_2 (size);
1381 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1382 /* need more data */
1388 case GST_RIFF_TAG_acid:{
1389 const gst_riff_acid *acid = NULL;
1390 const guint data_size = sizeof (gst_riff_acid);
1393 GST_INFO_OBJECT (wav, "Have acid chunk");
1394 if (size < data_size) {
1395 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1396 /* need more data */
1399 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1403 if (wav->streaming) {
1404 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1407 gst_adapter_flush (wav->adapter, 8);
1408 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1410 tempo = acid->tempo;
1411 gst_adapter_unmap (wav->adapter);
1414 gst_buffer_unref (buf);
1417 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1418 size, &buf)) != GST_FLOW_OK)
1419 goto header_read_error;
1420 gst_buffer_map (buf, &map, GST_MAP_READ);
1421 acid = (const gst_riff_acid *) map.data;
1422 tempo = acid->tempo;
1423 gst_buffer_unmap (buf, &map);
1425 /* send data as tags */
1427 wav->tags = gst_tag_list_new_empty ();
1428 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1429 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1431 size = GST_ROUND_UP_2 (size);
1432 if (wav->streaming) {
1433 gst_adapter_flush (wav->adapter, size);
1435 gst_buffer_unref (buf);
1437 wav->offset += 8 + size;
1440 /* FIXME: all list tags after data are ignored in streaming mode */
1441 case GST_RIFF_TAG_LIST:{
1444 if (wav->streaming) {
1445 const guint8 *data = NULL;
1447 if (gst_adapter_available (wav->adapter) < 12) {
1450 data = gst_adapter_map (wav->adapter, 12);
1451 ltag = GST_READ_UINT32_LE (data + 8);
1452 gst_adapter_unmap (wav->adapter);
1454 gst_buffer_unref (buf);
1457 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1458 &buf)) != GST_FLOW_OK)
1459 goto header_read_error;
1460 gst_buffer_extract (buf, 8, <ag, 4);
1461 ltag = GUINT32_FROM_LE (ltag);
1464 case GST_RIFF_LIST_INFO:{
1465 const gint data_size = size - 4;
1468 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1469 if (wav->streaming) {
1470 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1473 gst_adapter_flush (wav->adapter, 12);
1475 if (data_size > 0) {
1476 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1478 gst_adapter_flush (wav->adapter, 1);
1482 gst_buffer_unref (buf);
1484 if (data_size > 0) {
1486 gst_pad_pull_range (wav->sinkpad, wav->offset,
1487 data_size, &buf)) != GST_FLOW_OK)
1488 goto header_read_error;
1491 if (data_size > 0) {
1493 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1495 GstTagList *old = wav->tags;
1497 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1499 gst_tag_list_unref (old);
1500 gst_tag_list_unref (new);
1502 gst_buffer_unref (buf);
1503 wav->offset += GST_ROUND_UP_2 (data_size);
1507 case GST_RIFF_LIST_adtl:{
1508 const gint data_size = size - 4;
1510 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1511 if (wav->streaming) {
1512 const guint8 *data = NULL;
1514 gst_adapter_flush (wav->adapter, 12);
1516 data = gst_adapter_map (wav->adapter, data_size);
1517 gst_wavparse_adtl_chunk (wav, data, data_size);
1518 gst_adapter_unmap (wav->adapter);
1522 gst_buffer_unref (buf);
1526 gst_pad_pull_range (wav->sinkpad, wav->offset,
1527 data_size, &buf)) != GST_FLOW_OK)
1528 goto header_read_error;
1529 gst_buffer_map (buf, &map, GST_MAP_READ);
1530 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1532 gst_buffer_unmap (buf, &map);
1534 wav->offset += GST_ROUND_UP_2 (data_size);
1538 GST_WARNING_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1539 GST_FOURCC_ARGS (ltag));
1540 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1541 /* need more data */
1547 case GST_RIFF_TAG_cue:{
1548 const guint data_size = size;
1550 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1551 if (wav->streaming) {
1552 const guint8 *data = NULL;
1554 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1557 gst_adapter_flush (wav->adapter, 8);
1559 data = gst_adapter_map (wav->adapter, data_size);
1560 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1561 goto header_read_error;
1563 gst_adapter_unmap (wav->adapter);
1568 gst_buffer_unref (buf);
1571 gst_pad_pull_range (wav->sinkpad, wav->offset,
1572 data_size, &buf)) != GST_FLOW_OK)
1573 goto header_read_error;
1574 gst_buffer_map (buf, &map, GST_MAP_READ);
1575 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1577 goto header_read_error;
1579 gst_buffer_unmap (buf, &map);
1581 size = GST_ROUND_UP_2 (size);
1582 if (wav->streaming) {
1583 gst_adapter_flush (wav->adapter, size);
1585 gst_buffer_unref (buf);
1587 size = GST_ROUND_UP_2 (size);
1588 wav->offset += size;
1591 case GST_RIFF_TAG_smpl:{
1592 const gint data_size = size;
1594 GST_DEBUG_OBJECT (wav, "Have 'smpl' TAG, size : %u", data_size);
1595 if (wav->streaming) {
1596 const guint8 *data = NULL;
1598 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1601 gst_adapter_flush (wav->adapter, 8);
1603 data = gst_adapter_map (wav->adapter, data_size);
1604 if (!gst_wavparse_smpl_chunk (wav, data, data_size)) {
1605 goto header_read_error;
1607 gst_adapter_unmap (wav->adapter);
1612 gst_buffer_unref (buf);
1615 gst_pad_pull_range (wav->sinkpad, wav->offset,
1616 data_size, &buf)) != GST_FLOW_OK)
1617 goto header_read_error;
1618 gst_buffer_map (buf, &map, GST_MAP_READ);
1619 if (!gst_wavparse_smpl_chunk (wav, (const guint8 *) map.data,
1621 goto header_read_error;
1623 gst_buffer_unmap (buf, &map);
1625 size = GST_ROUND_UP_2 (size);
1626 if (wav->streaming) {
1627 gst_adapter_flush (wav->adapter, size);
1629 gst_buffer_unref (buf);
1631 size = GST_ROUND_UP_2 (size);
1632 wav->offset += size;
1636 GST_WARNING_OBJECT (wav, "Ignoring chunk %" GST_FOURCC_FORMAT,
1637 GST_FOURCC_ARGS (tag));
1638 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1639 /* need more data */
1644 if (upstream_size && (wav->offset >= upstream_size)) {
1645 /* Now we are gone through the whole file */
1650 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1652 if (wav->bps <= 0 && wav->fact) {
1654 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1656 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1657 (guint64) wav->fact);
1658 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1663 if (gst_wavparse_calculate_duration (wav)) {
1664 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1665 if (!wav->ignore_length)
1666 wav->segment.duration = wav->duration;
1668 gst_wavparse_create_toc (wav);
1670 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1671 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1672 if (!wav->ignore_length)
1673 wav->segment.duration = wav->datasize;
1676 /* now we have all the info to perform a pending seek if any, if no
1677 * event, this will still do the right thing and it will also send
1678 * the right newsegment event downstream. */
1679 gst_wavparse_perform_seek (wav, wav->seek_event);
1680 /* remove pending event */
1681 gst_event_replace (&wav->seek_event, NULL);
1683 /* we just started, we are discont */
1684 wav->discont = TRUE;
1686 wav->state = GST_WAVPARSE_DATA;
1688 /* determine reasonable max buffer size,
1689 * that is, buffers not too small either size or time wise
1690 * so we do not end up with too many of them */
1692 if (gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size))
1693 wav->max_buf_size = upstream_size;
1695 wav->max_buf_size = 0;
1696 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1697 if (wav->blockalign > 0)
1698 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1700 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1707 g_free (codec_name);
1710 gst_caps_unref (caps);
1715 res = GST_FLOW_ERROR;
1720 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1721 ("Couldn't parse audio header"));
1726 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1727 ("Stream claims to contain no channels - invalid data"));
1732 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1733 ("Stream with sample_rate == 0 - invalid data"));
1738 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1739 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1740 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1745 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1746 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1747 wav->av_bps, wav->blockalign * wav->rate));
1750 no_bytes_per_sample:
1752 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1753 ("Could not caluclate bytes per sample - invalid data"));
1758 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1759 ("No caps found for format 0x%x, %u channels, %u Hz",
1760 wav->format, wav->channels, wav->rate));
1765 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1766 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1772 * Read WAV file tag when streaming
1774 static GstFlowReturn
1775 gst_wavparse_parse_stream_init (GstWavParse * wav)
1777 if (gst_adapter_available (wav->adapter) >= 12) {
1780 /* _take flushes the data */
1781 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1783 GST_DEBUG ("Parsing wav header");
1784 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1785 return GST_FLOW_ERROR;
1788 /* Go to next state */
1789 wav->state = GST_WAVPARSE_HEADER;
1794 /* handle an event sent directly to the element.
1796 * This event can be sent either in the READY state or the
1797 * >READY state. The only event of interest really is the seek
1800 * In the READY state we can only store the event and try to
1801 * respect it when going to PAUSED. We assume we are in the
1802 * READY state when our parsing state != GST_WAVPARSE_DATA.
1804 * When we are steaming, we can simply perform the seek right
1808 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1810 GstWavParse *wav = GST_WAVPARSE (element);
1811 gboolean res = FALSE;
1813 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1815 switch (GST_EVENT_TYPE (event)) {
1816 case GST_EVENT_SEEK:
1817 if (wav->state == GST_WAVPARSE_DATA) {
1818 /* we can handle the seek directly when streaming data */
1819 res = gst_wavparse_perform_seek (wav, event);
1821 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1823 gst_event_replace (&wav->seek_event, event);
1825 /* we always return true */
1832 gst_event_unref (event);
1837 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1841 s = gst_caps_get_structure (caps, 0);
1842 if (!gst_structure_has_name (s, "audio/x-dts"))
1844 /* typefind behavior for DTS:
1845 * MAXIMUM: multiple frame syncs detected, certainly DTS
1846 * LIKELY: single frame sync at offset 0. Maybe DTS?
1847 * POSSIBLE: single frame sync, not at offset 0. Highly unlikely
1849 if (prob > GST_TYPE_FIND_LIKELY)
1851 if (prob <= GST_TYPE_FIND_POSSIBLE)
1853 /* for maybe, check for at least a valid-looking rate and channels */
1854 if (!gst_structure_has_field (s, "channels"))
1856 /* and for extra assurance we could also check the rate from the DTS frame
1857 * against the one in the wav header, but for now let's not do that */
1858 return gst_structure_has_field (s, "rate");
1862 gst_wavparse_get_upstream_tags (GstWavParse * wav, GstTagScope scope)
1864 GstTagList *tags = NULL;
1869 while ((ev = gst_pad_get_sticky_event (wav->sinkpad, GST_EVENT_TAG, i++))) {
1870 gst_event_parse_tag (ev, &tags);
1871 if (tags != NULL && gst_tag_list_get_scope (tags) == scope) {
1872 tags = gst_tag_list_copy (tags);
1873 gst_tag_list_remove_tag (tags, GST_TAG_CONTAINER_FORMAT);
1874 gst_event_unref (ev);
1878 gst_event_unref (ev);
1884 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1887 GstTagList *tags, *utags;
1889 GST_DEBUG_OBJECT (wav, "adding src pad");
1891 g_assert (wav->caps != NULL);
1893 s = gst_caps_get_structure (wav->caps, 0);
1894 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1895 GstTypeFindProbability prob;
1898 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1899 if (tf_caps != NULL) {
1900 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1901 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1902 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1903 gst_caps_unref (wav->caps);
1904 wav->caps = tf_caps;
1906 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1907 GST_TAG_AUDIO_CODEC, "dts", NULL);
1909 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1910 "marked as raw PCM audio, but ignoring for now", tf_caps);
1911 gst_caps_unref (tf_caps);
1916 gst_pad_set_caps (wav->srcpad, wav->caps);
1918 if (wav->start_segment) {
1919 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1920 gst_pad_push_event (wav->srcpad, wav->start_segment);
1921 wav->start_segment = NULL;
1924 /* upstream tags, e.g. from id3/ape tag before the wav file; assume for now
1925 * that there'll be only one scope/type of tag list from upstream, if any */
1926 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_GLOBAL);
1928 utags = gst_wavparse_get_upstream_tags (wav, GST_TAG_SCOPE_STREAM);
1930 /* if there's a tag upstream it's probably been added to override the
1931 * tags from inside the wav header, so keep upstream tags if in doubt */
1932 tags = gst_tag_list_merge (utags, wav->tags, GST_TAG_MERGE_KEEP);
1934 if (wav->tags != NULL) {
1935 gst_tag_list_unref (wav->tags);
1940 gst_tag_list_unref (utags);
1942 /* send tags downstream, if any */
1944 gst_pad_push_event (wav->srcpad, gst_event_new_tag (tags));
1947 static GstFlowReturn
1948 gst_wavparse_stream_data (GstWavParse * wav)
1950 GstBuffer *buf = NULL;
1951 GstFlowReturn res = GST_FLOW_OK;
1952 guint64 desired, obtained;
1953 GstClockTime timestamp, next_timestamp, duration;
1954 guint64 pos, nextpos;
1957 GST_LOG_OBJECT (wav,
1958 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1959 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1961 /* Get the next n bytes and output them */
1962 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1965 /* scale the amount of data by the segment rate so we get equal
1966 * amounts of data regardless of the playback rate */
1968 MIN (gst_guint64_to_gdouble (wav->dataleft),
1969 wav->max_buf_size * ABS (wav->segment.rate));
1971 if (desired >= wav->blockalign && wav->blockalign > 0)
1972 desired -= (desired % wav->blockalign);
1974 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1975 "from the sinkpad", desired);
1977 if (wav->streaming) {
1978 guint avail = gst_adapter_available (wav->adapter);
1981 /* flush some bytes if evil upstream sends segment that starts
1982 * before data or does is not send sample aligned segment */
1983 if (G_LIKELY (wav->offset >= wav->datastart)) {
1984 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1986 extra = wav->datastart - wav->offset;
1989 if (G_UNLIKELY (extra)) {
1990 extra = wav->bytes_per_sample - extra;
1991 if (extra <= avail) {
1992 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
1993 gst_adapter_flush (wav->adapter, extra);
1994 wav->offset += extra;
1995 wav->dataleft -= extra;
1996 goto iterate_adapter;
1998 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
1999 gst_adapter_clear (wav->adapter);
2000 wav->offset += avail;
2001 wav->dataleft -= avail;
2006 if (avail < desired) {
2007 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
2011 buf = gst_adapter_take_buffer (wav->adapter, desired);
2013 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
2014 desired, &buf)) != GST_FLOW_OK)
2017 /* we may get a short buffer at the end of the file */
2018 if (gst_buffer_get_size (buf) < desired) {
2019 gsize size = gst_buffer_get_size (buf);
2021 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2022 if (size >= wav->blockalign) {
2023 if (wav->blockalign > 0) {
2024 buf = gst_buffer_make_writable (buf);
2025 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2028 gst_buffer_unref (buf);
2034 obtained = gst_buffer_get_size (buf);
2036 /* our positions in bytes */
2037 pos = wav->offset - wav->datastart;
2038 nextpos = pos + obtained;
2040 /* update offsets, does not overflow. */
2041 buf = gst_buffer_make_writable (buf);
2042 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2043 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2045 /* first chunk of data? create the source pad. We do this only here so
2046 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2047 if (G_UNLIKELY (wav->first)) {
2049 /* this will also push the segment events */
2050 gst_wavparse_add_src_pad (wav, buf);
2052 /* If we have a pending start segment, send it now. */
2053 if (G_UNLIKELY (wav->start_segment != NULL)) {
2054 gst_pad_push_event (wav->srcpad, wav->start_segment);
2055 wav->start_segment = NULL;
2060 /* and timestamps if we have a bitrate, be careful for overflows */
2062 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2064 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2065 duration = next_timestamp - timestamp;
2067 /* update current running segment position */
2068 if (G_LIKELY (next_timestamp >= wav->segment.start))
2069 wav->segment.position = next_timestamp;
2070 } else if (wav->fact) {
2072 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2073 /* and timestamps if we have a bitrate, be careful for overflows */
2074 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2075 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2076 duration = next_timestamp - timestamp;
2078 /* no bitrate, all we know is that the first sample has timestamp 0, all
2079 * other positions and durations have unknown timestamp. */
2083 timestamp = GST_CLOCK_TIME_NONE;
2084 duration = GST_CLOCK_TIME_NONE;
2085 /* update current running segment position with byte offset */
2086 if (G_LIKELY (nextpos >= wav->segment.start))
2087 wav->segment.position = nextpos;
2089 if ((pos > 0) && wav->vbr) {
2090 /* don't set timestamps for VBR files if it's not the first buffer */
2091 timestamp = GST_CLOCK_TIME_NONE;
2092 duration = GST_CLOCK_TIME_NONE;
2095 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2096 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2097 wav->discont = FALSE;
2100 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2101 GST_BUFFER_DURATION (buf) = duration;
2103 GST_LOG_OBJECT (wav,
2104 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2105 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2106 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2108 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2111 if (obtained < wav->dataleft) {
2112 wav->offset += obtained;
2113 wav->dataleft -= obtained;
2115 wav->offset += wav->dataleft;
2119 /* Iterate until need more data, so adapter size won't grow */
2120 if (wav->streaming) {
2121 GST_LOG_OBJECT (wav,
2122 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2124 goto iterate_adapter;
2131 GST_DEBUG_OBJECT (wav, "found EOS");
2132 return GST_FLOW_EOS;
2136 /* check if we got EOS */
2137 if (res == GST_FLOW_EOS)
2140 GST_WARNING_OBJECT (wav,
2141 "Error getting %" G_GINT64_FORMAT " bytes from the "
2142 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2147 GST_INFO_OBJECT (wav,
2148 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2149 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2150 gst_pad_is_linked (wav->srcpad));
2156 gst_wavparse_loop (GstPad * pad)
2159 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2163 GST_LOG_OBJECT (wav, "process data");
2165 switch (wav->state) {
2166 case GST_WAVPARSE_START:
2167 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2168 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2172 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2173 event = gst_event_new_stream_start (stream_id);
2174 gst_event_set_group_id (event, gst_util_group_id_next ());
2175 gst_pad_push_event (wav->srcpad, event);
2178 wav->state = GST_WAVPARSE_HEADER;
2181 case GST_WAVPARSE_HEADER:
2182 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2183 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2186 wav->state = GST_WAVPARSE_DATA;
2187 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2190 case GST_WAVPARSE_DATA:
2191 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2195 g_assert_not_reached ();
2202 const gchar *reason = gst_flow_get_name (ret);
2204 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2205 gst_pad_pause_task (pad);
2207 if (ret == GST_FLOW_EOS) {
2208 /* handle end-of-stream/segment */
2209 /* so align our position with the end of it, if there is one
2210 * this ensures a subsequent will arrive at correct base/acc time */
2211 if (wav->segment.format == GST_FORMAT_TIME) {
2212 if (wav->segment.rate > 0.0 &&
2213 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2214 wav->segment.position = wav->segment.stop;
2215 else if (wav->segment.rate < 0.0)
2216 wav->segment.position = wav->segment.start;
2218 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2219 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2220 ("No valid input found before end of stream"));
2221 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2223 /* add pad before we perform EOS */
2224 if (G_UNLIKELY (wav->first)) {
2226 gst_wavparse_add_src_pad (wav, NULL);
2229 /* perform EOS logic */
2230 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2233 if ((stop = wav->segment.stop) == -1)
2234 stop = wav->segment.duration;
2236 gst_element_post_message (GST_ELEMENT_CAST (wav),
2237 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2238 wav->segment.format, stop));
2239 gst_pad_push_event (wav->srcpad,
2240 gst_event_new_segment_done (wav->segment.format, stop));
2242 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2245 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2246 /* for fatal errors we post an error message, post the error
2247 * first so the app knows about the error first. */
2248 GST_ELEMENT_FLOW_ERROR (wav, ret);
2249 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2255 static GstFlowReturn
2256 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2259 GstWavParse *wav = GST_WAVPARSE (parent);
2261 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2262 gst_buffer_get_size (buf));
2264 gst_adapter_push (wav->adapter, buf);
2266 switch (wav->state) {
2267 case GST_WAVPARSE_START:
2268 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2269 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2272 if (wav->state != GST_WAVPARSE_HEADER)
2275 /* otherwise fall-through */
2276 case GST_WAVPARSE_HEADER:
2277 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2278 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2281 if (!wav->got_fmt || wav->datastart == 0)
2284 wav->state = GST_WAVPARSE_DATA;
2285 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2288 case GST_WAVPARSE_DATA:
2289 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2290 wav->discont = TRUE;
2291 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2295 g_return_val_if_reached (GST_FLOW_ERROR);
2298 if (G_UNLIKELY (wav->abort_buffering)) {
2299 wav->abort_buffering = FALSE;
2300 ret = GST_FLOW_ERROR;
2301 /* sort of demux/parse error */
2302 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2308 static GstFlowReturn
2309 gst_wavparse_flush_data (GstWavParse * wav)
2311 GstFlowReturn ret = GST_FLOW_OK;
2314 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2315 ret = gst_wavparse_stream_data (wav);
2322 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2324 GstWavParse *wav = GST_WAVPARSE (parent);
2325 gboolean ret = TRUE;
2327 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2329 switch (GST_EVENT_TYPE (event)) {
2330 case GST_EVENT_CAPS:
2332 /* discard, we'll come up with proper src caps */
2333 gst_event_unref (event);
2336 case GST_EVENT_SEGMENT:
2338 gint64 start, stop, offset = 0, end_offset = -1;
2341 /* some debug output */
2342 gst_event_copy_segment (event, &segment);
2343 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2346 if (wav->state != GST_WAVPARSE_DATA) {
2347 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2351 /* now we are either committed to TIME or BYTE format,
2352 * and we only expect a BYTE segment, e.g. following a seek */
2353 if (segment.format == GST_FORMAT_BYTES) {
2354 /* handle (un)signed issues */
2355 start = segment.start;
2356 stop = segment.stop;
2359 start -= wav->datastart;
2360 start = MAX (start, 0);
2364 stop -= wav->datastart;
2365 stop = MAX (stop, 0);
2367 if (wav->segment.format == GST_FORMAT_TIME) {
2368 guint64 bps = wav->bps;
2370 /* operating in format TIME, so we can convert */
2371 if (!bps && wav->fact)
2373 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2377 gst_util_uint64_scale_ceil (start, GST_SECOND,
2378 (guint64) wav->bps);
2381 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2382 (guint64) wav->bps);
2386 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2390 segment.start = start;
2391 segment.stop = stop;
2393 /* accept upstream's notion of segment and distribute along */
2394 segment.format = wav->segment.format;
2395 segment.time = segment.position = segment.start;
2396 segment.duration = wav->segment.duration;
2397 segment.base = gst_segment_to_running_time (&wav->segment,
2398 GST_FORMAT_TIME, wav->segment.position);
2400 gst_segment_copy_into (&segment, &wav->segment);
2402 /* also store the newsegment event for the streaming thread */
2403 if (wav->start_segment)
2404 gst_event_unref (wav->start_segment);
2405 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2406 wav->start_segment = gst_event_new_segment (&segment);
2408 /* stream leftover data in current segment */
2409 gst_wavparse_flush_data (wav);
2410 /* and set up streaming thread for next one */
2411 wav->offset = offset;
2412 wav->end_offset = end_offset;
2414 if (wav->datasize > 0 && (wav->end_offset == -1
2415 || wav->end_offset > wav->datastart + wav->datasize))
2416 wav->end_offset = wav->datastart + wav->datasize;
2418 if (wav->end_offset != -1) {
2419 wav->dataleft = wav->end_offset - wav->offset;
2421 /* infinity; upstream will EOS when done */
2422 wav->dataleft = G_MAXUINT64;
2425 gst_event_unref (event);
2429 if (wav->state == GST_WAVPARSE_START || !wav->caps) {
2430 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2431 ("No valid input found before end of stream"));
2433 /* add pad if needed so EOS is seen downstream */
2434 if (G_UNLIKELY (wav->first)) {
2436 gst_wavparse_add_src_pad (wav, NULL);
2438 /* stream leftover data in current segment */
2439 gst_wavparse_flush_data (wav);
2444 case GST_EVENT_FLUSH_STOP:
2448 gst_adapter_clear (wav->adapter);
2449 wav->discont = TRUE;
2450 dur = wav->segment.duration;
2451 gst_segment_init (&wav->segment, wav->segment.format);
2452 wav->segment.duration = dur;
2456 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2464 /* convert and query stuff */
2465 static const GstFormat *
2466 gst_wavparse_get_formats (GstPad * pad)
2468 static const GstFormat formats[] = {
2471 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2480 gst_wavparse_pad_convert (GstPad * pad,
2481 GstFormat src_format, gint64 src_value,
2482 GstFormat * dest_format, gint64 * dest_value)
2484 GstWavParse *wavparse;
2485 gboolean res = TRUE;
2487 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2489 if (*dest_format == src_format) {
2490 *dest_value = src_value;
2494 if ((wavparse->bps == 0) && !wavparse->fact)
2497 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2498 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2500 switch (src_format) {
2501 case GST_FORMAT_BYTES:
2502 switch (*dest_format) {
2503 case GST_FORMAT_DEFAULT:
2504 *dest_value = src_value / wavparse->bytes_per_sample;
2505 /* make sure we end up on a sample boundary */
2506 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2508 case GST_FORMAT_TIME:
2509 /* src_value + datastart = offset */
2510 GST_INFO_OBJECT (wavparse,
2511 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2513 if (wavparse->bps > 0)
2514 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2515 (guint64) wavparse->bps);
2516 else if (wavparse->fact) {
2517 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2518 wavparse->rate, wavparse->fact);
2521 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2532 case GST_FORMAT_DEFAULT:
2533 switch (*dest_format) {
2534 case GST_FORMAT_BYTES:
2535 *dest_value = src_value * wavparse->bytes_per_sample;
2537 case GST_FORMAT_TIME:
2538 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2539 (guint64) wavparse->rate);
2547 case GST_FORMAT_TIME:
2548 switch (*dest_format) {
2549 case GST_FORMAT_BYTES:
2550 if (wavparse->bps > 0)
2551 *dest_value = gst_util_uint64_scale (src_value,
2552 (guint64) wavparse->bps, GST_SECOND);
2554 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2555 wavparse->rate, wavparse->fact);
2557 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2559 /* make sure we end up on a sample boundary */
2560 *dest_value -= *dest_value % wavparse->blockalign;
2562 case GST_FORMAT_DEFAULT:
2563 *dest_value = gst_util_uint64_scale (src_value,
2564 (guint64) wavparse->rate, GST_SECOND);
2583 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2589 /* handle queries for location and length in requested format */
2591 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2593 gboolean res = TRUE;
2594 GstWavParse *wav = GST_WAVPARSE (parent);
2596 /* only if we know */
2597 if (wav->state != GST_WAVPARSE_DATA) {
2601 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2603 switch (GST_QUERY_TYPE (query)) {
2604 case GST_QUERY_POSITION:
2610 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2611 curb = wav->offset - wav->datastart;
2612 gst_query_parse_position (query, &format, NULL);
2613 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2616 case GST_FORMAT_BYTES:
2617 format = GST_FORMAT_BYTES;
2621 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2626 gst_query_set_position (query, format, cur);
2629 case GST_QUERY_DURATION:
2631 gint64 duration = 0;
2634 if (wav->ignore_length) {
2639 gst_query_parse_duration (query, &format, NULL);
2642 case GST_FORMAT_BYTES:{
2643 format = GST_FORMAT_BYTES;
2644 duration = wav->datasize;
2647 case GST_FORMAT_TIME:
2648 if ((res = gst_wavparse_calculate_duration (wav))) {
2649 duration = wav->duration;
2657 gst_query_set_duration (query, format, duration);
2660 case GST_QUERY_CONVERT:
2662 gint64 srcvalue, dstvalue;
2663 GstFormat srcformat, dstformat;
2665 gst_query_parse_convert (query, &srcformat, &srcvalue,
2666 &dstformat, &dstvalue);
2667 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2668 &dstformat, &dstvalue);
2670 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2673 case GST_QUERY_SEEKING:{
2675 gboolean seekable = FALSE;
2677 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2678 if (fmt == wav->segment.format) {
2679 if (wav->streaming) {
2682 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2683 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2684 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2685 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2687 gst_query_unref (q);
2689 GST_LOG_OBJECT (wav, "looping => seekable");
2693 } else if (fmt == GST_FORMAT_TIME) {
2697 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2702 res = gst_pad_query_default (pad, parent, query);
2709 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2711 GstWavParse *wavparse = GST_WAVPARSE (parent);
2712 gboolean res = FALSE;
2714 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2716 switch (GST_EVENT_TYPE (event)) {
2717 case GST_EVENT_SEEK:
2718 /* can only handle events when we are in the data state */
2719 if (wavparse->state == GST_WAVPARSE_DATA) {
2720 res = gst_wavparse_perform_seek (wavparse, event);
2722 gst_event_unref (event);
2725 case GST_EVENT_TOC_SELECT:
2728 GstTocEntry *entry = NULL;
2729 GstEvent *seek_event;
2732 if (!wavparse->toc) {
2733 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2736 gst_event_parse_toc_select (event, &uid);
2738 GST_OBJECT_LOCK (wavparse);
2739 entry = gst_toc_find_entry (wavparse->toc, uid);
2740 if (entry == NULL) {
2741 GST_OBJECT_UNLOCK (wavparse);
2742 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2746 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2747 GST_OBJECT_UNLOCK (wavparse);
2748 seek_event = gst_event_new_seek (1.0,
2750 GST_SEEK_FLAG_FLUSH,
2751 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2752 res = gst_wavparse_perform_seek (wavparse, seek_event);
2753 gst_event_unref (seek_event);
2757 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2761 gst_event_unref (event);
2766 res = gst_pad_push_event (wavparse->sinkpad, event);
2773 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2775 GstWavParse *wav = GST_WAVPARSE (parent);
2780 gst_adapter_clear (wav->adapter);
2781 g_object_unref (wav->adapter);
2782 wav->adapter = NULL;
2785 query = gst_query_new_scheduling ();
2787 if (!gst_pad_peer_query (sinkpad, query)) {
2788 gst_query_unref (query);
2792 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2793 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2794 gst_query_unref (query);
2799 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2800 wav->streaming = FALSE;
2801 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2805 GST_DEBUG_OBJECT (sinkpad, "activating push");
2806 wav->streaming = TRUE;
2807 wav->adapter = gst_adapter_new ();
2808 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2814 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2815 GstPadMode mode, gboolean active)
2820 case GST_PAD_MODE_PUSH:
2823 case GST_PAD_MODE_PULL:
2825 /* if we have a scheduler we can start the task */
2826 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2829 res = gst_pad_stop_task (sinkpad);
2839 static GstStateChangeReturn
2840 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2842 GstStateChangeReturn ret;
2843 GstWavParse *wav = GST_WAVPARSE (element);
2845 switch (transition) {
2846 case GST_STATE_CHANGE_NULL_TO_READY:
2848 case GST_STATE_CHANGE_READY_TO_PAUSED:
2849 gst_wavparse_reset (wav);
2851 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2857 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2859 switch (transition) {
2860 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2862 case GST_STATE_CHANGE_PAUSED_TO_READY:
2863 gst_wavparse_reset (wav);
2865 case GST_STATE_CHANGE_READY_TO_NULL:
2874 gst_wavparse_set_property (GObject * object, guint prop_id,
2875 const GValue * value, GParamSpec * pspec)
2879 g_return_if_fail (GST_IS_WAVPARSE (object));
2880 self = GST_WAVPARSE (object);
2883 case PROP_IGNORE_LENGTH:
2884 self->ignore_length = g_value_get_boolean (value);
2887 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2893 gst_wavparse_get_property (GObject * object, guint prop_id,
2894 GValue * value, GParamSpec * pspec)
2898 g_return_if_fail (GST_IS_WAVPARSE (object));
2899 self = GST_WAVPARSE (object);
2902 case PROP_IGNORE_LENGTH:
2903 g_value_set_boolean (value, self->ignore_length);
2906 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2911 plugin_init (GstPlugin * plugin)
2915 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2919 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2922 "Parse a .wav file into raw audio",
2923 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)