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 #include <gst/gst-i18n-plugin.h>
50 #include <wavpack/wavpack.h>
51 #include "gstwavpackparse.h"
52 #include "gstwavpackstreamreader.h"
53 #include "gstwavpackcommon.h"
55 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_parse_debug);
56 #define GST_CAT_DEFAULT gst_wavpack_parse_debug
58 static inline GstWavpackParseIndexEntry *
59 gst_wavpack_parse_index_entry_new (void)
61 return g_slice_new (GstWavpackParseIndexEntry);
65 gst_wavpack_parse_index_entry_free (GstWavpackParseIndexEntry * entry)
67 g_slice_free (GstWavpackParseIndexEntry, entry);
70 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS ("audio/x-wavpack, "
74 "framed = (boolean) false; "
75 "audio/x-wavpack-correction, " "framed = (boolean) false")
78 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
81 GST_STATIC_CAPS ("audio/x-wavpack, "
82 "width = (int) [ 1, 32 ], "
83 "channels = (int) [ 1, 8 ], "
84 "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
87 static GstStaticPadTemplate wvc_src_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
90 GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) true")
93 static gboolean gst_wavpack_parse_sink_activate (GstPad * sinkpad);
96 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active);
98 static void gst_wavpack_parse_loop (GstElement * element);
100 static GstStateChangeReturn gst_wavpack_parse_change_state (GstElement *
101 element, GstStateChange transition);
102 static void gst_wavpack_parse_reset (GstWavpackParse * parse);
104 static gint64 gst_wavpack_parse_get_upstream_length (GstWavpackParse * wvparse);
106 static GstBuffer *gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse,
107 gint64 offset, guint size, GstFlowReturn * flow);
108 static GstFlowReturn gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf);
110 GST_BOILERPLATE (GstWavpackParse, gst_wavpack_parse, GstElement,
114 gst_wavpack_parse_base_init (gpointer klass)
116 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
118 gst_element_class_add_static_pad_template (element_class, &src_factory);
119 gst_element_class_add_static_pad_template (element_class,
121 gst_element_class_add_static_pad_template (element_class, &sink_factory);
123 gst_element_class_set_details_simple (element_class, "Wavpack parser",
124 "Codec/Demuxer/Audio",
125 "Parses Wavpack files",
126 "Arwed v. Merkatz <v.merkatz@gmx.net>, "
127 "Sebastian Dröge <slomo@circular-chaos.org>");
131 gst_wavpack_parse_finalize (GObject * object)
133 gst_wavpack_parse_reset (GST_WAVPACK_PARSE (object));
135 G_OBJECT_CLASS (parent_class)->finalize (object);
139 gst_wavpack_parse_class_init (GstWavpackParseClass * klass)
141 GObjectClass *gobject_class;
143 GstElementClass *gstelement_class;
145 gobject_class = (GObjectClass *) klass;
146 gstelement_class = (GstElementClass *) klass;
148 gobject_class->finalize = gst_wavpack_parse_finalize;
149 gstelement_class->change_state =
150 GST_DEBUG_FUNCPTR (gst_wavpack_parse_change_state);
153 static GstWavpackParseIndexEntry *
154 gst_wavpack_parse_index_get_last_entry (GstWavpackParse * wvparse)
156 g_assert (wvparse->entries != NULL);
158 return wvparse->entries->data;
161 static GstWavpackParseIndexEntry *
162 gst_wavpack_parse_index_get_entry_from_sample (GstWavpackParse * wvparse,
163 gint64 sample_offset)
169 if (wvparse->entries == NULL)
172 for (node = wvparse->entries, i = 0; node; node = node->next, i++) {
173 GstWavpackParseIndexEntry *entry;
177 GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
178 " byte %" G_GINT64_FORMAT, i, entry->sample_offset, entry->byte_offset);
180 if (entry->sample_offset <= sample_offset &&
181 sample_offset < entry->sample_offset_end) {
182 GST_LOG_OBJECT (wvparse, "found match");
186 /* as the list is sorted and we first look at the latest entry
187 * we can abort searching for an entry if the sample we want is
188 * after the latest one */
189 if (sample_offset >= entry->sample_offset_end)
192 GST_LOG_OBJECT (wvparse, "no match in index");
197 gst_wavpack_parse_index_append_entry (GstWavpackParse * wvparse,
198 gint64 byte_offset, gint64 sample_offset, gint64 num_samples)
200 GstWavpackParseIndexEntry *entry;
202 /* do we have this one already? */
203 if (wvparse->entries) {
204 entry = gst_wavpack_parse_index_get_last_entry (wvparse);
205 if (entry->byte_offset >= byte_offset
206 || entry->sample_offset >= sample_offset)
210 GST_LOG_OBJECT (wvparse, "Adding index entry %8" G_GINT64_FORMAT " - %"
211 GST_TIME_FORMAT " @ offset 0x%08" G_GINT64_MODIFIER "x", sample_offset,
212 GST_TIME_ARGS (gst_util_uint64_scale_int (sample_offset,
213 GST_SECOND, wvparse->samplerate)), byte_offset);
215 entry = gst_wavpack_parse_index_entry_new ();
216 entry->byte_offset = byte_offset;
217 entry->sample_offset = sample_offset;
218 entry->sample_offset_end = sample_offset + num_samples;
219 wvparse->entries = g_slist_prepend (wvparse->entries, entry);
223 gst_wavpack_parse_reset (GstWavpackParse * parse)
225 parse->total_samples = G_GINT64_CONSTANT (-1);
226 parse->samplerate = 0;
229 gst_segment_init (&parse->segment, GST_FORMAT_UNDEFINED);
230 parse->next_block_index = 0;
232 parse->current_offset = 0;
233 parse->need_newsegment = TRUE;
234 parse->discont = TRUE;
235 parse->upstream_length = -1;
237 if (parse->entries) {
238 g_slist_foreach (parse->entries, (GFunc) gst_wavpack_parse_index_entry_free,
240 g_slist_free (parse->entries);
241 parse->entries = NULL;
244 if (parse->adapter) {
245 gst_adapter_clear (parse->adapter);
246 g_object_unref (parse->adapter);
247 parse->adapter = NULL;
250 if (parse->srcpad != NULL) {
253 GST_DEBUG_OBJECT (parse, "Removing src pad");
254 res = gst_element_remove_pad (GST_ELEMENT (parse), parse->srcpad);
255 g_return_if_fail (res != FALSE);
256 gst_object_unref (parse->srcpad);
257 parse->srcpad = NULL;
260 g_list_foreach (parse->queued_events, (GFunc) gst_mini_object_unref, NULL);
261 g_list_free (parse->queued_events);
262 parse->queued_events = NULL;
264 if (parse->pending_buffer)
265 gst_buffer_unref (parse->pending_buffer);
267 parse->pending_buffer = NULL;
270 static const GstQueryType *
271 gst_wavpack_parse_get_src_query_types (GstPad * pad)
273 static const GstQueryType types[] = {
284 gst_wavpack_parse_src_query (GstPad * pad, GstQuery * query)
286 GstWavpackParse *parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
290 gboolean ret = FALSE;
292 switch (GST_QUERY_TYPE (query)) {
293 case GST_QUERY_POSITION:{
298 GST_OBJECT_LOCK (parse);
299 cur = parse->segment.last_stop;
300 rate = parse->samplerate;
301 GST_OBJECT_UNLOCK (parse);
304 GST_DEBUG_OBJECT (parse, "haven't read header yet");
308 gst_query_parse_position (query, &format, NULL);
311 case GST_FORMAT_TIME:
312 cur = gst_util_uint64_scale_int (cur, GST_SECOND, rate);
313 gst_query_set_position (query, GST_FORMAT_TIME, cur);
316 case GST_FORMAT_DEFAULT:
317 gst_query_set_position (query, GST_FORMAT_DEFAULT, cur);
321 GST_DEBUG_OBJECT (parse, "cannot handle position query in "
322 "%s format. Forwarding upstream.", gst_format_get_name (format));
323 ret = gst_pad_query_default (pad, query);
328 case GST_QUERY_DURATION:{
333 GST_OBJECT_LOCK (parse);
334 rate = parse->samplerate;
335 len = parse->total_samples;
336 GST_OBJECT_UNLOCK (parse);
339 GST_DEBUG_OBJECT (parse, "haven't read header yet");
343 gst_query_parse_duration (query, &format, NULL);
346 case GST_FORMAT_TIME:
347 if (len != G_GINT64_CONSTANT (-1))
348 len = gst_util_uint64_scale_int (len, GST_SECOND, rate);
349 gst_query_set_duration (query, GST_FORMAT_TIME, len);
352 case GST_FORMAT_DEFAULT:
353 gst_query_set_duration (query, GST_FORMAT_DEFAULT, len);
357 GST_DEBUG_OBJECT (parse, "cannot handle duration query in "
358 "%s format. Forwarding upstream.", gst_format_get_name (format));
359 ret = gst_pad_query_default (pad, query);
364 case GST_QUERY_SEEKING:{
365 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
366 if (format == GST_FORMAT_TIME || format == GST_FORMAT_DEFAULT) {
369 gint64 duration = -1;
371 /* only fails if we didn't read the headers yet and can't say
372 * anything about our seeking capabilities */
373 if (!gst_pad_query_duration (pad, &format, &duration))
376 /* can't seek in streaming mode yet */
377 GST_OBJECT_LOCK (parse);
378 seekable = (parse->adapter == NULL);
379 GST_OBJECT_UNLOCK (parse);
381 gst_query_set_seeking (query, format, seekable, 0, duration);
387 ret = gst_pad_query_default (pad, query);
392 gst_object_unref (parse);
397 /* returns TRUE on success, with byte_offset set to the offset of the
398 * wavpack chunk containing the sample requested. start_sample will be
399 * set to the first sample in the chunk starting at byte_offset.
400 * Scanning from the last known header offset to the wanted position
401 * when seeking forward isn't very clever, but seems fast enough in
402 * practice and has the nice side effect of populating our index
405 gst_wavpack_parse_scan_to_find_sample (GstWavpackParse * parse,
406 gint64 sample, gint64 * byte_offset, gint64 * start_sample)
408 GstWavpackParseIndexEntry *entry;
414 /* first, check if we have to scan at all */
415 entry = gst_wavpack_parse_index_get_entry_from_sample (parse, sample);
417 *byte_offset = entry->byte_offset;
418 *start_sample = entry->sample_offset;
419 GST_LOG_OBJECT (parse, "Found index entry: sample %" G_GINT64_FORMAT
420 " @ offset %" G_GINT64_FORMAT, entry->sample_offset,
425 GST_LOG_OBJECT (parse, "No matching entry in index, scanning file ...");
427 /* if we have an index, we can start scanning from the last known offset
428 * in there, after all we know our wanted sample is not in the index */
429 if (parse->entries) {
430 GstWavpackParseIndexEntry *entry;
432 entry = gst_wavpack_parse_index_get_last_entry (parse);
433 off = entry->byte_offset;
436 /* now scan forward until we find the chunk we're looking for or hit EOS */
438 WavpackHeader header;
442 buf = gst_wavpack_parse_pull_buffer (parse, off, sizeof (WavpackHeader),
448 gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
449 gst_buffer_unref (buf);
451 if (header.flags & INITIAL_BLOCK)
452 gst_wavpack_parse_index_append_entry (parse, off, header.block_index,
453 header.block_samples);
457 if (header.block_index <= sample &&
458 sample < (header.block_index + header.block_samples)) {
460 *start_sample = header.block_index;
464 off += header.ckSize + 8;
467 GST_DEBUG_OBJECT (parse, "scan failed: %s (off=0x%08" G_GINT64_MODIFIER "x)",
468 gst_flow_get_name (ret), off);
474 gst_wavpack_parse_send_newsegment (GstWavpackParse * wvparse, gboolean update)
476 GstSegment *s = &wvparse->segment;
480 gint64 stop_time = -1;
482 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;
523 GstSeekType start_type;
525 GstSeekType stop_type;
531 gboolean only_update;
539 gint64 start; /* sample we want to seek to */
541 gint64 byte_offset; /* byte offset the chunk we seek to starts at */
543 gint64 chunk_start; /* first sample in chunk we seek to */
549 if (wvparse->adapter) {
550 GST_DEBUG_OBJECT (wvparse, "seeking in streaming mode not implemented yet");
554 gst_event_parse_seek (event, &speed, &format, &seek_flags, &start_type,
555 &start, &stop_type, &stop);
557 if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME) {
558 GST_DEBUG ("seeking is only supported in TIME or DEFAULT format");
563 GST_DEBUG ("only forward playback supported, rate %f not allowed", speed);
567 GST_OBJECT_LOCK (wvparse);
569 rate = wvparse->samplerate;
571 GST_OBJECT_UNLOCK (wvparse);
572 GST_DEBUG ("haven't read header yet");
576 /* figure out the last position we need to play. If it's configured (stop !=
577 * -1), use that, else we play until the total duration of the file */
579 stop = wvparse->segment.duration;
581 /* convert from time to samples if necessary */
582 if (format == GST_FORMAT_TIME) {
583 if (start_type != GST_SEEK_TYPE_NONE)
584 start = gst_util_uint64_scale_int (start, rate, GST_SECOND);
585 if (stop_type != GST_SEEK_TYPE_NONE)
586 stop = gst_util_uint64_scale_int (stop, rate, GST_SECOND);
590 GST_OBJECT_UNLOCK (wvparse);
591 GST_DEBUG_OBJECT (wvparse, "Invalid start sample %" G_GINT64_FORMAT, start);
595 flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
597 /* operate on segment copy until we know the seek worked */
598 segment = wvparse->segment;
600 gst_segment_set_seek (&segment, speed, GST_FORMAT_DEFAULT,
601 seek_flags, start_type, start, stop_type, stop, &only_update);
605 wvparse->segment = segment;
606 gst_wavpack_parse_send_newsegment (wvparse, TRUE);
611 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_start ());
614 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_start ());
616 gst_pad_pause_task (wvparse->sinkpad);
619 GST_PAD_STREAM_LOCK (wvparse->sinkpad);
621 /* Save current position */
622 last_stop = wvparse->segment.last_stop;
624 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_stop ());
627 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_stop ());
630 GST_DEBUG_OBJECT (wvparse, "Performing seek to %" GST_TIME_FORMAT " sample %"
631 G_GINT64_FORMAT, GST_TIME_ARGS (segment.start * GST_SECOND / rate),
634 ret = gst_wavpack_parse_scan_to_find_sample (wvparse, segment.start,
635 &byte_offset, &chunk_start);
638 GST_DEBUG_OBJECT (wvparse, "new offset: %" G_GINT64_FORMAT, byte_offset);
639 wvparse->current_offset = byte_offset;
640 /* we want to send a newsegment event with the actual seek position
641 * as start, even though our first buffer might start before the
642 * configured segment. We leave it up to the decoder or sink to crop
643 * the output buffers accordingly */
644 wvparse->segment = segment;
645 wvparse->segment.last_stop = chunk_start;
646 wvparse->need_newsegment = TRUE;
647 wvparse->discont = (last_stop != chunk_start) ? TRUE : FALSE;
649 /* if we're doing a segment seek, post a SEGMENT_START message */
650 if (wvparse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
651 gst_element_post_message (GST_ELEMENT_CAST (wvparse),
652 gst_message_new_segment_start (GST_OBJECT_CAST (wvparse),
653 wvparse->segment.format, wvparse->segment.last_stop));
656 GST_DEBUG_OBJECT (wvparse, "seek failed: don't know where to seek to");
659 GST_PAD_STREAM_UNLOCK (wvparse->sinkpad);
660 GST_OBJECT_UNLOCK (wvparse);
662 gst_pad_start_task (wvparse->sinkpad,
663 (GstTaskFunction) gst_wavpack_parse_loop, wvparse);
669 gst_wavpack_parse_sink_event (GstPad * pad, GstEvent * event)
671 GstWavpackParse *parse;
675 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
677 switch (GST_EVENT_TYPE (event)) {
678 case GST_EVENT_FLUSH_STOP:{
679 if (parse->adapter) {
680 gst_adapter_clear (parse->adapter);
682 if (parse->pending_buffer) {
683 gst_buffer_unref (parse->pending_buffer);
684 parse->pending_buffer = NULL;
685 parse->pending_offset = 0;
687 ret = gst_pad_push_event (parse->srcpad, event);
690 case GST_EVENT_NEWSEGMENT:{
691 parse->need_newsegment = TRUE;
692 gst_event_unref (event);
697 if (parse->adapter) {
698 /* remove all bytes that are left in the adapter after EOS. They can't
699 * be a complete Wavpack block and we can't do anything with them */
700 gst_adapter_clear (parse->adapter);
702 if (parse->pending_buffer) {
703 gst_buffer_unref (parse->pending_buffer);
704 parse->pending_buffer = NULL;
705 parse->pending_offset = 0;
707 ret = gst_pad_push_event (parse->srcpad, event);
711 /* stream lock is recursive, should be fine for all events */
712 GST_PAD_STREAM_LOCK (pad);
713 if (parse->srcpad == NULL) {
714 parse->queued_events = g_list_append (parse->queued_events, event);
716 ret = gst_pad_push_event (parse->srcpad, event);
718 GST_PAD_STREAM_UNLOCK (pad);
723 gst_object_unref (parse);
728 gst_wavpack_parse_src_event (GstPad * pad, GstEvent * event)
730 GstWavpackParse *parse;
734 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
736 switch (GST_EVENT_TYPE (event)) {
738 ret = gst_wavpack_parse_handle_seek_event (parse, event);
741 ret = gst_pad_event_default (pad, event);
745 gst_object_unref (parse);
750 gst_wavpack_parse_init (GstWavpackParse * parse, GstWavpackParseClass * gclass)
752 GstElementClass *klass = GST_ELEMENT_GET_CLASS (parse);
754 GstPadTemplate *tmpl;
756 tmpl = gst_element_class_get_pad_template (klass, "sink");
757 parse->sinkpad = gst_pad_new_from_template (tmpl, "sink");
759 gst_pad_set_activate_function (parse->sinkpad,
760 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate));
761 gst_pad_set_activatepull_function (parse->sinkpad,
762 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate_pull));
763 gst_pad_set_event_function (parse->sinkpad,
764 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_event));
765 gst_pad_set_chain_function (parse->sinkpad,
766 GST_DEBUG_FUNCPTR (gst_wavpack_parse_chain));
768 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
770 parse->srcpad = NULL;
771 gst_wavpack_parse_reset (parse);
775 gst_wavpack_parse_get_upstream_length (GstWavpackParse * parse)
779 GstFormat format = GST_FORMAT_BYTES;
781 if (!gst_pad_query_peer_duration (parse->sinkpad, &format, &length)) {
784 GST_DEBUG ("upstream length: %" G_GINT64_FORMAT, length);
790 gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse, gint64 offset,
791 guint size, GstFlowReturn * flow)
793 GstFlowReturn flow_ret;
795 GstBuffer *buf = NULL;
797 if (offset + size > wvparse->upstream_length) {
798 wvparse->upstream_length = gst_wavpack_parse_get_upstream_length (wvparse);
799 if (offset + size > wvparse->upstream_length) {
800 GST_DEBUG_OBJECT (wvparse, "EOS: %" G_GINT64_FORMAT " + %u > %"
801 G_GINT64_FORMAT, offset, size, wvparse->upstream_length);
802 flow_ret = GST_FLOW_UNEXPECTED;
807 flow_ret = gst_pad_pull_range (wvparse->sinkpad, offset, size, &buf);
809 if (flow_ret != GST_FLOW_OK) {
810 GST_DEBUG_OBJECT (wvparse, "pull_range (%" G_GINT64_FORMAT ", %u) "
811 "failed, flow: %s", offset, size, gst_flow_get_name (flow_ret));
816 if (GST_BUFFER_SIZE (buf) < size) {
817 GST_DEBUG_OBJECT (wvparse, "Short read at offset %" G_GINT64_FORMAT
818 ", got only %u of %u bytes", offset, GST_BUFFER_SIZE (buf), size);
819 gst_buffer_unref (buf);
821 flow_ret = GST_FLOW_UNEXPECTED;
831 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
832 WavpackHeader * header)
834 GstWavpackMetadata meta;
836 GstCaps *caps = NULL;
840 g_assert (wvparse->srcpad == NULL);
842 bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
844 while (gst_wavpack_read_metadata (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
846 case ID_WVC_BITSTREAM:{
847 caps = gst_caps_new_simple ("audio/x-wavpack-correction",
848 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
850 gst_pad_new_from_template (gst_element_class_get_pad_template
851 (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
854 case ID_WV_BITSTREAM:
855 case ID_WVX_BITSTREAM:{
856 WavpackStreamReader *stream_reader = gst_wavpack_stream_reader_new ();
866 rid.buffer = GST_BUFFER_DATA (buf);
867 rid.length = GST_BUFFER_SIZE (buf);
871 WavpackOpenFileInputEx (stream_reader, &rid, NULL, error_msg, 0, 0);
876 wvparse->samplerate = WavpackGetSampleRate (wpc);
877 wvparse->channels = WavpackGetNumChannels (wpc);
878 wvparse->total_samples =
879 (header->total_samples ==
880 0xffffffff) ? G_GINT64_CONSTANT (-1) : header->total_samples;
882 caps = gst_caps_new_simple ("audio/x-wavpack",
883 "width", G_TYPE_INT, WavpackGetBitsPerSample (wpc),
884 "channels", G_TYPE_INT, wvparse->channels,
885 "rate", G_TYPE_INT, wvparse->samplerate,
886 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
887 #ifdef WAVPACK_OLD_API
888 channel_mask = wpc->config.channel_mask;
890 channel_mask = WavpackGetChannelMask (wpc);
892 if (channel_mask == 0)
894 gst_wavpack_get_default_channel_mask (wvparse->channels);
896 if (channel_mask != 0) {
897 if (!gst_wavpack_set_channel_layout (caps, channel_mask)) {
898 GST_WARNING_OBJECT (wvparse, "Failed to set channel layout");
899 gst_caps_unref (caps);
901 WavpackCloseFile (wpc);
902 g_free (stream_reader);
908 gst_pad_new_from_template (gst_element_class_get_pad_template
909 (GST_ELEMENT_GET_CLASS (wvparse), "src"), "src");
910 WavpackCloseFile (wpc);
911 g_free (stream_reader);
915 GST_LOG_OBJECT (wvparse, "unhandled ID: 0x%02x", meta.id);
923 if (caps == NULL || wvparse->srcpad == NULL)
926 GST_DEBUG_OBJECT (wvparse, "Added src pad with caps %" GST_PTR_FORMAT, caps);
928 gst_pad_set_query_function (wvparse->srcpad,
929 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_query));
930 gst_pad_set_query_type_function (wvparse->srcpad,
931 GST_DEBUG_FUNCPTR (gst_wavpack_parse_get_src_query_types));
932 gst_pad_set_event_function (wvparse->srcpad,
933 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_event));
935 gst_pad_set_caps (wvparse->srcpad, caps);
936 gst_caps_unref (caps);
937 gst_pad_use_fixed_caps (wvparse->srcpad);
939 gst_object_ref (wvparse->srcpad);
940 gst_pad_set_active (wvparse->srcpad, TRUE);
941 gst_element_add_pad (GST_ELEMENT (wvparse), wvparse->srcpad);
942 gst_element_no_more_pads (GST_ELEMENT (wvparse));
948 gst_wavpack_parse_push_buffer (GstWavpackParse * wvparse, GstBuffer * buf,
949 WavpackHeader * header)
952 wvparse->current_offset += header->ckSize + 8;
954 wvparse->segment.last_stop = header->block_index;
956 if (wvparse->need_newsegment) {
957 if (gst_wavpack_parse_send_newsegment (wvparse, FALSE))
958 wvparse->need_newsegment = FALSE;
961 /* send any queued events */
962 if (wvparse->queued_events) {
965 for (l = wvparse->queued_events; l != NULL; l = l->next) {
966 gst_pad_push_event (wvparse->srcpad, GST_EVENT (l->data));
968 g_list_free (wvparse->queued_events);
969 wvparse->queued_events = NULL;
972 if (wvparse->pending_buffer == NULL) {
973 wvparse->pending_buffer = buf;
974 wvparse->pending_offset = header->block_index;
975 } else if (wvparse->pending_offset == header->block_index) {
976 wvparse->pending_buffer = gst_buffer_join (wvparse->pending_buffer, buf);
978 GST_ERROR ("Got incomplete block, dropping");
979 gst_buffer_unref (wvparse->pending_buffer);
980 wvparse->pending_buffer = buf;
981 wvparse->pending_offset = header->block_index;
984 if (!(header->flags & FINAL_BLOCK))
987 buf = wvparse->pending_buffer;
988 wvparse->pending_buffer = NULL;
990 GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (header->block_index,
991 GST_SECOND, wvparse->samplerate);
992 GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (header->block_samples,
993 GST_SECOND, wvparse->samplerate);
994 GST_BUFFER_OFFSET (buf) = header->block_index;
995 GST_BUFFER_OFFSET_END (buf) = header->block_index + header->block_samples;
997 if (wvparse->discont || wvparse->next_block_index != header->block_index) {
998 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
999 wvparse->discont = FALSE;
1002 wvparse->next_block_index = header->block_index + header->block_samples;
1004 gst_buffer_set_caps (buf, GST_PAD_CAPS (wvparse->srcpad));
1006 GST_LOG_OBJECT (wvparse, "Pushing buffer with time %" GST_TIME_FORMAT,
1007 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1009 ret = gst_pad_push (wvparse->srcpad, buf);
1011 wvparse->segment.last_stop = wvparse->next_block_index;
1017 gst_wavpack_parse_find_marker (guint8 * buf, guint size)
1023 if (G_UNLIKELY (size < 4))
1026 for (i = 0; i < size - 4; i++) {
1027 if (memcmp (buf + i, "wvpk", 4) == 0) {
1035 static GstFlowReturn
1036 gst_wavpack_parse_resync_loop (GstWavpackParse * parse, WavpackHeader * header)
1038 GstFlowReturn flow_ret = GST_FLOW_UNEXPECTED;
1040 GstBuffer *buf = NULL;
1042 /* loop until we have a frame header or reach the end of the stream */
1044 guint8 *data, *marker;
1049 gst_buffer_unref (buf);
1053 if (parse->upstream_length == 0 ||
1054 parse->upstream_length <= parse->current_offset) {
1055 parse->upstream_length = gst_wavpack_parse_get_upstream_length (parse);
1056 if (parse->upstream_length == 0 ||
1057 parse->upstream_length <= parse->current_offset) {
1062 len = MIN (parse->upstream_length - parse->current_offset, 2048);
1064 GST_LOG_OBJECT (parse, "offset: %" G_GINT64_FORMAT, parse->current_offset);
1066 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1069 /* whatever the problem is, there's nothing more for us to do for now */
1070 if (flow_ret != GST_FLOW_OK)
1073 data = GST_BUFFER_DATA (buf);
1074 size = GST_BUFFER_SIZE (buf);
1076 /* not enough data for a header? */
1077 if (size < sizeof (WavpackHeader))
1080 /* got a header right where we are at now? */
1081 if (gst_wavpack_read_header (header, data))
1084 /* nope, let's see if we can find one */
1085 marker = gst_wavpack_parse_find_marker (data + 1, size - 1);
1088 parse->current_offset += marker - data;
1089 /* do one more loop iteration to make sure we pull enough
1090 * data for a full header, we'll bail out then */
1092 parse->current_offset += len - 4;
1097 gst_buffer_unref (buf);
1103 gst_wavpack_parse_loop (GstElement * element)
1105 GstWavpackParse *parse = GST_WAVPACK_PARSE (element);
1107 GstFlowReturn flow_ret;
1108 WavpackHeader header = { {0,}, 0, };
1109 GstBuffer *buf = NULL;
1111 flow_ret = gst_wavpack_parse_resync_loop (parse, &header);
1113 if (flow_ret != GST_FLOW_OK)
1116 GST_LOG_OBJECT (parse, "Read header at offset %" G_GINT64_FORMAT
1117 ": chunk size = %u+8", parse->current_offset, header.ckSize);
1119 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1120 header.ckSize + 8, &flow_ret);
1122 if (flow_ret != GST_FLOW_OK)
1125 if (parse->srcpad == NULL) {
1126 if (!gst_wavpack_parse_create_src_pad (parse, buf, &header)) {
1127 GST_ERROR_OBJECT (parse, "Failed to create src pad");
1128 flow_ret = GST_FLOW_ERROR;
1132 if (header.flags & INITIAL_BLOCK)
1133 gst_wavpack_parse_index_append_entry (parse, parse->current_offset,
1134 header.block_index, header.block_samples);
1136 flow_ret = gst_wavpack_parse_push_buffer (parse, buf, &header);
1137 if (flow_ret != GST_FLOW_OK)
1144 const gchar *reason = gst_flow_get_name (flow_ret);
1146 GST_LOG_OBJECT (parse, "pausing task, reason %s", reason);
1147 gst_pad_pause_task (parse->sinkpad);
1149 if (flow_ret == GST_FLOW_UNEXPECTED && parse->srcpad) {
1150 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1153 GST_LOG_OBJECT (parse, "Sending segment done");
1155 if ((stop = parse->segment.stop) == -1)
1156 stop = parse->segment.duration;
1158 gst_element_post_message (GST_ELEMENT_CAST (parse),
1159 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
1160 parse->segment.format, stop));
1162 GST_LOG_OBJECT (parse, "Sending EOS, at end of stream");
1163 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1165 } else if (flow_ret == GST_FLOW_NOT_LINKED
1166 || flow_ret < GST_FLOW_UNEXPECTED) {
1167 GST_ELEMENT_ERROR (parse, STREAM, FAILED,
1168 (_("Internal data stream error.")), ("stream stopped, reason %s",
1171 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1178 gst_wavpack_parse_resync_adapter (GstAdapter * adapter)
1180 const guint8 *buf, *marker;
1182 guint avail = gst_adapter_available (adapter);
1187 /* if the marker is at the beginning don't do the expensive search */
1188 buf = gst_adapter_peek (adapter, 4);
1189 if (memcmp (buf, "wvpk", 4) == 0)
1195 /* search for the marker in the complete content of the adapter */
1196 buf = gst_adapter_peek (adapter, avail);
1197 if (buf && (marker = gst_wavpack_parse_find_marker ((guint8 *) buf, avail))) {
1198 gst_adapter_flush (adapter, marker - buf);
1202 /* flush everything except the last 4 bytes. they could contain
1203 * the start of a new marker */
1204 gst_adapter_flush (adapter, avail - 4);
1209 static GstFlowReturn
1210 gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf)
1212 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (GST_PAD_PARENT (pad));
1214 GstFlowReturn ret = GST_FLOW_OK;
1218 const guint8 *tmp_buf;
1220 if (!wvparse->adapter) {
1221 wvparse->adapter = gst_adapter_new ();
1224 if (GST_BUFFER_IS_DISCONT (buf)) {
1225 gst_adapter_clear (wvparse->adapter);
1226 wvparse->discont = TRUE;
1229 gst_adapter_push (wvparse->adapter, buf);
1231 if (gst_adapter_available (wvparse->adapter) < sizeof (WavpackHeader))
1234 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1237 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1238 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1240 while (gst_adapter_available (wvparse->adapter) >= wph.ckSize + 4 * 1 + 4) {
1242 gst_adapter_take_buffer (wvparse->adapter, wph.ckSize + 4 * 1 + 4);
1245 return GST_FLOW_ERROR;
1247 if (wvparse->srcpad == NULL) {
1248 if (!gst_wavpack_parse_create_src_pad (wvparse, outbuf, &wph)) {
1249 GST_ERROR_OBJECT (wvparse, "Failed to create src pad");
1250 ret = GST_FLOW_ERROR;
1255 ret = gst_wavpack_parse_push_buffer (wvparse, outbuf, &wph);
1257 if (ret != GST_FLOW_OK)
1260 if (gst_adapter_available (wvparse->adapter) >= sizeof (WavpackHeader)) {
1261 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1263 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1266 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1273 static GstStateChangeReturn
1274 gst_wavpack_parse_change_state (GstElement * element, GstStateChange transition)
1276 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (element);
1278 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1280 switch (transition) {
1281 case GST_STATE_CHANGE_READY_TO_PAUSED:
1282 gst_segment_init (&wvparse->segment, GST_FORMAT_DEFAULT);
1283 wvparse->segment.last_stop = 0;
1288 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1289 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1291 switch (transition) {
1292 case GST_STATE_CHANGE_PAUSED_TO_READY:
1293 gst_wavpack_parse_reset (wvparse);
1303 gst_wavpack_parse_sink_activate (GstPad * sinkpad)
1305 if (gst_pad_check_pull_range (sinkpad)) {
1306 return gst_pad_activate_pull (sinkpad, TRUE);
1308 return gst_pad_activate_push (sinkpad, TRUE);
1313 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
1318 result = gst_pad_start_task (sinkpad,
1319 (GstTaskFunction) gst_wavpack_parse_loop, GST_PAD_PARENT (sinkpad));
1321 result = gst_pad_stop_task (sinkpad);
1328 gst_wavpack_parse_plugin_init (GstPlugin * plugin)
1330 if (!gst_element_register (plugin, "wavpackparse",
1331 GST_RANK_PRIMARY, GST_TYPE_WAVPACK_PARSE)) {
1335 GST_DEBUG_CATEGORY_INIT (gst_wavpack_parse_debug, "wavpack_parse", 0,
1336 "Wavpack file parser");