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
27 * WavpackParse takes raw, unframed Wavpack streams and splits them into
28 * single Wavpack chunks with information like bit depth and the position
30 * <ulink url="http://www.wavpack.com/">Wavpack</ulink> is an open-source
31 * audio codec that features both lossless and lossy encoding.
34 * <title>Example launch line</title>
36 * gst-launch filesrc location=test.wv ! wavpackparse ! wavpackdec ! fakesink
37 * ]| This pipeline decodes the Wavpack file test.wv into raw audio buffers.
45 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
46 * with newer GLib versions (>= 2.31.0) */
47 #define GLIB_DISABLE_DEPRECATION_WARNINGS
50 #include <gst/gst-i18n-plugin.h>
55 #include <wavpack/wavpack.h>
56 #include "gstwavpackparse.h"
57 #include "gstwavpackstreamreader.h"
58 #include "gstwavpackcommon.h"
60 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_parse_debug);
61 #define GST_CAT_DEFAULT gst_wavpack_parse_debug
63 static inline GstWavpackParseIndexEntry *
64 gst_wavpack_parse_index_entry_new (void)
66 return g_slice_new (GstWavpackParseIndexEntry);
70 gst_wavpack_parse_index_entry_free (GstWavpackParseIndexEntry * entry)
72 g_slice_free (GstWavpackParseIndexEntry, entry);
75 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
78 GST_STATIC_CAPS ("audio/x-wavpack, "
79 "framed = (boolean) false; "
80 "audio/x-wavpack-correction, " "framed = (boolean) false")
83 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
86 GST_STATIC_CAPS ("audio/x-wavpack, "
87 "width = (int) [ 1, 32 ], "
88 "channels = (int) [ 1, 8 ], "
89 "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
92 static GstStaticPadTemplate wvc_src_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
95 GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) true")
98 static gboolean gst_wavpack_parse_sink_activate (GstPad * sinkpad);
101 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active);
103 static void gst_wavpack_parse_loop (GstElement * element);
105 static GstStateChangeReturn gst_wavpack_parse_change_state (GstElement *
106 element, GstStateChange transition);
107 static void gst_wavpack_parse_reset (GstWavpackParse * parse);
109 static gint64 gst_wavpack_parse_get_upstream_length (GstWavpackParse * wvparse);
111 static GstBuffer *gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse,
112 gint64 offset, guint size, GstFlowReturn * flow);
113 static GstFlowReturn gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf);
115 GST_BOILERPLATE (GstWavpackParse, gst_wavpack_parse, GstElement,
119 gst_wavpack_parse_base_init (gpointer klass)
121 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
123 gst_element_class_add_static_pad_template (element_class, &src_factory);
124 gst_element_class_add_static_pad_template (element_class,
126 gst_element_class_add_static_pad_template (element_class, &sink_factory);
128 gst_element_class_set_details_simple (element_class, "Wavpack parser",
129 "Codec/Demuxer/Audio",
130 "Parses Wavpack files",
131 "Arwed v. Merkatz <v.merkatz@gmx.net>, "
132 "Sebastian Dröge <slomo@circular-chaos.org>");
136 gst_wavpack_parse_finalize (GObject * object)
138 gst_wavpack_parse_reset (GST_WAVPACK_PARSE (object));
140 G_OBJECT_CLASS (parent_class)->finalize (object);
144 gst_wavpack_parse_class_init (GstWavpackParseClass * klass)
146 GObjectClass *gobject_class;
148 GstElementClass *gstelement_class;
150 gobject_class = (GObjectClass *) klass;
151 gstelement_class = (GstElementClass *) klass;
153 gobject_class->finalize = gst_wavpack_parse_finalize;
154 gstelement_class->change_state =
155 GST_DEBUG_FUNCPTR (gst_wavpack_parse_change_state);
158 static GstWavpackParseIndexEntry *
159 gst_wavpack_parse_index_get_last_entry (GstWavpackParse * wvparse)
161 g_assert (wvparse->entries != NULL);
163 return wvparse->entries->data;
166 static GstWavpackParseIndexEntry *
167 gst_wavpack_parse_index_get_entry_from_sample (GstWavpackParse * wvparse,
168 gint64 sample_offset)
174 if (wvparse->entries == NULL)
177 for (node = wvparse->entries, i = 0; node; node = node->next, i++) {
178 GstWavpackParseIndexEntry *entry;
182 GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
183 " byte %" G_GINT64_FORMAT, i, entry->sample_offset, entry->byte_offset);
185 if (entry->sample_offset <= sample_offset &&
186 sample_offset < entry->sample_offset_end) {
187 GST_LOG_OBJECT (wvparse, "found match");
191 /* as the list is sorted and we first look at the latest entry
192 * we can abort searching for an entry if the sample we want is
193 * after the latest one */
194 if (sample_offset >= entry->sample_offset_end)
197 GST_LOG_OBJECT (wvparse, "no match in index");
202 gst_wavpack_parse_index_append_entry (GstWavpackParse * wvparse,
203 gint64 byte_offset, gint64 sample_offset, gint64 num_samples)
205 GstWavpackParseIndexEntry *entry;
207 /* do we have this one already? */
208 if (wvparse->entries) {
209 entry = gst_wavpack_parse_index_get_last_entry (wvparse);
210 if (entry->byte_offset >= byte_offset
211 || entry->sample_offset >= sample_offset)
215 GST_LOG_OBJECT (wvparse, "Adding index entry %8" G_GINT64_FORMAT " - %"
216 GST_TIME_FORMAT " @ offset 0x%08" G_GINT64_MODIFIER "x", sample_offset,
217 GST_TIME_ARGS (gst_util_uint64_scale_int (sample_offset,
218 GST_SECOND, wvparse->samplerate)), byte_offset);
220 entry = gst_wavpack_parse_index_entry_new ();
221 entry->byte_offset = byte_offset;
222 entry->sample_offset = sample_offset;
223 entry->sample_offset_end = sample_offset + num_samples;
224 wvparse->entries = g_slist_prepend (wvparse->entries, entry);
228 gst_wavpack_parse_reset (GstWavpackParse * parse)
230 parse->total_samples = G_GINT64_CONSTANT (-1);
231 parse->samplerate = 0;
234 gst_segment_init (&parse->segment, GST_FORMAT_UNDEFINED);
235 parse->next_block_index = 0;
237 parse->current_offset = 0;
238 parse->need_newsegment = TRUE;
239 parse->discont = TRUE;
240 parse->upstream_length = -1;
242 if (parse->entries) {
243 g_slist_foreach (parse->entries, (GFunc) gst_wavpack_parse_index_entry_free,
245 g_slist_free (parse->entries);
246 parse->entries = NULL;
249 if (parse->adapter) {
250 gst_adapter_clear (parse->adapter);
251 g_object_unref (parse->adapter);
252 parse->adapter = NULL;
255 if (parse->srcpad != NULL) {
258 GST_DEBUG_OBJECT (parse, "Removing src pad");
259 res = gst_element_remove_pad (GST_ELEMENT (parse), parse->srcpad);
260 g_return_if_fail (res != FALSE);
261 gst_object_unref (parse->srcpad);
262 parse->srcpad = NULL;
265 g_list_foreach (parse->queued_events, (GFunc) gst_mini_object_unref, NULL);
266 g_list_free (parse->queued_events);
267 parse->queued_events = NULL;
269 if (parse->pending_buffer)
270 gst_buffer_unref (parse->pending_buffer);
272 parse->pending_buffer = NULL;
275 static const GstQueryType *
276 gst_wavpack_parse_get_src_query_types (GstPad * pad)
278 static const GstQueryType types[] = {
289 gst_wavpack_parse_src_query (GstPad * pad, GstQuery * query)
291 GstWavpackParse *parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
295 gboolean ret = FALSE;
297 switch (GST_QUERY_TYPE (query)) {
298 case GST_QUERY_POSITION:{
303 GST_OBJECT_LOCK (parse);
304 cur = parse->segment.last_stop;
305 rate = parse->samplerate;
306 GST_OBJECT_UNLOCK (parse);
309 GST_DEBUG_OBJECT (parse, "haven't read header yet");
313 gst_query_parse_position (query, &format, NULL);
316 case GST_FORMAT_TIME:
317 cur = gst_util_uint64_scale_int (cur, GST_SECOND, rate);
318 gst_query_set_position (query, GST_FORMAT_TIME, cur);
321 case GST_FORMAT_DEFAULT:
322 gst_query_set_position (query, GST_FORMAT_DEFAULT, cur);
326 GST_DEBUG_OBJECT (parse, "cannot handle position query in "
327 "%s format. Forwarding upstream.", gst_format_get_name (format));
328 ret = gst_pad_query_default (pad, query);
333 case GST_QUERY_DURATION:{
338 GST_OBJECT_LOCK (parse);
339 rate = parse->samplerate;
340 len = parse->total_samples;
341 GST_OBJECT_UNLOCK (parse);
344 GST_DEBUG_OBJECT (parse, "haven't read header yet");
348 gst_query_parse_duration (query, &format, NULL);
351 case GST_FORMAT_TIME:
352 if (len != G_GINT64_CONSTANT (-1))
353 len = gst_util_uint64_scale_int (len, GST_SECOND, rate);
354 gst_query_set_duration (query, GST_FORMAT_TIME, len);
357 case GST_FORMAT_DEFAULT:
358 gst_query_set_duration (query, GST_FORMAT_DEFAULT, len);
362 GST_DEBUG_OBJECT (parse, "cannot handle duration query in "
363 "%s format. Forwarding upstream.", gst_format_get_name (format));
364 ret = gst_pad_query_default (pad, query);
369 case GST_QUERY_SEEKING:{
370 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
371 if (format == GST_FORMAT_TIME || format == GST_FORMAT_DEFAULT) {
374 gint64 duration = -1;
376 /* only fails if we didn't read the headers yet and can't say
377 * anything about our seeking capabilities */
378 if (!gst_pad_query_duration (pad, &format, &duration))
381 /* can't seek in streaming mode yet */
382 GST_OBJECT_LOCK (parse);
383 seekable = (parse->adapter == NULL);
384 GST_OBJECT_UNLOCK (parse);
386 gst_query_set_seeking (query, format, seekable, 0, duration);
392 ret = gst_pad_query_default (pad, query);
397 gst_object_unref (parse);
402 /* returns TRUE on success, with byte_offset set to the offset of the
403 * wavpack chunk containing the sample requested. start_sample will be
404 * set to the first sample in the chunk starting at byte_offset.
405 * Scanning from the last known header offset to the wanted position
406 * when seeking forward isn't very clever, but seems fast enough in
407 * practice and has the nice side effect of populating our index
410 gst_wavpack_parse_scan_to_find_sample (GstWavpackParse * parse,
411 gint64 sample, gint64 * byte_offset, gint64 * start_sample)
413 GstWavpackParseIndexEntry *entry;
419 /* first, check if we have to scan at all */
420 entry = gst_wavpack_parse_index_get_entry_from_sample (parse, sample);
422 *byte_offset = entry->byte_offset;
423 *start_sample = entry->sample_offset;
424 GST_LOG_OBJECT (parse, "Found index entry: sample %" G_GINT64_FORMAT
425 " @ offset %" G_GINT64_FORMAT, entry->sample_offset,
430 GST_LOG_OBJECT (parse, "No matching entry in index, scanning file ...");
432 /* if we have an index, we can start scanning from the last known offset
433 * in there, after all we know our wanted sample is not in the index */
434 if (parse->entries) {
435 GstWavpackParseIndexEntry *entry;
437 entry = gst_wavpack_parse_index_get_last_entry (parse);
438 off = entry->byte_offset;
441 /* now scan forward until we find the chunk we're looking for or hit EOS */
443 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;
485 gint64 stop_time = -1;
487 gint64 start_time = 0;
493 /* segment is in DEFAULT format, but we want to send a TIME newsegment */
494 start_time = gst_util_uint64_scale_int (s->start, GST_SECOND,
495 wvparse->samplerate);
498 stop_time = gst_util_uint64_scale_int (s->stop, GST_SECOND,
499 wvparse->samplerate);
502 GST_DEBUG_OBJECT (wvparse, "sending newsegment from %" GST_TIME_FORMAT
503 " to %" GST_TIME_FORMAT, GST_TIME_ARGS (start_time),
504 GST_TIME_ARGS (stop_time));
506 /* after a seek, s->last_stop will point to a chunk boundary, ie. from
507 * which sample we will start sending data again, while s->start will
508 * point to the sample we actually want to seek to and want to start
509 * playing right after the seek. Adjust clock-time for the difference
510 * so we start playing from start_time */
511 cur_pos_time = gst_util_uint64_scale_int (s->last_stop, GST_SECOND,
512 wvparse->samplerate);
513 diff = start_time - cur_pos_time;
515 ret = gst_pad_push_event (wvparse->srcpad,
516 gst_event_new_new_segment (update, s->rate, GST_FORMAT_TIME,
517 start_time, stop_time, start_time - diff));
523 gst_wavpack_parse_handle_seek_event (GstWavpackParse * wvparse,
526 GstSeekFlags seek_flags;
528 GstSeekType start_type;
530 GstSeekType stop_type;
536 gboolean only_update;
544 gint64 start; /* sample we want to seek to */
546 gint64 byte_offset; /* byte offset the chunk we seek to starts at */
548 gint64 chunk_start; /* first sample in chunk we seek to */
554 if (wvparse->adapter) {
555 GST_DEBUG_OBJECT (wvparse, "seeking in streaming mode not implemented yet");
559 gst_event_parse_seek (event, &speed, &format, &seek_flags, &start_type,
560 &start, &stop_type, &stop);
562 if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME) {
563 GST_DEBUG ("seeking is only supported in TIME or DEFAULT format");
568 GST_DEBUG ("only forward playback supported, rate %f not allowed", speed);
572 GST_OBJECT_LOCK (wvparse);
574 rate = wvparse->samplerate;
576 GST_OBJECT_UNLOCK (wvparse);
577 GST_DEBUG ("haven't read header yet");
581 /* figure out the last position we need to play. If it's configured (stop !=
582 * -1), use that, else we play until the total duration of the file */
584 stop = wvparse->segment.duration;
586 /* convert from time to samples if necessary */
587 if (format == GST_FORMAT_TIME) {
588 if (start_type != GST_SEEK_TYPE_NONE)
589 start = gst_util_uint64_scale_int (start, rate, GST_SECOND);
590 if (stop_type != GST_SEEK_TYPE_NONE)
591 stop = gst_util_uint64_scale_int (stop, rate, GST_SECOND);
595 GST_OBJECT_UNLOCK (wvparse);
596 GST_DEBUG_OBJECT (wvparse, "Invalid start sample %" G_GINT64_FORMAT, start);
600 flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
602 /* operate on segment copy until we know the seek worked */
603 segment = wvparse->segment;
605 gst_segment_set_seek (&segment, speed, GST_FORMAT_DEFAULT,
606 seek_flags, start_type, start, stop_type, stop, &only_update);
610 wvparse->segment = segment;
611 gst_wavpack_parse_send_newsegment (wvparse, TRUE);
616 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_start ());
619 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_start ());
621 gst_pad_pause_task (wvparse->sinkpad);
624 GST_PAD_STREAM_LOCK (wvparse->sinkpad);
626 /* Save current position */
627 last_stop = wvparse->segment.last_stop;
629 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_stop ());
632 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_stop ());
635 GST_DEBUG_OBJECT (wvparse, "Performing seek to %" GST_TIME_FORMAT " sample %"
636 G_GINT64_FORMAT, GST_TIME_ARGS (segment.start * GST_SECOND / rate),
639 ret = gst_wavpack_parse_scan_to_find_sample (wvparse, segment.start,
640 &byte_offset, &chunk_start);
643 GST_DEBUG_OBJECT (wvparse, "new offset: %" G_GINT64_FORMAT, byte_offset);
644 wvparse->current_offset = byte_offset;
645 /* we want to send a newsegment event with the actual seek position
646 * as start, even though our first buffer might start before the
647 * configured segment. We leave it up to the decoder or sink to crop
648 * the output buffers accordingly */
649 wvparse->segment = segment;
650 wvparse->segment.last_stop = chunk_start;
651 wvparse->need_newsegment = TRUE;
652 wvparse->discont = (last_stop != chunk_start) ? TRUE : FALSE;
654 /* if we're doing a segment seek, post a SEGMENT_START message */
655 if (wvparse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
656 gst_element_post_message (GST_ELEMENT_CAST (wvparse),
657 gst_message_new_segment_start (GST_OBJECT_CAST (wvparse),
658 wvparse->segment.format, wvparse->segment.last_stop));
661 GST_DEBUG_OBJECT (wvparse, "seek failed: don't know where to seek to");
664 GST_PAD_STREAM_UNLOCK (wvparse->sinkpad);
665 GST_OBJECT_UNLOCK (wvparse);
667 gst_pad_start_task (wvparse->sinkpad,
668 (GstTaskFunction) gst_wavpack_parse_loop, wvparse);
674 gst_wavpack_parse_sink_event (GstPad * pad, GstEvent * event)
676 GstWavpackParse *parse;
680 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
682 switch (GST_EVENT_TYPE (event)) {
683 case GST_EVENT_FLUSH_STOP:{
684 if (parse->adapter) {
685 gst_adapter_clear (parse->adapter);
687 if (parse->pending_buffer) {
688 gst_buffer_unref (parse->pending_buffer);
689 parse->pending_buffer = NULL;
690 parse->pending_offset = 0;
692 ret = gst_pad_push_event (parse->srcpad, event);
695 case GST_EVENT_NEWSEGMENT:{
696 parse->need_newsegment = TRUE;
697 gst_event_unref (event);
702 if (parse->adapter) {
703 /* remove all bytes that are left in the adapter after EOS. They can't
704 * be a complete Wavpack block and we can't do anything with them */
705 gst_adapter_clear (parse->adapter);
707 if (parse->pending_buffer) {
708 gst_buffer_unref (parse->pending_buffer);
709 parse->pending_buffer = NULL;
710 parse->pending_offset = 0;
712 ret = gst_pad_push_event (parse->srcpad, event);
716 /* stream lock is recursive, should be fine for all events */
717 GST_PAD_STREAM_LOCK (pad);
718 if (parse->srcpad == NULL) {
719 parse->queued_events = g_list_append (parse->queued_events, event);
721 ret = gst_pad_push_event (parse->srcpad, event);
723 GST_PAD_STREAM_UNLOCK (pad);
728 gst_object_unref (parse);
733 gst_wavpack_parse_src_event (GstPad * pad, GstEvent * event)
735 GstWavpackParse *parse;
739 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
741 switch (GST_EVENT_TYPE (event)) {
743 ret = gst_wavpack_parse_handle_seek_event (parse, event);
746 ret = gst_pad_event_default (pad, event);
750 gst_object_unref (parse);
755 gst_wavpack_parse_init (GstWavpackParse * parse, GstWavpackParseClass * gclass)
757 GstElementClass *klass = GST_ELEMENT_GET_CLASS (parse);
759 GstPadTemplate *tmpl;
761 tmpl = gst_element_class_get_pad_template (klass, "sink");
762 parse->sinkpad = gst_pad_new_from_template (tmpl, "sink");
764 gst_pad_set_activate_function (parse->sinkpad,
765 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate));
766 gst_pad_set_activatepull_function (parse->sinkpad,
767 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate_pull));
768 gst_pad_set_event_function (parse->sinkpad,
769 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_event));
770 gst_pad_set_chain_function (parse->sinkpad,
771 GST_DEBUG_FUNCPTR (gst_wavpack_parse_chain));
773 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
775 parse->srcpad = NULL;
776 gst_wavpack_parse_reset (parse);
780 gst_wavpack_parse_get_upstream_length (GstWavpackParse * parse)
784 GstFormat format = GST_FORMAT_BYTES;
786 if (!gst_pad_query_peer_duration (parse->sinkpad, &format, &length)) {
789 GST_DEBUG ("upstream length: %" G_GINT64_FORMAT, length);
795 gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse, gint64 offset,
796 guint size, GstFlowReturn * flow)
798 GstFlowReturn flow_ret;
800 GstBuffer *buf = NULL;
802 if (offset + size > wvparse->upstream_length) {
803 wvparse->upstream_length = gst_wavpack_parse_get_upstream_length (wvparse);
804 if (offset + size > wvparse->upstream_length) {
805 GST_DEBUG_OBJECT (wvparse, "EOS: %" G_GINT64_FORMAT " + %u > %"
806 G_GINT64_FORMAT, offset, size, wvparse->upstream_length);
807 flow_ret = GST_FLOW_UNEXPECTED;
812 flow_ret = gst_pad_pull_range (wvparse->sinkpad, offset, size, &buf);
814 if (flow_ret != GST_FLOW_OK) {
815 GST_DEBUG_OBJECT (wvparse, "pull_range (%" G_GINT64_FORMAT ", %u) "
816 "failed, flow: %s", offset, size, gst_flow_get_name (flow_ret));
821 if (GST_BUFFER_SIZE (buf) < size) {
822 GST_DEBUG_OBJECT (wvparse, "Short read at offset %" G_GINT64_FORMAT
823 ", got only %u of %u bytes", offset, GST_BUFFER_SIZE (buf), size);
824 gst_buffer_unref (buf);
826 flow_ret = GST_FLOW_UNEXPECTED;
836 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
837 WavpackHeader * header)
839 GstWavpackMetadata meta;
841 GstCaps *caps = NULL;
845 g_assert (wvparse->srcpad == NULL);
847 bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
849 while (gst_wavpack_read_metadata (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
851 case ID_WVC_BITSTREAM:{
852 caps = gst_caps_new_simple ("audio/x-wavpack-correction",
853 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
855 gst_pad_new_from_template (gst_element_class_get_pad_template
856 (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
859 case ID_WV_BITSTREAM:
860 case ID_WVX_BITSTREAM:{
861 WavpackStreamReader *stream_reader = gst_wavpack_stream_reader_new ();
871 rid.buffer = GST_BUFFER_DATA (buf);
872 rid.length = GST_BUFFER_SIZE (buf);
876 WavpackOpenFileInputEx (stream_reader, &rid, NULL, error_msg, 0, 0);
881 wvparse->samplerate = WavpackGetSampleRate (wpc);
882 wvparse->channels = WavpackGetNumChannels (wpc);
883 wvparse->total_samples =
884 (header->total_samples ==
885 0xffffffff) ? G_GINT64_CONSTANT (-1) : header->total_samples;
887 caps = gst_caps_new_simple ("audio/x-wavpack",
888 "width", G_TYPE_INT, WavpackGetBitsPerSample (wpc),
889 "channels", G_TYPE_INT, wvparse->channels,
890 "rate", G_TYPE_INT, wvparse->samplerate,
891 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
892 #ifdef WAVPACK_OLD_API
893 channel_mask = wpc->config.channel_mask;
895 channel_mask = WavpackGetChannelMask (wpc);
897 if (channel_mask == 0)
899 gst_wavpack_get_default_channel_mask (wvparse->channels);
901 if (channel_mask != 0) {
902 if (!gst_wavpack_set_channel_layout (caps, channel_mask)) {
903 GST_WARNING_OBJECT (wvparse, "Failed to set channel layout");
904 gst_caps_unref (caps);
906 WavpackCloseFile (wpc);
907 g_free (stream_reader);
913 gst_pad_new_from_template (gst_element_class_get_pad_template
914 (GST_ELEMENT_GET_CLASS (wvparse), "src"), "src");
915 WavpackCloseFile (wpc);
916 g_free (stream_reader);
920 GST_LOG_OBJECT (wvparse, "unhandled ID: 0x%02x", meta.id);
928 if (caps == NULL || wvparse->srcpad == NULL)
931 GST_DEBUG_OBJECT (wvparse, "Added src pad with caps %" GST_PTR_FORMAT, caps);
933 gst_pad_set_query_function (wvparse->srcpad,
934 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_query));
935 gst_pad_set_query_type_function (wvparse->srcpad,
936 GST_DEBUG_FUNCPTR (gst_wavpack_parse_get_src_query_types));
937 gst_pad_set_event_function (wvparse->srcpad,
938 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_event));
940 gst_pad_set_caps (wvparse->srcpad, caps);
941 gst_caps_unref (caps);
942 gst_pad_use_fixed_caps (wvparse->srcpad);
944 gst_object_ref (wvparse->srcpad);
945 gst_pad_set_active (wvparse->srcpad, TRUE);
946 gst_element_add_pad (GST_ELEMENT (wvparse), wvparse->srcpad);
947 gst_element_no_more_pads (GST_ELEMENT (wvparse));
953 gst_wavpack_parse_push_buffer (GstWavpackParse * wvparse, GstBuffer * buf,
954 WavpackHeader * header)
957 wvparse->current_offset += header->ckSize + 8;
959 wvparse->segment.last_stop = header->block_index;
961 if (wvparse->need_newsegment) {
962 if (gst_wavpack_parse_send_newsegment (wvparse, FALSE))
963 wvparse->need_newsegment = FALSE;
966 /* send any queued events */
967 if (wvparse->queued_events) {
970 for (l = wvparse->queued_events; l != NULL; l = l->next) {
971 gst_pad_push_event (wvparse->srcpad, GST_EVENT (l->data));
973 g_list_free (wvparse->queued_events);
974 wvparse->queued_events = NULL;
977 if (wvparse->pending_buffer == NULL) {
978 wvparse->pending_buffer = buf;
979 wvparse->pending_offset = header->block_index;
980 } else if (wvparse->pending_offset == header->block_index) {
981 wvparse->pending_buffer = gst_buffer_join (wvparse->pending_buffer, buf);
983 GST_ERROR ("Got incomplete block, dropping");
984 gst_buffer_unref (wvparse->pending_buffer);
985 wvparse->pending_buffer = buf;
986 wvparse->pending_offset = header->block_index;
989 if (!(header->flags & FINAL_BLOCK))
992 buf = wvparse->pending_buffer;
993 wvparse->pending_buffer = NULL;
995 GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (header->block_index,
996 GST_SECOND, wvparse->samplerate);
997 GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (header->block_samples,
998 GST_SECOND, wvparse->samplerate);
999 GST_BUFFER_OFFSET (buf) = header->block_index;
1000 GST_BUFFER_OFFSET_END (buf) = header->block_index + header->block_samples;
1002 if (wvparse->discont || wvparse->next_block_index != header->block_index) {
1003 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1004 wvparse->discont = FALSE;
1007 wvparse->next_block_index = header->block_index + header->block_samples;
1009 gst_buffer_set_caps (buf, GST_PAD_CAPS (wvparse->srcpad));
1011 GST_LOG_OBJECT (wvparse, "Pushing buffer with time %" GST_TIME_FORMAT,
1012 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1014 ret = gst_pad_push (wvparse->srcpad, buf);
1016 wvparse->segment.last_stop = wvparse->next_block_index;
1022 gst_wavpack_parse_find_marker (guint8 * buf, guint size)
1028 if (G_UNLIKELY (size < 4))
1031 for (i = 0; i < size - 4; i++) {
1032 if (memcmp (buf + i, "wvpk", 4) == 0) {
1040 static GstFlowReturn
1041 gst_wavpack_parse_resync_loop (GstWavpackParse * parse, WavpackHeader * header)
1043 GstFlowReturn flow_ret = GST_FLOW_UNEXPECTED;
1045 GstBuffer *buf = NULL;
1047 /* loop until we have a frame header or reach the end of the stream */
1049 guint8 *data, *marker;
1054 gst_buffer_unref (buf);
1058 if (parse->upstream_length == 0 ||
1059 parse->upstream_length <= parse->current_offset) {
1060 parse->upstream_length = gst_wavpack_parse_get_upstream_length (parse);
1061 if (parse->upstream_length == 0 ||
1062 parse->upstream_length <= parse->current_offset) {
1067 len = MIN (parse->upstream_length - parse->current_offset, 2048);
1069 GST_LOG_OBJECT (parse, "offset: %" G_GINT64_FORMAT, parse->current_offset);
1071 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1074 /* whatever the problem is, there's nothing more for us to do for now */
1075 if (flow_ret != GST_FLOW_OK)
1078 data = GST_BUFFER_DATA (buf);
1079 size = GST_BUFFER_SIZE (buf);
1081 /* not enough data for a header? */
1082 if (size < sizeof (WavpackHeader))
1085 /* got a header right where we are at now? */
1086 if (gst_wavpack_read_header (header, data))
1089 /* nope, let's see if we can find one */
1090 marker = gst_wavpack_parse_find_marker (data + 1, size - 1);
1093 parse->current_offset += marker - data;
1094 /* do one more loop iteration to make sure we pull enough
1095 * data for a full header, we'll bail out then */
1097 parse->current_offset += len - 4;
1102 gst_buffer_unref (buf);
1108 gst_wavpack_parse_loop (GstElement * element)
1110 GstWavpackParse *parse = GST_WAVPACK_PARSE (element);
1112 GstFlowReturn flow_ret;
1113 WavpackHeader header = { {0,}, 0, };
1114 GstBuffer *buf = NULL;
1116 flow_ret = gst_wavpack_parse_resync_loop (parse, &header);
1118 if (flow_ret != GST_FLOW_OK)
1121 GST_LOG_OBJECT (parse, "Read header at offset %" G_GINT64_FORMAT
1122 ": chunk size = %u+8", parse->current_offset, header.ckSize);
1124 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1125 header.ckSize + 8, &flow_ret);
1127 if (flow_ret != GST_FLOW_OK)
1130 if (parse->srcpad == NULL) {
1131 if (!gst_wavpack_parse_create_src_pad (parse, buf, &header)) {
1132 GST_ERROR_OBJECT (parse, "Failed to create src pad");
1133 flow_ret = GST_FLOW_ERROR;
1137 if (header.flags & INITIAL_BLOCK)
1138 gst_wavpack_parse_index_append_entry (parse, parse->current_offset,
1139 header.block_index, header.block_samples);
1141 flow_ret = gst_wavpack_parse_push_buffer (parse, buf, &header);
1142 if (flow_ret != GST_FLOW_OK)
1149 const gchar *reason = gst_flow_get_name (flow_ret);
1151 GST_LOG_OBJECT (parse, "pausing task, reason %s", reason);
1152 gst_pad_pause_task (parse->sinkpad);
1154 if (flow_ret == GST_FLOW_UNEXPECTED && parse->srcpad) {
1155 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1158 GST_LOG_OBJECT (parse, "Sending segment done");
1160 if ((stop = parse->segment.stop) == -1)
1161 stop = parse->segment.duration;
1163 gst_element_post_message (GST_ELEMENT_CAST (parse),
1164 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
1165 parse->segment.format, stop));
1167 GST_LOG_OBJECT (parse, "Sending EOS, at end of stream");
1168 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1170 } else if (flow_ret == GST_FLOW_NOT_LINKED
1171 || flow_ret < GST_FLOW_UNEXPECTED) {
1172 GST_ELEMENT_ERROR (parse, STREAM, FAILED,
1173 (_("Internal data stream error.")), ("stream stopped, reason %s",
1176 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1183 gst_wavpack_parse_resync_adapter (GstAdapter * adapter)
1185 const guint8 *buf, *marker;
1187 guint avail = gst_adapter_available (adapter);
1192 /* if the marker is at the beginning don't do the expensive search */
1193 buf = gst_adapter_peek (adapter, 4);
1194 if (memcmp (buf, "wvpk", 4) == 0)
1200 /* search for the marker in the complete content of the adapter */
1201 buf = gst_adapter_peek (adapter, avail);
1202 if (buf && (marker = gst_wavpack_parse_find_marker ((guint8 *) buf, avail))) {
1203 gst_adapter_flush (adapter, marker - buf);
1207 /* flush everything except the last 4 bytes. they could contain
1208 * the start of a new marker */
1209 gst_adapter_flush (adapter, avail - 4);
1214 static GstFlowReturn
1215 gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf)
1217 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (GST_PAD_PARENT (pad));
1219 GstFlowReturn ret = GST_FLOW_OK;
1223 const guint8 *tmp_buf;
1225 if (!wvparse->adapter) {
1226 wvparse->adapter = gst_adapter_new ();
1229 if (GST_BUFFER_IS_DISCONT (buf)) {
1230 gst_adapter_clear (wvparse->adapter);
1231 wvparse->discont = TRUE;
1234 gst_adapter_push (wvparse->adapter, buf);
1236 if (gst_adapter_available (wvparse->adapter) < sizeof (WavpackHeader))
1239 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1242 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1243 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1245 while (gst_adapter_available (wvparse->adapter) >= wph.ckSize + 4 * 1 + 4) {
1247 gst_adapter_take_buffer (wvparse->adapter, wph.ckSize + 4 * 1 + 4);
1250 return GST_FLOW_ERROR;
1252 if (wvparse->srcpad == NULL) {
1253 if (!gst_wavpack_parse_create_src_pad (wvparse, outbuf, &wph)) {
1254 GST_ERROR_OBJECT (wvparse, "Failed to create src pad");
1255 ret = GST_FLOW_ERROR;
1260 ret = gst_wavpack_parse_push_buffer (wvparse, outbuf, &wph);
1262 if (ret != GST_FLOW_OK)
1265 if (gst_adapter_available (wvparse->adapter) >= sizeof (WavpackHeader)) {
1266 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1268 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1271 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1278 static GstStateChangeReturn
1279 gst_wavpack_parse_change_state (GstElement * element, GstStateChange transition)
1281 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (element);
1283 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1285 switch (transition) {
1286 case GST_STATE_CHANGE_READY_TO_PAUSED:
1287 gst_segment_init (&wvparse->segment, GST_FORMAT_DEFAULT);
1288 wvparse->segment.last_stop = 0;
1293 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1294 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1296 switch (transition) {
1297 case GST_STATE_CHANGE_PAUSED_TO_READY:
1298 gst_wavpack_parse_reset (wvparse);
1308 gst_wavpack_parse_sink_activate (GstPad * sinkpad)
1310 if (gst_pad_check_pull_range (sinkpad)) {
1311 return gst_pad_activate_pull (sinkpad, TRUE);
1313 return gst_pad_activate_push (sinkpad, TRUE);
1318 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
1323 result = gst_pad_start_task (sinkpad,
1324 (GstTaskFunction) gst_wavpack_parse_loop, GST_PAD_PARENT (sinkpad));
1326 result = gst_pad_stop_task (sinkpad);
1333 gst_wavpack_parse_plugin_init (GstPlugin * plugin)
1335 if (!gst_element_register (plugin, "wavpackparse",
1336 GST_RANK_PRIMARY, GST_TYPE_WAVPACK_PARSE)) {
1340 GST_DEBUG_CATEGORY_INIT (gst_wavpack_parse_debug, "wavpack_parse", 0,
1341 "Wavpack file parser");