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 static inline GstWavpackParseIndexEntry *
61 gst_wavpack_parse_index_entry_new ()
63 return g_slice_new (GstWavpackParseIndexEntry);
67 gst_wavpack_parse_index_entry_free (GstWavpackParseIndexEntry * entry)
69 g_slice_free (GstWavpackParseIndexEntry, entry);
72 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
75 GST_STATIC_CAPS ("audio/x-wavpack, "
76 "framed = (boolean) false; "
77 "audio/x-wavpack-correction, " "framed = (boolean) false")
80 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
83 GST_STATIC_CAPS ("audio/x-wavpack, "
84 "width = (int) [ 1, 32 ], "
85 "channels = (int) [ 1, 8 ], "
86 "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
89 static GstStaticPadTemplate wvc_src_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
92 GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) true")
95 static gboolean gst_wavpack_parse_sink_activate (GstPad * sinkpad);
97 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active);
99 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);
103 static gint64 gst_wavpack_parse_get_upstream_length (GstWavpackParse * wvparse);
104 static GstBuffer *gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse,
105 gint64 offset, guint size, GstFlowReturn * flow);
106 static GstFlowReturn gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf);
108 GST_BOILERPLATE (GstWavpackParse, gst_wavpack_parse, GstElement,
112 gst_wavpack_parse_base_init (gpointer klass)
114 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
116 gst_element_class_add_pad_template (element_class,
117 gst_static_pad_template_get (&src_factory));
118 gst_element_class_add_pad_template (element_class,
119 gst_static_pad_template_get (&wvc_src_factory));
120 gst_element_class_add_pad_template (element_class,
121 gst_static_pad_template_get (&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;
142 GstElementClass *gstelement_class;
144 gobject_class = (GObjectClass *) klass;
145 gstelement_class = (GstElementClass *) klass;
147 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wavpack_parse_finalize);
148 gstelement_class->change_state =
149 GST_DEBUG_FUNCPTR (gst_wavpack_parse_change_state);
152 static GstWavpackParseIndexEntry *
153 gst_wavpack_parse_index_get_last_entry (GstWavpackParse * wvparse)
155 g_assert (wvparse->entries != NULL);
157 return wvparse->entries->data;
160 static GstWavpackParseIndexEntry *
161 gst_wavpack_parse_index_get_entry_from_sample (GstWavpackParse * wvparse,
162 gint64 sample_offset)
167 if (wvparse->entries == NULL)
170 for (node = wvparse->entries, i = 0; node; node = node->next, i++) {
171 GstWavpackParseIndexEntry *entry;
175 GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
176 " byte %" G_GINT64_FORMAT, i, entry->sample_offset, entry->byte_offset);
178 if (entry->sample_offset <= sample_offset &&
179 sample_offset < entry->sample_offset_end) {
180 GST_LOG_OBJECT (wvparse, "found match");
184 /* as the list is sorted and we first look at the latest entry
185 * we can abort searching for an entry if the sample we want is
186 * after the latest one */
187 if (sample_offset >= entry->sample_offset_end)
190 GST_LOG_OBJECT (wvparse, "no match in index");
195 gst_wavpack_parse_index_append_entry (GstWavpackParse * wvparse,
196 gint64 byte_offset, gint64 sample_offset, gint64 num_samples)
198 GstWavpackParseIndexEntry *entry;
200 /* do we have this one already? */
201 if (wvparse->entries) {
202 entry = gst_wavpack_parse_index_get_last_entry (wvparse);
203 if (entry->byte_offset >= byte_offset
204 || entry->sample_offset >= sample_offset)
208 GST_LOG_OBJECT (wvparse, "Adding index entry %8" G_GINT64_FORMAT " - %"
209 GST_TIME_FORMAT " @ offset 0x%08" G_GINT64_MODIFIER "x", sample_offset,
210 GST_TIME_ARGS (gst_util_uint64_scale_int (sample_offset,
211 GST_SECOND, wvparse->samplerate)), byte_offset);
213 entry = gst_wavpack_parse_index_entry_new ();
214 entry->byte_offset = byte_offset;
215 entry->sample_offset = sample_offset;
216 entry->sample_offset_end = sample_offset + num_samples;
217 wvparse->entries = g_slist_prepend (wvparse->entries, entry);
221 gst_wavpack_parse_reset (GstWavpackParse * parse)
223 parse->total_samples = -1;
224 parse->samplerate = 0;
227 gst_segment_init (&parse->segment, GST_FORMAT_UNDEFINED);
228 parse->next_block_index = 0;
230 parse->current_offset = 0;
231 parse->need_newsegment = TRUE;
232 parse->discont = TRUE;
233 parse->upstream_length = -1;
235 if (parse->entries) {
236 g_slist_foreach (parse->entries, (GFunc) gst_wavpack_parse_index_entry_free,
238 g_slist_free (parse->entries);
239 parse->entries = NULL;
242 if (parse->adapter) {
243 gst_adapter_clear (parse->adapter);
244 g_object_unref (parse->adapter);
245 parse->adapter = NULL;
248 if (parse->srcpad != NULL) {
251 GST_DEBUG_OBJECT (parse, "Removing src pad");
252 res = gst_element_remove_pad (GST_ELEMENT (parse), parse->srcpad);
253 g_return_if_fail (res != FALSE);
254 gst_object_unref (parse->srcpad);
255 parse->srcpad = NULL;
258 g_list_foreach (parse->queued_events, (GFunc) gst_mini_object_unref, NULL);
259 g_list_free (parse->queued_events);
260 parse->queued_events = NULL;
262 if (parse->pending_buffer)
263 gst_buffer_unref (parse->pending_buffer);
265 parse->pending_buffer = NULL;
268 static const GstQueryType *
269 gst_wavpack_parse_get_src_query_types (GstPad * pad)
271 static const GstQueryType types[] = {
282 gst_wavpack_parse_src_query (GstPad * pad, GstQuery * query)
284 GstWavpackParse *parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
286 gboolean ret = FALSE;
288 switch (GST_QUERY_TYPE (query)) {
289 case GST_QUERY_POSITION:{
293 GST_OBJECT_LOCK (parse);
294 cur = parse->segment.last_stop;
295 rate = parse->samplerate;
296 GST_OBJECT_UNLOCK (parse);
299 GST_DEBUG_OBJECT (parse, "haven't read header yet");
303 gst_query_parse_position (query, &format, NULL);
306 case GST_FORMAT_TIME:
307 cur = gst_util_uint64_scale_int (cur, GST_SECOND, rate);
308 gst_query_set_position (query, GST_FORMAT_TIME, cur);
311 case GST_FORMAT_DEFAULT:
312 gst_query_set_position (query, GST_FORMAT_DEFAULT, cur);
316 GST_DEBUG_OBJECT (parse, "cannot handle position query in "
317 "%s format. Forwarding upstream.", gst_format_get_name (format));
318 ret = gst_pad_query_default (pad, query);
323 case GST_QUERY_DURATION:{
327 GST_OBJECT_LOCK (parse);
328 rate = parse->samplerate;
329 len = parse->total_samples;
330 GST_OBJECT_UNLOCK (parse);
333 GST_DEBUG_OBJECT (parse, "haven't read header yet");
337 gst_query_parse_duration (query, &format, NULL);
340 case GST_FORMAT_TIME:
342 len = gst_util_uint64_scale_int (len, GST_SECOND, rate);
343 gst_query_set_duration (query, GST_FORMAT_TIME, len);
346 case GST_FORMAT_DEFAULT:
347 gst_query_set_duration (query, GST_FORMAT_DEFAULT, len);
351 GST_DEBUG_OBJECT (parse, "cannot handle duration query in "
352 "%s format. Forwarding upstream.", gst_format_get_name (format));
353 ret = gst_pad_query_default (pad, query);
358 case GST_QUERY_SEEKING:{
359 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
360 if (format == GST_FORMAT_TIME || format == GST_FORMAT_DEFAULT) {
362 gint64 duration = -1;
364 /* only fails if we didn't read the headers yet and can't say
365 * anything about our seeking capabilities */
366 if (!gst_pad_query_duration (pad, &format, &duration))
369 /* can't seek in streaming mode yet */
370 GST_OBJECT_LOCK (parse);
371 seekable = (parse->adapter == NULL);
372 GST_OBJECT_UNLOCK (parse);
374 gst_query_set_seeking (query, format, seekable, 0, duration);
380 ret = gst_pad_query_default (pad, query);
385 gst_object_unref (parse);
390 /* returns TRUE on success, with byte_offset set to the offset of the
391 * wavpack chunk containing the sample requested. start_sample will be
392 * set to the first sample in the chunk starting at byte_offset.
393 * Scanning from the last known header offset to the wanted position
394 * when seeking forward isn't very clever, but seems fast enough in
395 * practice and has the nice side effect of populating our index
398 gst_wavpack_parse_scan_to_find_sample (GstWavpackParse * parse,
399 gint64 sample, gint64 * byte_offset, gint64 * start_sample)
401 GstWavpackParseIndexEntry *entry;
405 /* first, check if we have to scan at all */
406 entry = gst_wavpack_parse_index_get_entry_from_sample (parse, sample);
408 *byte_offset = entry->byte_offset;
409 *start_sample = entry->sample_offset;
410 GST_LOG_OBJECT (parse, "Found index entry: sample %" G_GINT64_FORMAT
411 " @ offset %" G_GINT64_FORMAT, entry->sample_offset,
416 GST_LOG_OBJECT (parse, "No matching entry in index, scanning file ...");
418 /* if we have an index, we can start scanning from the last known offset
419 * in there, after all we know our wanted sample is not in the index */
420 if (parse->entries) {
421 GstWavpackParseIndexEntry *entry;
423 entry = gst_wavpack_parse_index_get_last_entry (parse);
424 off = entry->byte_offset;
427 /* now scan forward until we find the chunk we're looking for or hit EOS */
429 WavpackHeader header;
432 buf = gst_wavpack_parse_pull_buffer (parse, off, sizeof (WavpackHeader),
438 gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
439 gst_buffer_unref (buf);
441 if (header.flags & INITIAL_BLOCK)
442 gst_wavpack_parse_index_append_entry (parse, off, header.block_index,
443 header.block_samples);
447 if (header.block_index <= sample &&
448 sample < (header.block_index + header.block_samples)) {
450 *start_sample = header.block_index;
454 off += header.ckSize + 8;
457 GST_DEBUG_OBJECT (parse, "scan failed: %s (off=0x%08" G_GINT64_MODIFIER "x)",
458 gst_flow_get_name (ret), off);
464 gst_wavpack_parse_send_newsegment (GstWavpackParse * wvparse, gboolean update)
466 GstSegment *s = &wvparse->segment;
468 gint64 stop_time = -1;
469 gint64 start_time = 0;
473 /* segment is in DEFAULT format, but we want to send a TIME newsegment */
474 start_time = gst_util_uint64_scale_int (s->start, GST_SECOND,
475 wvparse->samplerate);
478 stop_time = gst_util_uint64_scale_int (s->stop, GST_SECOND,
479 wvparse->samplerate);
482 GST_DEBUG_OBJECT (wvparse, "sending newsegment from %" GST_TIME_FORMAT
483 " to %" GST_TIME_FORMAT, GST_TIME_ARGS (start_time),
484 GST_TIME_ARGS (stop_time));
486 /* after a seek, s->last_stop will point to a chunk boundary, ie. from
487 * which sample we will start sending data again, while s->start will
488 * point to the sample we actually want to seek to and want to start
489 * playing right after the seek. Adjust clock-time for the difference
490 * so we start playing from start_time */
491 cur_pos_time = gst_util_uint64_scale_int (s->last_stop, GST_SECOND,
492 wvparse->samplerate);
493 diff = start_time - cur_pos_time;
495 ret = gst_pad_push_event (wvparse->srcpad,
496 gst_event_new_new_segment (update, s->rate, GST_FORMAT_TIME,
497 start_time, stop_time, start_time - diff));
503 gst_wavpack_parse_handle_seek_event (GstWavpackParse * wvparse,
506 GstSeekFlags seek_flags;
507 GstSeekType start_type;
508 GstSeekType stop_type;
511 gboolean only_update;
515 gint64 start; /* sample we want to seek to */
516 gint64 byte_offset; /* byte offset the chunk we seek to starts at */
517 gint64 chunk_start; /* first sample in chunk we seek to */
521 if (wvparse->adapter) {
522 GST_DEBUG_OBJECT (wvparse, "seeking in streaming mode not implemented yet");
526 gst_event_parse_seek (event, &speed, &format, &seek_flags, &start_type,
527 &start, &stop_type, &stop);
529 if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME) {
530 GST_DEBUG ("seeking is only supported in TIME or DEFAULT format");
535 GST_DEBUG ("only forward playback supported, rate %f not allowed", speed);
539 GST_OBJECT_LOCK (wvparse);
541 rate = wvparse->samplerate;
543 GST_OBJECT_UNLOCK (wvparse);
544 GST_DEBUG ("haven't read header yet");
548 /* figure out the last position we need to play. If it's configured (stop !=
549 * -1), use that, else we play until the total duration of the file */
551 stop = wvparse->segment.duration;
553 /* convert from time to samples if necessary */
554 if (format == GST_FORMAT_TIME) {
555 if (start_type != GST_SEEK_TYPE_NONE)
556 start = gst_util_uint64_scale_int (start, rate, GST_SECOND);
557 if (stop_type != GST_SEEK_TYPE_NONE)
558 stop = gst_util_uint64_scale_int (stop, rate, GST_SECOND);
562 GST_OBJECT_UNLOCK (wvparse);
563 GST_DEBUG_OBJECT (wvparse, "Invalid start sample %" G_GINT64_FORMAT, start);
567 flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
569 /* operate on segment copy until we know the seek worked */
570 segment = wvparse->segment;
572 gst_segment_set_seek (&segment, speed, GST_FORMAT_DEFAULT,
573 seek_flags, start_type, start, stop_type, stop, &only_update);
577 wvparse->segment = segment;
578 gst_wavpack_parse_send_newsegment (wvparse, TRUE);
583 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_start ());
586 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_start ());
588 gst_pad_pause_task (wvparse->sinkpad);
591 GST_PAD_STREAM_LOCK (wvparse->sinkpad);
593 /* Save current position */
594 last_stop = wvparse->segment.last_stop;
596 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_stop ());
599 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_stop ());
602 GST_DEBUG_OBJECT (wvparse, "Performing seek to %" GST_TIME_FORMAT " sample %"
603 G_GINT64_FORMAT, GST_TIME_ARGS (segment.start * GST_SECOND / rate),
606 ret = gst_wavpack_parse_scan_to_find_sample (wvparse, segment.start,
607 &byte_offset, &chunk_start);
610 GST_DEBUG_OBJECT (wvparse, "new offset: %" G_GINT64_FORMAT, byte_offset);
611 wvparse->current_offset = byte_offset;
612 /* we want to send a newsegment event with the actual seek position
613 * as start, even though our first buffer might start before the
614 * configured segment. We leave it up to the decoder or sink to crop
615 * the output buffers accordingly */
616 wvparse->segment = segment;
617 wvparse->segment.last_stop = chunk_start;
618 wvparse->need_newsegment = TRUE;
619 wvparse->discont = (last_stop != chunk_start) ? TRUE : FALSE;
621 /* if we're doing a segment seek, post a SEGMENT_START message */
622 if (wvparse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
623 gst_element_post_message (GST_ELEMENT_CAST (wvparse),
624 gst_message_new_segment_start (GST_OBJECT_CAST (wvparse),
625 wvparse->segment.format, wvparse->segment.last_stop));
628 GST_DEBUG_OBJECT (wvparse, "seek failed: don't know where to seek to");
631 GST_PAD_STREAM_UNLOCK (wvparse->sinkpad);
632 GST_OBJECT_UNLOCK (wvparse);
634 gst_pad_start_task (wvparse->sinkpad,
635 (GstTaskFunction) gst_wavpack_parse_loop, wvparse);
641 gst_wavpack_parse_sink_event (GstPad * pad, GstEvent * event)
643 GstWavpackParse *parse;
646 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
648 switch (GST_EVENT_TYPE (event)) {
649 case GST_EVENT_FLUSH_STOP:{
650 if (parse->adapter) {
651 gst_adapter_clear (parse->adapter);
653 if (parse->pending_buffer) {
654 gst_buffer_unref (parse->pending_buffer);
655 parse->pending_buffer = NULL;
656 parse->pending_offset = 0;
658 ret = gst_pad_push_event (parse->srcpad, event);
661 case GST_EVENT_NEWSEGMENT:{
662 parse->need_newsegment = TRUE;
663 gst_event_unref (event);
668 if (parse->adapter) {
669 /* remove all bytes that are left in the adapter after EOS. They can't
670 * be a complete Wavpack block and we can't do anything with them */
671 gst_adapter_clear (parse->adapter);
673 if (parse->pending_buffer) {
674 gst_buffer_unref (parse->pending_buffer);
675 parse->pending_buffer = NULL;
676 parse->pending_offset = 0;
678 ret = gst_pad_push_event (parse->srcpad, event);
682 /* stream lock is recursive, should be fine for all events */
683 GST_PAD_STREAM_LOCK (pad);
684 if (parse->srcpad == NULL) {
685 parse->queued_events = g_list_append (parse->queued_events, event);
687 ret = gst_pad_push_event (parse->srcpad, event);
689 GST_PAD_STREAM_UNLOCK (pad);
694 gst_object_unref (parse);
699 gst_wavpack_parse_src_event (GstPad * pad, GstEvent * event)
701 GstWavpackParse *parse;
704 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
706 switch (GST_EVENT_TYPE (event)) {
708 ret = gst_wavpack_parse_handle_seek_event (parse, event);
711 ret = gst_pad_event_default (pad, event);
715 gst_object_unref (parse);
720 gst_wavpack_parse_init (GstWavpackParse * parse, GstWavpackParseClass * gclass)
722 GstElementClass *klass = GST_ELEMENT_GET_CLASS (parse);
723 GstPadTemplate *tmpl;
725 tmpl = gst_element_class_get_pad_template (klass, "sink");
726 parse->sinkpad = gst_pad_new_from_template (tmpl, "sink");
728 gst_pad_set_activate_function (parse->sinkpad,
729 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate));
730 gst_pad_set_activatepull_function (parse->sinkpad,
731 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate_pull));
732 gst_pad_set_event_function (parse->sinkpad,
733 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_event));
734 gst_pad_set_chain_function (parse->sinkpad,
735 GST_DEBUG_FUNCPTR (gst_wavpack_parse_chain));
737 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
739 parse->srcpad = NULL;
740 gst_wavpack_parse_reset (parse);
744 gst_wavpack_parse_get_upstream_length (GstWavpackParse * parse)
748 GstFormat format = GST_FORMAT_BYTES;
750 if (!gst_pad_query_peer_duration (parse->sinkpad, &format, &length)) {
753 GST_DEBUG ("upstream length: %" G_GINT64_FORMAT, length);
759 gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse, gint64 offset,
760 guint size, GstFlowReturn * flow)
762 GstFlowReturn flow_ret;
763 GstBuffer *buf = NULL;
765 if (offset + size >= wvparse->upstream_length) {
766 wvparse->upstream_length = gst_wavpack_parse_get_upstream_length (wvparse);
767 if (offset + size >= wvparse->upstream_length) {
768 GST_DEBUG_OBJECT (wvparse, "EOS: %" G_GINT64_FORMAT " + %u > %"
769 G_GINT64_FORMAT, offset, size, wvparse->upstream_length);
770 flow_ret = GST_FLOW_UNEXPECTED;
775 flow_ret = gst_pad_pull_range (wvparse->sinkpad, offset, size, &buf);
777 if (flow_ret != GST_FLOW_OK) {
778 GST_DEBUG_OBJECT (wvparse, "pull_range (%" G_GINT64_FORMAT ", %u) "
779 "failed, flow: %s", offset, size, gst_flow_get_name (flow_ret));
784 if (GST_BUFFER_SIZE (buf) < size) {
785 GST_DEBUG_OBJECT (wvparse, "Short read at offset %" G_GINT64_FORMAT
786 ", got only %u of %u bytes", offset, GST_BUFFER_SIZE (buf), size);
787 gst_buffer_unref (buf);
789 flow_ret = GST_FLOW_UNEXPECTED;
799 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
800 WavpackHeader * header)
802 GstWavpackMetadata meta;
803 GstCaps *caps = NULL;
806 g_assert (wvparse->srcpad == NULL);
808 bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
810 while (gst_wavpack_read_metadata (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
812 case ID_WVC_BITSTREAM:{
813 caps = gst_caps_new_simple ("audio/x-wavpack-correction",
814 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
816 gst_pad_new_from_template (gst_element_class_get_pad_template
817 (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
820 case ID_WV_BITSTREAM:
821 case ID_WVX_BITSTREAM:{
822 WavpackStreamReader *stream_reader = gst_wavpack_stream_reader_new ();
828 rid.buffer = GST_BUFFER_DATA (buf);
829 rid.length = GST_BUFFER_SIZE (buf);
833 WavpackOpenFileInputEx (stream_reader, &rid, NULL, error_msg, 0, 0);
838 wvparse->samplerate = WavpackGetSampleRate (wpc);
839 wvparse->channels = WavpackGetNumChannels (wpc);
840 wvparse->total_samples = (header->total_samples == (int32_t) - 1) ?
841 -1 : header->total_samples;
843 caps = gst_caps_new_simple ("audio/x-wavpack",
844 "width", G_TYPE_INT, WavpackGetBitsPerSample (wpc),
845 "channels", G_TYPE_INT, wvparse->channels,
846 "rate", G_TYPE_INT, wvparse->samplerate,
847 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
848 #ifdef WAVPACK_OLD_API
849 channel_mask = wpc->config.channel_mask;
851 channel_mask = WavpackGetChannelMask (wpc);
853 if (channel_mask == 0)
855 gst_wavpack_get_default_channel_mask (wvparse->channels);
857 if (channel_mask != 0) {
858 if (!gst_wavpack_set_channel_layout (caps, channel_mask)) {
859 GST_WARNING_OBJECT (wvparse, "Failed to set channel layout");
860 gst_caps_unref (caps);
862 WavpackCloseFile (wpc);
863 g_free (stream_reader);
869 gst_pad_new_from_template (gst_element_class_get_pad_template
870 (GST_ELEMENT_GET_CLASS (wvparse), "src"), "src");
871 WavpackCloseFile (wpc);
872 g_free (stream_reader);
876 GST_LOG_OBJECT (wvparse, "unhandled ID: 0x%02x", meta.id);
884 if (caps == NULL || wvparse->srcpad == NULL)
887 GST_DEBUG_OBJECT (wvparse, "Added src pad with caps %" GST_PTR_FORMAT, caps);
889 gst_pad_set_query_function (wvparse->srcpad,
890 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_query));
891 gst_pad_set_query_type_function (wvparse->srcpad,
892 GST_DEBUG_FUNCPTR (gst_wavpack_parse_get_src_query_types));
893 gst_pad_set_event_function (wvparse->srcpad,
894 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_event));
896 gst_pad_set_caps (wvparse->srcpad, caps);
897 gst_caps_unref (caps);
898 gst_pad_use_fixed_caps (wvparse->srcpad);
900 gst_object_ref (wvparse->srcpad);
901 gst_pad_set_active (wvparse->srcpad, TRUE);
902 gst_element_add_pad (GST_ELEMENT (wvparse), wvparse->srcpad);
903 gst_element_no_more_pads (GST_ELEMENT (wvparse));
909 gst_wavpack_parse_push_buffer (GstWavpackParse * wvparse, GstBuffer * buf,
910 WavpackHeader * header)
912 wvparse->current_offset += header->ckSize + 8;
914 wvparse->segment.last_stop = header->block_index;
916 if (wvparse->need_newsegment) {
917 if (gst_wavpack_parse_send_newsegment (wvparse, FALSE))
918 wvparse->need_newsegment = FALSE;
921 /* send any queued events */
922 if (wvparse->queued_events) {
925 for (l = wvparse->queued_events; l != NULL; l = l->next) {
926 gst_pad_push_event (wvparse->srcpad, GST_EVENT (l->data));
928 g_list_free (wvparse->queued_events);
929 wvparse->queued_events = NULL;
932 if (wvparse->pending_buffer == NULL) {
933 wvparse->pending_buffer = buf;
934 wvparse->pending_offset = header->block_index;
935 } else if (wvparse->pending_offset == header->block_index) {
936 wvparse->pending_buffer = gst_buffer_join (wvparse->pending_buffer, buf);
938 GST_ERROR ("Got incomplete block, dropping");
939 gst_buffer_unref (wvparse->pending_buffer);
940 wvparse->pending_buffer = buf;
941 wvparse->pending_offset = header->block_index;
944 if (!(header->flags & FINAL_BLOCK))
947 buf = wvparse->pending_buffer;
948 wvparse->pending_buffer = NULL;
950 GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (header->block_index,
951 GST_SECOND, wvparse->samplerate);
952 GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (header->block_samples,
953 GST_SECOND, wvparse->samplerate);
954 GST_BUFFER_OFFSET (buf) = header->block_index;
955 GST_BUFFER_OFFSET_END (buf) = header->block_index + header->block_samples;
957 if (wvparse->discont || wvparse->next_block_index != header->block_index) {
958 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
959 wvparse->discont = FALSE;
962 wvparse->next_block_index = header->block_index + header->block_samples;
964 gst_buffer_set_caps (buf, GST_PAD_CAPS (wvparse->srcpad));
966 GST_LOG_OBJECT (wvparse, "Pushing buffer with time %" GST_TIME_FORMAT,
967 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
969 return gst_pad_push (wvparse->srcpad, buf);
973 gst_wavpack_parse_find_marker (guint8 * buf, guint size)
978 if (G_UNLIKELY (size < 4))
981 for (i = 0; i < size - 4; i++) {
982 if (memcmp (buf + i, "wvpk", 4) == 0) {
991 gst_wavpack_parse_resync_loop (GstWavpackParse * parse, WavpackHeader * header)
993 GstFlowReturn flow_ret = GST_FLOW_UNEXPECTED;
994 GstBuffer *buf = NULL;
996 /* loop until we have a frame header or reach the end of the stream */
998 guint8 *data, *marker;
1002 gst_buffer_unref (buf);
1006 if (parse->upstream_length == 0 ||
1007 parse->upstream_length <= parse->current_offset) {
1008 parse->upstream_length = gst_wavpack_parse_get_upstream_length (parse);
1009 if (parse->upstream_length == 0 ||
1010 parse->upstream_length <= parse->current_offset) {
1015 len = MIN (parse->upstream_length - parse->current_offset, 2048);
1017 GST_LOG_OBJECT (parse, "offset: %" G_GINT64_FORMAT, parse->current_offset);
1019 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1022 /* whatever the problem is, there's nothing more for us to do for now */
1023 if (flow_ret != GST_FLOW_OK)
1026 data = GST_BUFFER_DATA (buf);
1027 size = GST_BUFFER_SIZE (buf);
1029 /* not enough data for a header? */
1030 if (size < sizeof (WavpackHeader))
1033 /* got a header right where we are at now? */
1034 if (gst_wavpack_read_header (header, data))
1037 /* nope, let's see if we can find one */
1038 marker = gst_wavpack_parse_find_marker (data + 1, size - 1);
1041 parse->current_offset += marker - data;
1042 /* do one more loop iteration to make sure we pull enough
1043 * data for a full header, we'll bail out then */
1045 parse->current_offset += len - 4;
1050 gst_buffer_unref (buf);
1056 gst_wavpack_parse_loop (GstElement * element)
1058 GstWavpackParse *parse = GST_WAVPACK_PARSE (element);
1059 GstFlowReturn flow_ret;
1060 WavpackHeader header = { {0,}, 0, };
1061 GstBuffer *buf = NULL;
1063 flow_ret = gst_wavpack_parse_resync_loop (parse, &header);
1065 if (flow_ret != GST_FLOW_OK)
1068 GST_LOG_OBJECT (parse, "Read header at offset %" G_GINT64_FORMAT
1069 ": chunk size = %u+8", parse->current_offset, header.ckSize);
1071 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1072 header.ckSize + 8, &flow_ret);
1074 if (flow_ret != GST_FLOW_OK)
1077 if (parse->srcpad == NULL) {
1078 if (!gst_wavpack_parse_create_src_pad (parse, buf, &header)) {
1079 GST_ERROR_OBJECT (parse, "Failed to create src pad");
1080 flow_ret = GST_FLOW_ERROR;
1084 if (header.flags & INITIAL_BLOCK)
1085 gst_wavpack_parse_index_append_entry (parse, parse->current_offset,
1086 header.block_index, header.block_samples);
1088 flow_ret = gst_wavpack_parse_push_buffer (parse, buf, &header);
1089 if (flow_ret != GST_FLOW_OK)
1096 const gchar *reason = gst_flow_get_name (flow_ret);
1098 GST_LOG_OBJECT (parse, "pausing task, reason %s", reason);
1099 gst_pad_pause_task (parse->sinkpad);
1101 if (GST_FLOW_IS_FATAL (flow_ret) || flow_ret == GST_FLOW_NOT_LINKED) {
1102 if (flow_ret == GST_FLOW_UNEXPECTED && parse->srcpad) {
1103 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1106 GST_LOG_OBJECT (parse, "Sending segment done");
1108 if ((stop = parse->segment.stop) == -1)
1109 stop = parse->segment.duration;
1111 gst_element_post_message (GST_ELEMENT_CAST (parse),
1112 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
1113 parse->segment.format, stop));
1115 GST_LOG_OBJECT (parse, "Sending EOS, at end of stream");
1116 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1119 GST_ELEMENT_ERROR (parse, STREAM, FAILED,
1120 (_("Internal data stream error.")),
1121 ("stream stopped, reason %s", reason));
1123 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1131 gst_wavpack_parse_resync_adapter (GstAdapter * adapter)
1133 const guint8 *buf, *marker;
1134 guint avail = gst_adapter_available (adapter);
1139 /* if the marker is at the beginning don't do the expensive search */
1140 buf = gst_adapter_peek (adapter, 4);
1141 if (memcmp (buf, "wvpk", 4) == 0)
1147 /* search for the marker in the complete content of the adapter */
1148 buf = gst_adapter_peek (adapter, avail);
1149 if (buf && (marker = gst_wavpack_parse_find_marker ((guint8 *) buf, avail))) {
1150 gst_adapter_flush (adapter, marker - buf);
1154 /* flush everything except the last 4 bytes. they could contain
1155 * the start of a new marker */
1156 gst_adapter_flush (adapter, avail - 4);
1161 static GstFlowReturn
1162 gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf)
1164 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (GST_PAD_PARENT (pad));
1165 GstFlowReturn ret = GST_FLOW_OK;
1167 const guint8 *tmp_buf;
1169 if (!wvparse->adapter) {
1170 wvparse->adapter = gst_adapter_new ();
1173 if (GST_BUFFER_IS_DISCONT (buf)) {
1174 gst_adapter_clear (wvparse->adapter);
1175 wvparse->discont = TRUE;
1178 gst_adapter_push (wvparse->adapter, buf);
1180 if (gst_adapter_available (wvparse->adapter) < sizeof (WavpackHeader))
1183 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1186 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1187 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1189 while (gst_adapter_available (wvparse->adapter) >= wph.ckSize + 4 * 1 + 4) {
1191 gst_adapter_take_buffer (wvparse->adapter, wph.ckSize + 4 * 1 + 4);
1194 return GST_FLOW_ERROR;
1196 if (wvparse->srcpad == NULL) {
1197 if (!gst_wavpack_parse_create_src_pad (wvparse, outbuf, &wph)) {
1198 GST_ERROR_OBJECT (wvparse, "Failed to create src pad");
1199 ret = GST_FLOW_ERROR;
1204 ret = gst_wavpack_parse_push_buffer (wvparse, outbuf, &wph);
1206 if (ret != GST_FLOW_OK)
1209 if (gst_adapter_available (wvparse->adapter) >= sizeof (WavpackHeader)) {
1210 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1212 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1215 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1222 static GstStateChangeReturn
1223 gst_wavpack_parse_change_state (GstElement * element, GstStateChange transition)
1225 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (element);
1226 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1228 switch (transition) {
1229 case GST_STATE_CHANGE_READY_TO_PAUSED:
1230 gst_segment_init (&wvparse->segment, GST_FORMAT_DEFAULT);
1231 wvparse->segment.last_stop = 0;
1236 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1237 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1239 switch (transition) {
1240 case GST_STATE_CHANGE_PAUSED_TO_READY:
1241 gst_wavpack_parse_reset (wvparse);
1251 gst_wavpack_parse_sink_activate (GstPad * sinkpad)
1253 if (gst_pad_check_pull_range (sinkpad)) {
1254 return gst_pad_activate_pull (sinkpad, TRUE);
1256 return gst_pad_activate_push (sinkpad, TRUE);
1261 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
1266 result = gst_pad_start_task (sinkpad,
1267 (GstTaskFunction) gst_wavpack_parse_loop, GST_PAD_PARENT (sinkpad));
1269 result = gst_pad_stop_task (sinkpad);
1276 gst_wavpack_parse_plugin_init (GstPlugin * plugin)
1278 if (!gst_element_register (plugin, "wavpackparse",
1279 GST_RANK_PRIMARY, GST_TYPE_WAVPACK_PARSE)) {
1283 GST_DEBUG_CATEGORY_INIT (gst_wavpack_parse_debug, "wavpack_parse", 0,
1284 "Wavpack file parser");