1 /* GStreamer wavpack plugin
2 * Copyright (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
3 * Copyright (c) 2006 Tim-Philipp Müller <tim centricular net>
4 * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
6 * gstwavpackparse.c: wavpack file parser
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:element-wavpackparse
28 * WavpackParse takes raw, unframed Wavpack streams and splits them into
29 * single Wavpack chunks with information like bit depth and the position
31 * <ulink url="http://www.wavpack.com/">Wavpack</ulink> is an open-source
32 * audio codec that features both lossless and lossy encoding.
33 * <title>Example launch line</title>
36 * gst-launch filesrc location=test.wv ! wavpackparse ! wavpackdec ! fakesink
38 * This pipeline decodes the Wavpack file test.wv into raw audio buffers.
47 #include <gst/gst-i18n-plugin.h>
52 #include <wavpack/wavpack.h>
53 #include "gstwavpackparse.h"
54 #include "gstwavpackstreamreader.h"
55 #include "gstwavpackcommon.h"
57 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_parse_debug);
58 #define GST_CAT_DEFAULT gst_wavpack_parse_debug
60 /* FIXME: unconditionally use GSlice after we depend on GLib >= 2.10 */
61 #if GLIB_CHECK_VERSION (2, 10, 0)
62 static inline GstWavpackParseIndexEntry *
63 gst_wavpack_parse_index_entry_new ()
65 return g_slice_new (GstWavpackParseIndexEntry);
69 gst_wavpack_parse_index_entry_free (GstWavpackParseIndexEntry * entry)
71 g_slice_free (GstWavpackParseIndexEntry, entry);
74 static inline GstWavpackParseIndexEntry *
75 gst_wavpack_parse_index_entry_new ()
77 return g_new (GstWavpackParseIndexEntry, 1);
81 gst_wavpack_parse_index_entry_free (GstWavpackParseIndexEntry * entry)
87 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
90 GST_STATIC_CAPS ("audio/x-wavpack, "
91 "framed = (boolean) false; "
92 "audio/x-wavpack-correction, " "framed = (boolean) false")
95 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
98 GST_STATIC_CAPS ("audio/x-wavpack, "
99 "width = (int) [ 1, 32 ], "
100 "channels = (int) [ 1, 8 ], "
101 "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
104 static GstStaticPadTemplate wvc_src_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
107 GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) true")
110 static gboolean gst_wavpack_parse_sink_activate (GstPad * sinkpad);
112 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active);
114 static void gst_wavpack_parse_loop (GstElement * element);
115 static GstStateChangeReturn gst_wavpack_parse_change_state (GstElement *
116 element, GstStateChange transition);
117 static void gst_wavpack_parse_reset (GstWavpackParse * parse);
118 static gint64 gst_wavpack_parse_get_upstream_length (GstWavpackParse * wvparse);
119 static GstBuffer *gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse,
120 gint64 offset, guint size, GstFlowReturn * flow);
121 static GstFlowReturn gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf);
123 GST_BOILERPLATE (GstWavpackParse, gst_wavpack_parse, GstElement,
127 gst_wavpack_parse_base_init (gpointer klass)
129 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
131 gst_element_class_add_pad_template (element_class,
132 gst_static_pad_template_get (&src_factory));
133 gst_element_class_add_pad_template (element_class,
134 gst_static_pad_template_get (&wvc_src_factory));
135 gst_element_class_add_pad_template (element_class,
136 gst_static_pad_template_get (&sink_factory));
138 gst_element_class_set_details_simple (element_class, "Wavpack parser",
139 "Codec/Demuxer/Audio",
140 "Parses Wavpack files",
141 "Arwed v. Merkatz <v.merkatz@gmx.net>, "
142 "Sebastian Dröge <slomo@circular-chaos.org>");
146 gst_wavpack_parse_finalize (GObject * object)
148 gst_wavpack_parse_reset (GST_WAVPACK_PARSE (object));
150 G_OBJECT_CLASS (parent_class)->finalize (object);
154 gst_wavpack_parse_class_init (GstWavpackParseClass * klass)
156 GObjectClass *gobject_class;
157 GstElementClass *gstelement_class;
159 gobject_class = (GObjectClass *) klass;
160 gstelement_class = (GstElementClass *) klass;
162 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wavpack_parse_finalize);
163 gstelement_class->change_state =
164 GST_DEBUG_FUNCPTR (gst_wavpack_parse_change_state);
167 static GstWavpackParseIndexEntry *
168 gst_wavpack_parse_index_get_last_entry (GstWavpackParse * wvparse)
170 g_assert (wvparse->entries != NULL);
172 return wvparse->entries->data;
175 static GstWavpackParseIndexEntry *
176 gst_wavpack_parse_index_get_entry_from_sample (GstWavpackParse * wvparse,
177 gint64 sample_offset)
182 if (wvparse->entries == NULL)
185 for (node = wvparse->entries, i = 0; node; node = node->next, i++) {
186 GstWavpackParseIndexEntry *entry;
190 GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
191 " byte %" G_GINT64_FORMAT, i, entry->sample_offset, entry->byte_offset);
193 if (entry->sample_offset <= sample_offset &&
194 sample_offset < entry->sample_offset_end) {
195 GST_LOG_OBJECT (wvparse, "found match");
199 /* as the list is sorted and we first look at the latest entry
200 * we can abort searching for an entry if the sample we want is
201 * after the latest one */
202 if (sample_offset >= entry->sample_offset_end)
205 GST_LOG_OBJECT (wvparse, "no match in index");
210 gst_wavpack_parse_index_append_entry (GstWavpackParse * wvparse,
211 gint64 byte_offset, gint64 sample_offset, gint64 num_samples)
213 GstWavpackParseIndexEntry *entry;
215 /* do we have this one already? */
216 if (wvparse->entries) {
217 entry = gst_wavpack_parse_index_get_last_entry (wvparse);
218 if (entry->byte_offset >= byte_offset
219 || entry->sample_offset >= sample_offset)
223 GST_LOG_OBJECT (wvparse, "Adding index entry %8" G_GINT64_FORMAT " - %"
224 GST_TIME_FORMAT " @ offset 0x%08" G_GINT64_MODIFIER "x", sample_offset,
225 GST_TIME_ARGS (gst_util_uint64_scale_int (sample_offset,
226 GST_SECOND, wvparse->samplerate)), byte_offset);
228 entry = gst_wavpack_parse_index_entry_new ();
229 entry->byte_offset = byte_offset;
230 entry->sample_offset = sample_offset;
231 entry->sample_offset_end = sample_offset + num_samples;
232 wvparse->entries = g_slist_prepend (wvparse->entries, entry);
236 gst_wavpack_parse_reset (GstWavpackParse * parse)
238 parse->total_samples = -1;
239 parse->samplerate = 0;
242 gst_segment_init (&parse->segment, GST_FORMAT_UNDEFINED);
243 parse->next_block_index = 0;
245 parse->current_offset = 0;
246 parse->need_newsegment = TRUE;
247 parse->discont = TRUE;
248 parse->upstream_length = -1;
250 if (parse->entries) {
251 g_slist_foreach (parse->entries, (GFunc) gst_wavpack_parse_index_entry_free,
253 g_slist_free (parse->entries);
254 parse->entries = NULL;
257 if (parse->adapter) {
258 gst_adapter_clear (parse->adapter);
259 g_object_unref (parse->adapter);
260 parse->adapter = NULL;
263 if (parse->srcpad != NULL) {
266 GST_DEBUG_OBJECT (parse, "Removing src pad");
267 res = gst_element_remove_pad (GST_ELEMENT (parse), parse->srcpad);
268 g_return_if_fail (res != FALSE);
269 gst_object_unref (parse->srcpad);
270 parse->srcpad = NULL;
273 g_list_foreach (parse->queued_events, (GFunc) gst_mini_object_unref, NULL);
274 g_list_free (parse->queued_events);
275 parse->queued_events = NULL;
277 if (parse->pending_buffer)
278 gst_buffer_unref (parse->pending_buffer);
280 parse->pending_buffer = NULL;
283 static const GstQueryType *
284 gst_wavpack_parse_get_src_query_types (GstPad * pad)
286 static const GstQueryType types[] = {
297 gst_wavpack_parse_src_query (GstPad * pad, GstQuery * query)
299 GstWavpackParse *parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
301 gboolean ret = FALSE;
303 switch (GST_QUERY_TYPE (query)) {
304 case GST_QUERY_POSITION:{
308 GST_OBJECT_LOCK (parse);
309 cur = parse->segment.last_stop;
310 rate = parse->samplerate;
311 GST_OBJECT_UNLOCK (parse);
314 GST_DEBUG_OBJECT (parse, "haven't read header yet");
318 gst_query_parse_position (query, &format, NULL);
321 case GST_FORMAT_TIME:
322 cur = gst_util_uint64_scale_int (cur, GST_SECOND, rate);
323 gst_query_set_position (query, GST_FORMAT_TIME, cur);
326 case GST_FORMAT_DEFAULT:
327 gst_query_set_position (query, GST_FORMAT_DEFAULT, cur);
331 GST_DEBUG_OBJECT (parse, "cannot handle position query in "
332 "%s format. Forwarding upstream.", gst_format_get_name (format));
333 ret = gst_pad_query_default (pad, query);
338 case GST_QUERY_DURATION:{
342 GST_OBJECT_LOCK (parse);
343 rate = parse->samplerate;
344 len = parse->total_samples;
345 GST_OBJECT_UNLOCK (parse);
348 GST_DEBUG_OBJECT (parse, "haven't read header yet");
352 gst_query_parse_duration (query, &format, NULL);
355 case GST_FORMAT_TIME:
357 len = gst_util_uint64_scale_int (len, GST_SECOND, rate);
358 gst_query_set_duration (query, GST_FORMAT_TIME, len);
361 case GST_FORMAT_DEFAULT:
362 gst_query_set_duration (query, GST_FORMAT_DEFAULT, len);
366 GST_DEBUG_OBJECT (parse, "cannot handle duration query in "
367 "%s format. Forwarding upstream.", gst_format_get_name (format));
368 ret = gst_pad_query_default (pad, query);
373 case GST_QUERY_SEEKING:{
374 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
375 if (format == GST_FORMAT_TIME || format == GST_FORMAT_DEFAULT) {
377 gint64 duration = -1;
379 /* only fails if we didn't read the headers yet and can't say
380 * anything about our seeking capabilities */
381 if (!gst_pad_query_duration (pad, &format, &duration))
384 /* can't seek in streaming mode yet */
385 GST_OBJECT_LOCK (parse);
386 seekable = (parse->adapter == NULL);
387 GST_OBJECT_UNLOCK (parse);
389 gst_query_set_seeking (query, format, seekable, 0, duration);
395 ret = gst_pad_query_default (pad, query);
400 gst_object_unref (parse);
405 /* returns TRUE on success, with byte_offset set to the offset of the
406 * wavpack chunk containing the sample requested. start_sample will be
407 * set to the first sample in the chunk starting at byte_offset.
408 * Scanning from the last known header offset to the wanted position
409 * when seeking forward isn't very clever, but seems fast enough in
410 * practice and has the nice side effect of populating our index
413 gst_wavpack_parse_scan_to_find_sample (GstWavpackParse * parse,
414 gint64 sample, gint64 * byte_offset, gint64 * start_sample)
416 GstWavpackParseIndexEntry *entry;
420 /* first, check if we have to scan at all */
421 entry = gst_wavpack_parse_index_get_entry_from_sample (parse, sample);
423 *byte_offset = entry->byte_offset;
424 *start_sample = entry->sample_offset;
425 GST_LOG_OBJECT (parse, "Found index entry: sample %" G_GINT64_FORMAT
426 " @ offset %" G_GINT64_FORMAT, entry->sample_offset,
431 GST_LOG_OBJECT (parse, "No matching entry in index, scanning file ...");
433 /* if we have an index, we can start scanning from the last known offset
434 * in there, after all we know our wanted sample is not in the index */
435 if (parse->entries) {
436 GstWavpackParseIndexEntry *entry;
438 entry = gst_wavpack_parse_index_get_last_entry (parse);
439 off = entry->byte_offset;
442 /* now scan forward until we find the chunk we're looking for or hit EOS */
444 WavpackHeader header;
447 buf = gst_wavpack_parse_pull_buffer (parse, off, sizeof (WavpackHeader),
453 gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
454 gst_buffer_unref (buf);
456 if (header.flags & INITIAL_BLOCK)
457 gst_wavpack_parse_index_append_entry (parse, off, header.block_index,
458 header.block_samples);
462 if (header.block_index <= sample &&
463 sample < (header.block_index + header.block_samples)) {
465 *start_sample = header.block_index;
469 off += header.ckSize + 8;
472 GST_DEBUG_OBJECT (parse, "scan failed: %s (off=0x%08" G_GINT64_MODIFIER "x)",
473 gst_flow_get_name (ret), off);
479 gst_wavpack_parse_send_newsegment (GstWavpackParse * wvparse, gboolean update)
481 GstSegment *s = &wvparse->segment;
483 gint64 stop_time = -1;
484 gint64 start_time = 0;
488 /* segment is in DEFAULT format, but we want to send a TIME newsegment */
489 start_time = gst_util_uint64_scale_int (s->start, GST_SECOND,
490 wvparse->samplerate);
493 stop_time = gst_util_uint64_scale_int (s->stop, GST_SECOND,
494 wvparse->samplerate);
497 GST_DEBUG_OBJECT (wvparse, "sending newsegment from %" GST_TIME_FORMAT
498 " to %" GST_TIME_FORMAT, GST_TIME_ARGS (start_time),
499 GST_TIME_ARGS (stop_time));
501 /* after a seek, s->last_stop will point to a chunk boundary, ie. from
502 * which sample we will start sending data again, while s->start will
503 * point to the sample we actually want to seek to and want to start
504 * playing right after the seek. Adjust clock-time for the difference
505 * so we start playing from start_time */
506 cur_pos_time = gst_util_uint64_scale_int (s->last_stop, GST_SECOND,
507 wvparse->samplerate);
508 diff = start_time - cur_pos_time;
510 ret = gst_pad_push_event (wvparse->srcpad,
511 gst_event_new_new_segment (update, s->rate, GST_FORMAT_TIME,
512 start_time, stop_time, start_time - diff));
518 gst_wavpack_parse_handle_seek_event (GstWavpackParse * wvparse,
521 GstSeekFlags seek_flags;
522 GstSeekType start_type;
523 GstSeekType stop_type;
526 gboolean only_update;
530 gint64 start; /* sample we want to seek to */
531 gint64 byte_offset; /* byte offset the chunk we seek to starts at */
532 gint64 chunk_start; /* first sample in chunk we seek to */
536 if (wvparse->adapter) {
537 GST_DEBUG_OBJECT (wvparse, "seeking in streaming mode not implemented yet");
541 gst_event_parse_seek (event, &speed, &format, &seek_flags, &start_type,
542 &start, &stop_type, &stop);
544 if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME) {
545 GST_DEBUG ("seeking is only supported in TIME or DEFAULT format");
550 GST_DEBUG ("only forward playback supported, rate %f not allowed", speed);
554 GST_OBJECT_LOCK (wvparse);
556 rate = wvparse->samplerate;
558 GST_OBJECT_UNLOCK (wvparse);
559 GST_DEBUG ("haven't read header yet");
563 /* figure out the last position we need to play. If it's configured (stop !=
564 * -1), use that, else we play until the total duration of the file */
566 stop = wvparse->segment.duration;
568 /* convert from time to samples if necessary */
569 if (format == GST_FORMAT_TIME) {
570 if (start_type != GST_SEEK_TYPE_NONE)
571 start = gst_util_uint64_scale_int (start, rate, GST_SECOND);
572 if (stop_type != GST_SEEK_TYPE_NONE)
573 stop = gst_util_uint64_scale_int (stop, rate, GST_SECOND);
577 GST_OBJECT_UNLOCK (wvparse);
578 GST_DEBUG_OBJECT (wvparse, "Invalid start sample %" G_GINT64_FORMAT, start);
582 flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
584 /* operate on segment copy until we know the seek worked */
585 segment = wvparse->segment;
587 gst_segment_set_seek (&segment, speed, GST_FORMAT_DEFAULT,
588 seek_flags, start_type, start, stop_type, stop, &only_update);
592 wvparse->segment = segment;
593 gst_wavpack_parse_send_newsegment (wvparse, TRUE);
598 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_start ());
601 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_start ());
603 gst_pad_pause_task (wvparse->sinkpad);
606 GST_PAD_STREAM_LOCK (wvparse->sinkpad);
608 /* Save current position */
609 last_stop = wvparse->segment.last_stop;
611 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_stop ());
614 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_stop ());
617 GST_DEBUG_OBJECT (wvparse, "Performing seek to %" GST_TIME_FORMAT " sample %"
618 G_GINT64_FORMAT, GST_TIME_ARGS (segment.start * GST_SECOND / rate),
621 ret = gst_wavpack_parse_scan_to_find_sample (wvparse, segment.start,
622 &byte_offset, &chunk_start);
625 GST_DEBUG_OBJECT (wvparse, "new offset: %" G_GINT64_FORMAT, byte_offset);
626 wvparse->current_offset = byte_offset;
627 /* we want to send a newsegment event with the actual seek position
628 * as start, even though our first buffer might start before the
629 * configured segment. We leave it up to the decoder or sink to crop
630 * the output buffers accordingly */
631 wvparse->segment = segment;
632 wvparse->segment.last_stop = chunk_start;
633 wvparse->need_newsegment = TRUE;
634 wvparse->discont = (last_stop != chunk_start) ? TRUE : FALSE;
636 /* if we're doing a segment seek, post a SEGMENT_START message */
637 if (wvparse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
638 gst_element_post_message (GST_ELEMENT_CAST (wvparse),
639 gst_message_new_segment_start (GST_OBJECT_CAST (wvparse),
640 wvparse->segment.format, wvparse->segment.last_stop));
643 GST_DEBUG_OBJECT (wvparse, "seek failed: don't know where to seek to");
646 GST_PAD_STREAM_UNLOCK (wvparse->sinkpad);
647 GST_OBJECT_UNLOCK (wvparse);
649 gst_pad_start_task (wvparse->sinkpad,
650 (GstTaskFunction) gst_wavpack_parse_loop, wvparse);
656 gst_wavpack_parse_sink_event (GstPad * pad, GstEvent * event)
658 GstWavpackParse *parse;
661 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
663 switch (GST_EVENT_TYPE (event)) {
664 case GST_EVENT_FLUSH_STOP:{
665 if (parse->adapter) {
666 gst_adapter_clear (parse->adapter);
668 if (parse->pending_buffer) {
669 gst_buffer_unref (parse->pending_buffer);
670 parse->pending_buffer = NULL;
671 parse->pending_offset = 0;
673 ret = gst_pad_push_event (parse->srcpad, event);
676 case GST_EVENT_NEWSEGMENT:{
677 parse->need_newsegment = TRUE;
678 gst_event_unref (event);
683 if (parse->adapter) {
684 /* remove all bytes that are left in the adapter after EOS. They can't
685 * be a complete Wavpack block and we can't do anything with them */
686 gst_adapter_clear (parse->adapter);
688 if (parse->pending_buffer) {
689 gst_buffer_unref (parse->pending_buffer);
690 parse->pending_buffer = NULL;
691 parse->pending_offset = 0;
693 ret = gst_pad_push_event (parse->srcpad, event);
697 /* stream lock is recursive, should be fine for all events */
698 GST_PAD_STREAM_LOCK (pad);
699 if (parse->srcpad == NULL) {
700 parse->queued_events = g_list_append (parse->queued_events, event);
702 ret = gst_pad_push_event (parse->srcpad, event);
704 GST_PAD_STREAM_UNLOCK (pad);
709 gst_object_unref (parse);
714 gst_wavpack_parse_src_event (GstPad * pad, GstEvent * event)
716 GstWavpackParse *parse;
719 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
721 switch (GST_EVENT_TYPE (event)) {
723 ret = gst_wavpack_parse_handle_seek_event (parse, event);
726 ret = gst_pad_event_default (pad, event);
730 gst_object_unref (parse);
735 gst_wavpack_parse_init (GstWavpackParse * parse, GstWavpackParseClass * gclass)
737 GstElementClass *klass = GST_ELEMENT_GET_CLASS (parse);
738 GstPadTemplate *tmpl;
740 tmpl = gst_element_class_get_pad_template (klass, "sink");
741 parse->sinkpad = gst_pad_new_from_template (tmpl, "sink");
743 gst_pad_set_activate_function (parse->sinkpad,
744 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate));
745 gst_pad_set_activatepull_function (parse->sinkpad,
746 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate_pull));
747 gst_pad_set_event_function (parse->sinkpad,
748 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_event));
749 gst_pad_set_chain_function (parse->sinkpad,
750 GST_DEBUG_FUNCPTR (gst_wavpack_parse_chain));
752 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
754 parse->srcpad = NULL;
755 gst_wavpack_parse_reset (parse);
759 gst_wavpack_parse_get_upstream_length (GstWavpackParse * parse)
763 GstFormat format = GST_FORMAT_BYTES;
765 if (!gst_pad_query_peer_duration (parse->sinkpad, &format, &length)) {
768 GST_DEBUG ("upstream length: %" G_GINT64_FORMAT, length);
774 gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse, gint64 offset,
775 guint size, GstFlowReturn * flow)
777 GstFlowReturn flow_ret;
778 GstBuffer *buf = NULL;
780 if (offset + size >= wvparse->upstream_length) {
781 wvparse->upstream_length = gst_wavpack_parse_get_upstream_length (wvparse);
782 if (offset + size >= wvparse->upstream_length) {
783 GST_DEBUG_OBJECT (wvparse, "EOS: %" G_GINT64_FORMAT " + %u > %"
784 G_GINT64_FORMAT, offset, size, wvparse->upstream_length);
785 flow_ret = GST_FLOW_UNEXPECTED;
790 flow_ret = gst_pad_pull_range (wvparse->sinkpad, offset, size, &buf);
792 if (flow_ret != GST_FLOW_OK) {
793 GST_DEBUG_OBJECT (wvparse, "pull_range (%" G_GINT64_FORMAT ", %u) "
794 "failed, flow: %s", offset, size, gst_flow_get_name (flow_ret));
799 if (GST_BUFFER_SIZE (buf) < size) {
800 GST_DEBUG_OBJECT (wvparse, "Short read at offset %" G_GINT64_FORMAT
801 ", got only %u of %u bytes", offset, GST_BUFFER_SIZE (buf), size);
802 gst_buffer_unref (buf);
804 flow_ret = GST_FLOW_UNEXPECTED;
814 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
815 WavpackHeader * header)
817 GstWavpackMetadata meta;
818 GstCaps *caps = NULL;
821 g_assert (wvparse->srcpad == NULL);
823 bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
825 while (gst_wavpack_read_metadata (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
827 case ID_WVC_BITSTREAM:{
828 caps = gst_caps_new_simple ("audio/x-wavpack-correction",
829 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
831 gst_pad_new_from_template (gst_element_class_get_pad_template
832 (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
835 case ID_WV_BITSTREAM:
836 case ID_WVX_BITSTREAM:{
837 WavpackStreamReader *stream_reader = gst_wavpack_stream_reader_new ();
843 rid.buffer = GST_BUFFER_DATA (buf);
844 rid.length = GST_BUFFER_SIZE (buf);
848 WavpackOpenFileInputEx (stream_reader, &rid, NULL, error_msg, 0, 0);
853 wvparse->samplerate = WavpackGetSampleRate (wpc);
854 wvparse->channels = WavpackGetNumChannels (wpc);
855 wvparse->total_samples = (header->total_samples == (int32_t) - 1) ?
856 -1 : header->total_samples;
858 caps = gst_caps_new_simple ("audio/x-wavpack",
859 "width", G_TYPE_INT, WavpackGetBitsPerSample (wpc),
860 "channels", G_TYPE_INT, wvparse->channels,
861 "rate", G_TYPE_INT, wvparse->samplerate,
862 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
863 #ifdef WAVPACK_OLD_API
864 channel_mask = wpc->config.channel_mask;
866 channel_mask = WavpackGetChannelMask (wpc);
868 if (channel_mask == 0)
870 gst_wavpack_get_default_channel_mask (wvparse->channels);
872 if (channel_mask != 0) {
873 if (!gst_wavpack_set_channel_layout (caps, channel_mask)) {
874 GST_WARNING_OBJECT (wvparse, "Failed to set channel layout");
875 gst_caps_unref (caps);
877 WavpackCloseFile (wpc);
878 g_free (stream_reader);
884 gst_pad_new_from_template (gst_element_class_get_pad_template
885 (GST_ELEMENT_GET_CLASS (wvparse), "src"), "src");
886 WavpackCloseFile (wpc);
887 g_free (stream_reader);
891 GST_LOG_OBJECT (wvparse, "unhandled ID: 0x%02x", meta.id);
899 if (caps == NULL || wvparse->srcpad == NULL)
902 GST_DEBUG_OBJECT (wvparse, "Added src pad with caps %" GST_PTR_FORMAT, caps);
904 gst_pad_set_query_function (wvparse->srcpad,
905 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_query));
906 gst_pad_set_query_type_function (wvparse->srcpad,
907 GST_DEBUG_FUNCPTR (gst_wavpack_parse_get_src_query_types));
908 gst_pad_set_event_function (wvparse->srcpad,
909 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_event));
911 gst_pad_set_caps (wvparse->srcpad, caps);
912 gst_caps_unref (caps);
913 gst_pad_use_fixed_caps (wvparse->srcpad);
915 gst_object_ref (wvparse->srcpad);
916 gst_pad_set_active (wvparse->srcpad, TRUE);
917 gst_element_add_pad (GST_ELEMENT (wvparse), wvparse->srcpad);
918 gst_element_no_more_pads (GST_ELEMENT (wvparse));
924 gst_wavpack_parse_push_buffer (GstWavpackParse * wvparse, GstBuffer * buf,
925 WavpackHeader * header)
927 wvparse->current_offset += header->ckSize + 8;
929 wvparse->segment.last_stop = header->block_index;
931 if (wvparse->need_newsegment) {
932 if (gst_wavpack_parse_send_newsegment (wvparse, FALSE))
933 wvparse->need_newsegment = FALSE;
936 /* send any queued events */
937 if (wvparse->queued_events) {
940 for (l = wvparse->queued_events; l != NULL; l = l->next) {
941 gst_pad_push_event (wvparse->srcpad, GST_EVENT (l->data));
943 g_list_free (wvparse->queued_events);
944 wvparse->queued_events = NULL;
947 if (wvparse->pending_buffer == NULL) {
948 wvparse->pending_buffer = buf;
949 wvparse->pending_offset = header->block_index;
950 } else if (wvparse->pending_offset == header->block_index) {
951 wvparse->pending_buffer = gst_buffer_join (wvparse->pending_buffer, buf);
953 GST_ERROR ("Got incomplete block, dropping");
954 gst_buffer_unref (wvparse->pending_buffer);
955 wvparse->pending_buffer = buf;
956 wvparse->pending_offset = header->block_index;
959 if (!(header->flags & FINAL_BLOCK))
962 buf = wvparse->pending_buffer;
963 wvparse->pending_buffer = NULL;
965 GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (header->block_index,
966 GST_SECOND, wvparse->samplerate);
967 GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (header->block_samples,
968 GST_SECOND, wvparse->samplerate);
969 GST_BUFFER_OFFSET (buf) = header->block_index;
970 GST_BUFFER_OFFSET_END (buf) = header->block_index + header->block_samples;
972 if (wvparse->discont || wvparse->next_block_index != header->block_index) {
973 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
974 wvparse->discont = FALSE;
977 wvparse->next_block_index = header->block_index + header->block_samples;
979 gst_buffer_set_caps (buf, GST_PAD_CAPS (wvparse->srcpad));
981 GST_LOG_OBJECT (wvparse, "Pushing buffer with time %" GST_TIME_FORMAT,
982 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
984 return gst_pad_push (wvparse->srcpad, buf);
988 gst_wavpack_parse_find_marker (guint8 * buf, guint size)
993 if (G_UNLIKELY (size < 4))
996 for (i = 0; i < size - 4; i++) {
997 if (memcmp (buf + i, "wvpk", 4) == 0) {
1005 static GstFlowReturn
1006 gst_wavpack_parse_resync_loop (GstWavpackParse * parse, WavpackHeader * header)
1008 GstFlowReturn flow_ret = GST_FLOW_UNEXPECTED;
1009 GstBuffer *buf = NULL;
1011 /* loop until we have a frame header or reach the end of the stream */
1013 guint8 *data, *marker;
1017 gst_buffer_unref (buf);
1021 if (parse->upstream_length == 0 ||
1022 parse->upstream_length <= parse->current_offset) {
1023 parse->upstream_length = gst_wavpack_parse_get_upstream_length (parse);
1024 if (parse->upstream_length == 0 ||
1025 parse->upstream_length <= parse->current_offset) {
1030 len = MIN (parse->upstream_length - parse->current_offset, 2048);
1032 GST_LOG_OBJECT (parse, "offset: %" G_GINT64_FORMAT, parse->current_offset);
1034 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1037 /* whatever the problem is, there's nothing more for us to do for now */
1038 if (flow_ret != GST_FLOW_OK)
1041 data = GST_BUFFER_DATA (buf);
1042 size = GST_BUFFER_SIZE (buf);
1044 /* not enough data for a header? */
1045 if (size < sizeof (WavpackHeader))
1048 /* got a header right where we are at now? */
1049 if (gst_wavpack_read_header (header, data))
1052 /* nope, let's see if we can find one */
1053 marker = gst_wavpack_parse_find_marker (data + 1, size - 1);
1056 parse->current_offset += marker - data;
1057 /* do one more loop iteration to make sure we pull enough
1058 * data for a full header, we'll bail out then */
1060 parse->current_offset += len - 4;
1065 gst_buffer_unref (buf);
1071 gst_wavpack_parse_loop (GstElement * element)
1073 GstWavpackParse *parse = GST_WAVPACK_PARSE (element);
1074 GstFlowReturn flow_ret;
1075 WavpackHeader header = { {0,}, 0, };
1076 GstBuffer *buf = NULL;
1078 flow_ret = gst_wavpack_parse_resync_loop (parse, &header);
1080 if (flow_ret != GST_FLOW_OK)
1083 GST_LOG_OBJECT (parse, "Read header at offset %" G_GINT64_FORMAT
1084 ": chunk size = %u+8", parse->current_offset, header.ckSize);
1086 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1087 header.ckSize + 8, &flow_ret);
1089 if (flow_ret != GST_FLOW_OK)
1092 if (parse->srcpad == NULL) {
1093 if (!gst_wavpack_parse_create_src_pad (parse, buf, &header)) {
1094 GST_ERROR_OBJECT (parse, "Failed to create src pad");
1095 flow_ret = GST_FLOW_ERROR;
1099 if (header.flags & INITIAL_BLOCK)
1100 gst_wavpack_parse_index_append_entry (parse, parse->current_offset,
1101 header.block_index, header.block_samples);
1103 flow_ret = gst_wavpack_parse_push_buffer (parse, buf, &header);
1104 if (flow_ret != GST_FLOW_OK)
1111 const gchar *reason = gst_flow_get_name (flow_ret);
1113 GST_LOG_OBJECT (parse, "pausing task, reason %s", reason);
1114 gst_pad_pause_task (parse->sinkpad);
1116 if (GST_FLOW_IS_FATAL (flow_ret) || flow_ret == GST_FLOW_NOT_LINKED) {
1117 if (flow_ret == GST_FLOW_UNEXPECTED && parse->srcpad) {
1118 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1121 GST_LOG_OBJECT (parse, "Sending segment done");
1123 if ((stop = parse->segment.stop) == -1)
1124 stop = parse->segment.duration;
1126 gst_element_post_message (GST_ELEMENT_CAST (parse),
1127 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
1128 parse->segment.format, stop));
1130 GST_LOG_OBJECT (parse, "Sending EOS, at end of stream");
1131 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1134 GST_ELEMENT_ERROR (parse, STREAM, FAILED,
1135 (_("Internal data stream error.")),
1136 ("stream stopped, reason %s", reason));
1138 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1146 gst_wavpack_parse_resync_adapter (GstAdapter * adapter)
1148 const guint8 *buf, *marker;
1149 guint avail = gst_adapter_available (adapter);
1154 /* if the marker is at the beginning don't do the expensive search */
1155 buf = gst_adapter_peek (adapter, 4);
1156 if (memcmp (buf, "wvpk", 4) == 0)
1162 /* search for the marker in the complete content of the adapter */
1163 buf = gst_adapter_peek (adapter, avail);
1164 if (buf && (marker = gst_wavpack_parse_find_marker ((guint8 *) buf, avail))) {
1165 gst_adapter_flush (adapter, marker - buf);
1169 /* flush everything except the last 4 bytes. they could contain
1170 * the start of a new marker */
1171 gst_adapter_flush (adapter, avail - 4);
1176 static GstFlowReturn
1177 gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf)
1179 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (GST_PAD_PARENT (pad));
1180 GstFlowReturn ret = GST_FLOW_OK;
1182 const guint8 *tmp_buf;
1184 if (!wvparse->adapter) {
1185 wvparse->adapter = gst_adapter_new ();
1188 if (GST_BUFFER_IS_DISCONT (buf)) {
1189 gst_adapter_clear (wvparse->adapter);
1190 wvparse->discont = TRUE;
1193 gst_adapter_push (wvparse->adapter, buf);
1195 if (gst_adapter_available (wvparse->adapter) < sizeof (WavpackHeader))
1198 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1201 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1202 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1204 while (gst_adapter_available (wvparse->adapter) >= wph.ckSize + 4 * 1 + 4) {
1206 gst_adapter_take_buffer (wvparse->adapter, wph.ckSize + 4 * 1 + 4);
1209 return GST_FLOW_ERROR;
1211 if (wvparse->srcpad == NULL) {
1212 if (!gst_wavpack_parse_create_src_pad (wvparse, outbuf, &wph)) {
1213 GST_ERROR_OBJECT (wvparse, "Failed to create src pad");
1214 ret = GST_FLOW_ERROR;
1219 ret = gst_wavpack_parse_push_buffer (wvparse, outbuf, &wph);
1221 if (ret != GST_FLOW_OK)
1224 if (gst_adapter_available (wvparse->adapter) >= sizeof (WavpackHeader)) {
1225 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1227 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1230 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1237 static GstStateChangeReturn
1238 gst_wavpack_parse_change_state (GstElement * element, GstStateChange transition)
1240 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (element);
1241 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1243 switch (transition) {
1244 case GST_STATE_CHANGE_READY_TO_PAUSED:
1245 gst_segment_init (&wvparse->segment, GST_FORMAT_DEFAULT);
1246 wvparse->segment.last_stop = 0;
1251 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1252 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1254 switch (transition) {
1255 case GST_STATE_CHANGE_PAUSED_TO_READY:
1256 gst_wavpack_parse_reset (wvparse);
1266 gst_wavpack_parse_sink_activate (GstPad * sinkpad)
1268 if (gst_pad_check_pull_range (sinkpad)) {
1269 return gst_pad_activate_pull (sinkpad, TRUE);
1271 return gst_pad_activate_push (sinkpad, TRUE);
1276 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
1281 result = gst_pad_start_task (sinkpad,
1282 (GstTaskFunction) gst_wavpack_parse_loop, GST_PAD_PARENT (sinkpad));
1284 result = gst_pad_stop_task (sinkpad);
1291 gst_wavpack_parse_plugin_init (GstPlugin * plugin)
1293 if (!gst_element_register (plugin, "wavpackparse",
1294 GST_RANK_PRIMARY, GST_TYPE_WAVPACK_PARSE)) {
1298 GST_DEBUG_CATEGORY_INIT (gst_wavpack_parse_debug, "wavpack_parse", 0,
1299 "Wavpack file parser");