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_pad_template (element_class,
119 gst_static_pad_template_get (&src_factory));
120 gst_element_class_add_pad_template (element_class,
121 gst_static_pad_template_get (&wvc_src_factory));
122 gst_element_class_add_pad_template (element_class,
123 gst_static_pad_template_get (&sink_factory));
125 gst_element_class_set_details_simple (element_class, "Wavpack parser",
126 "Codec/Demuxer/Audio",
127 "Parses Wavpack files",
128 "Arwed v. Merkatz <v.merkatz@gmx.net>, "
129 "Sebastian Dröge <slomo@circular-chaos.org>");
133 gst_wavpack_parse_finalize (GObject * object)
135 gst_wavpack_parse_reset (GST_WAVPACK_PARSE (object));
137 G_OBJECT_CLASS (parent_class)->finalize (object);
141 gst_wavpack_parse_class_init (GstWavpackParseClass * klass)
143 GObjectClass *gobject_class;
145 GstElementClass *gstelement_class;
147 gobject_class = (GObjectClass *) klass;
148 gstelement_class = (GstElementClass *) klass;
150 gobject_class->finalize = gst_wavpack_parse_finalize;
151 gstelement_class->change_state =
152 GST_DEBUG_FUNCPTR (gst_wavpack_parse_change_state);
155 static GstWavpackParseIndexEntry *
156 gst_wavpack_parse_index_get_last_entry (GstWavpackParse * wvparse)
158 g_assert (wvparse->entries != NULL);
160 return wvparse->entries->data;
163 static GstWavpackParseIndexEntry *
164 gst_wavpack_parse_index_get_entry_from_sample (GstWavpackParse * wvparse,
165 gint64 sample_offset)
171 if (wvparse->entries == NULL)
174 for (node = wvparse->entries, i = 0; node; node = node->next, i++) {
175 GstWavpackParseIndexEntry *entry;
179 GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
180 " byte %" G_GINT64_FORMAT, i, entry->sample_offset, entry->byte_offset);
182 if (entry->sample_offset <= sample_offset &&
183 sample_offset < entry->sample_offset_end) {
184 GST_LOG_OBJECT (wvparse, "found match");
188 /* as the list is sorted and we first look at the latest entry
189 * we can abort searching for an entry if the sample we want is
190 * after the latest one */
191 if (sample_offset >= entry->sample_offset_end)
194 GST_LOG_OBJECT (wvparse, "no match in index");
199 gst_wavpack_parse_index_append_entry (GstWavpackParse * wvparse,
200 gint64 byte_offset, gint64 sample_offset, gint64 num_samples)
202 GstWavpackParseIndexEntry *entry;
204 /* do we have this one already? */
205 if (wvparse->entries) {
206 entry = gst_wavpack_parse_index_get_last_entry (wvparse);
207 if (entry->byte_offset >= byte_offset
208 || entry->sample_offset >= sample_offset)
212 GST_LOG_OBJECT (wvparse, "Adding index entry %8" G_GINT64_FORMAT " - %"
213 GST_TIME_FORMAT " @ offset 0x%08" G_GINT64_MODIFIER "x", sample_offset,
214 GST_TIME_ARGS (gst_util_uint64_scale_int (sample_offset,
215 GST_SECOND, wvparse->samplerate)), byte_offset);
217 entry = gst_wavpack_parse_index_entry_new ();
218 entry->byte_offset = byte_offset;
219 entry->sample_offset = sample_offset;
220 entry->sample_offset_end = sample_offset + num_samples;
221 wvparse->entries = g_slist_prepend (wvparse->entries, entry);
225 gst_wavpack_parse_reset (GstWavpackParse * parse)
227 parse->total_samples = G_GINT64_CONSTANT (-1);
228 parse->samplerate = 0;
231 gst_segment_init (&parse->segment, GST_FORMAT_UNDEFINED);
232 parse->next_block_index = 0;
234 parse->current_offset = 0;
235 parse->need_newsegment = TRUE;
236 parse->discont = TRUE;
237 parse->upstream_length = -1;
239 if (parse->entries) {
240 g_slist_foreach (parse->entries, (GFunc) gst_wavpack_parse_index_entry_free,
242 g_slist_free (parse->entries);
243 parse->entries = NULL;
246 if (parse->adapter) {
247 gst_adapter_clear (parse->adapter);
248 g_object_unref (parse->adapter);
249 parse->adapter = NULL;
252 if (parse->srcpad != NULL) {
255 GST_DEBUG_OBJECT (parse, "Removing src pad");
256 res = gst_element_remove_pad (GST_ELEMENT (parse), parse->srcpad);
257 g_return_if_fail (res != FALSE);
258 gst_object_unref (parse->srcpad);
259 parse->srcpad = NULL;
262 g_list_foreach (parse->queued_events, (GFunc) gst_mini_object_unref, NULL);
263 g_list_free (parse->queued_events);
264 parse->queued_events = NULL;
266 if (parse->pending_buffer)
267 gst_buffer_unref (parse->pending_buffer);
269 parse->pending_buffer = NULL;
272 static const GstQueryType *
273 gst_wavpack_parse_get_src_query_types (GstPad * pad)
275 static const GstQueryType types[] = {
286 gst_wavpack_parse_src_query (GstPad * pad, GstQuery * query)
288 GstWavpackParse *parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
292 gboolean ret = FALSE;
294 switch (GST_QUERY_TYPE (query)) {
295 case GST_QUERY_POSITION:{
300 GST_OBJECT_LOCK (parse);
301 cur = parse->segment.last_stop;
302 rate = parse->samplerate;
303 GST_OBJECT_UNLOCK (parse);
306 GST_DEBUG_OBJECT (parse, "haven't read header yet");
310 gst_query_parse_position (query, &format, NULL);
313 case GST_FORMAT_TIME:
314 cur = gst_util_uint64_scale_int (cur, GST_SECOND, rate);
315 gst_query_set_position (query, GST_FORMAT_TIME, cur);
318 case GST_FORMAT_DEFAULT:
319 gst_query_set_position (query, GST_FORMAT_DEFAULT, cur);
323 GST_DEBUG_OBJECT (parse, "cannot handle position query in "
324 "%s format. Forwarding upstream.", gst_format_get_name (format));
325 ret = gst_pad_query_default (pad, query);
330 case GST_QUERY_DURATION:{
335 GST_OBJECT_LOCK (parse);
336 rate = parse->samplerate;
337 len = parse->total_samples;
338 GST_OBJECT_UNLOCK (parse);
341 GST_DEBUG_OBJECT (parse, "haven't read header yet");
345 gst_query_parse_duration (query, &format, NULL);
348 case GST_FORMAT_TIME:
349 if (len != G_GINT64_CONSTANT (-1))
350 len = gst_util_uint64_scale_int (len, GST_SECOND, rate);
351 gst_query_set_duration (query, GST_FORMAT_TIME, len);
354 case GST_FORMAT_DEFAULT:
355 gst_query_set_duration (query, GST_FORMAT_DEFAULT, len);
359 GST_DEBUG_OBJECT (parse, "cannot handle duration query in "
360 "%s format. Forwarding upstream.", gst_format_get_name (format));
361 ret = gst_pad_query_default (pad, query);
366 case GST_QUERY_SEEKING:{
367 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
368 if (format == GST_FORMAT_TIME || format == GST_FORMAT_DEFAULT) {
371 gint64 duration = -1;
373 /* only fails if we didn't read the headers yet and can't say
374 * anything about our seeking capabilities */
375 if (!gst_pad_query_duration (pad, &format, &duration))
378 /* can't seek in streaming mode yet */
379 GST_OBJECT_LOCK (parse);
380 seekable = (parse->adapter == NULL);
381 GST_OBJECT_UNLOCK (parse);
383 gst_query_set_seeking (query, format, seekable, 0, duration);
389 ret = gst_pad_query_default (pad, query);
394 gst_object_unref (parse);
399 /* returns TRUE on success, with byte_offset set to the offset of the
400 * wavpack chunk containing the sample requested. start_sample will be
401 * set to the first sample in the chunk starting at byte_offset.
402 * Scanning from the last known header offset to the wanted position
403 * when seeking forward isn't very clever, but seems fast enough in
404 * practice and has the nice side effect of populating our index
407 gst_wavpack_parse_scan_to_find_sample (GstWavpackParse * parse,
408 gint64 sample, gint64 * byte_offset, gint64 * start_sample)
410 GstWavpackParseIndexEntry *entry;
416 /* first, check if we have to scan at all */
417 entry = gst_wavpack_parse_index_get_entry_from_sample (parse, sample);
419 *byte_offset = entry->byte_offset;
420 *start_sample = entry->sample_offset;
421 GST_LOG_OBJECT (parse, "Found index entry: sample %" G_GINT64_FORMAT
422 " @ offset %" G_GINT64_FORMAT, entry->sample_offset,
427 GST_LOG_OBJECT (parse, "No matching entry in index, scanning file ...");
429 /* if we have an index, we can start scanning from the last known offset
430 * in there, after all we know our wanted sample is not in the index */
431 if (parse->entries) {
432 GstWavpackParseIndexEntry *entry;
434 entry = gst_wavpack_parse_index_get_last_entry (parse);
435 off = entry->byte_offset;
438 /* now scan forward until we find the chunk we're looking for or hit EOS */
440 WavpackHeader header;
444 buf = gst_wavpack_parse_pull_buffer (parse, off, sizeof (WavpackHeader),
450 gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
451 gst_buffer_unref (buf);
453 if (header.flags & INITIAL_BLOCK)
454 gst_wavpack_parse_index_append_entry (parse, off, header.block_index,
455 header.block_samples);
459 if (header.block_index <= sample &&
460 sample < (header.block_index + header.block_samples)) {
462 *start_sample = header.block_index;
466 off += header.ckSize + 8;
469 GST_DEBUG_OBJECT (parse, "scan failed: %s (off=0x%08" G_GINT64_MODIFIER "x)",
470 gst_flow_get_name (ret), off);
476 gst_wavpack_parse_send_newsegment (GstWavpackParse * wvparse, gboolean update)
478 GstSegment *s = &wvparse->segment;
482 gint64 stop_time = -1;
484 gint64 start_time = 0;
490 /* segment is in DEFAULT format, but we want to send a TIME newsegment */
491 start_time = gst_util_uint64_scale_int (s->start, GST_SECOND,
492 wvparse->samplerate);
495 stop_time = gst_util_uint64_scale_int (s->stop, GST_SECOND,
496 wvparse->samplerate);
499 GST_DEBUG_OBJECT (wvparse, "sending newsegment from %" GST_TIME_FORMAT
500 " to %" GST_TIME_FORMAT, GST_TIME_ARGS (start_time),
501 GST_TIME_ARGS (stop_time));
503 /* after a seek, s->last_stop will point to a chunk boundary, ie. from
504 * which sample we will start sending data again, while s->start will
505 * point to the sample we actually want to seek to and want to start
506 * playing right after the seek. Adjust clock-time for the difference
507 * so we start playing from start_time */
508 cur_pos_time = gst_util_uint64_scale_int (s->last_stop, GST_SECOND,
509 wvparse->samplerate);
510 diff = start_time - cur_pos_time;
512 ret = gst_pad_push_event (wvparse->srcpad,
513 gst_event_new_new_segment (update, s->rate, GST_FORMAT_TIME,
514 start_time, stop_time, start_time - diff));
520 gst_wavpack_parse_handle_seek_event (GstWavpackParse * wvparse,
523 GstSeekFlags seek_flags;
525 GstSeekType start_type;
527 GstSeekType stop_type;
533 gboolean only_update;
541 gint64 start; /* sample we want to seek to */
543 gint64 byte_offset; /* byte offset the chunk we seek to starts at */
545 gint64 chunk_start; /* first sample in chunk we seek to */
551 if (wvparse->adapter) {
552 GST_DEBUG_OBJECT (wvparse, "seeking in streaming mode not implemented yet");
556 gst_event_parse_seek (event, &speed, &format, &seek_flags, &start_type,
557 &start, &stop_type, &stop);
559 if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME) {
560 GST_DEBUG ("seeking is only supported in TIME or DEFAULT format");
565 GST_DEBUG ("only forward playback supported, rate %f not allowed", speed);
569 GST_OBJECT_LOCK (wvparse);
571 rate = wvparse->samplerate;
573 GST_OBJECT_UNLOCK (wvparse);
574 GST_DEBUG ("haven't read header yet");
578 /* figure out the last position we need to play. If it's configured (stop !=
579 * -1), use that, else we play until the total duration of the file */
581 stop = wvparse->segment.duration;
583 /* convert from time to samples if necessary */
584 if (format == GST_FORMAT_TIME) {
585 if (start_type != GST_SEEK_TYPE_NONE)
586 start = gst_util_uint64_scale_int (start, rate, GST_SECOND);
587 if (stop_type != GST_SEEK_TYPE_NONE)
588 stop = gst_util_uint64_scale_int (stop, rate, GST_SECOND);
592 GST_OBJECT_UNLOCK (wvparse);
593 GST_DEBUG_OBJECT (wvparse, "Invalid start sample %" G_GINT64_FORMAT, start);
597 flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
599 /* operate on segment copy until we know the seek worked */
600 segment = wvparse->segment;
602 gst_segment_set_seek (&segment, speed, GST_FORMAT_DEFAULT,
603 seek_flags, start_type, start, stop_type, stop, &only_update);
607 wvparse->segment = segment;
608 gst_wavpack_parse_send_newsegment (wvparse, TRUE);
613 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_start ());
616 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_start ());
618 gst_pad_pause_task (wvparse->sinkpad);
621 GST_PAD_STREAM_LOCK (wvparse->sinkpad);
623 /* Save current position */
624 last_stop = wvparse->segment.last_stop;
626 gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_stop ());
629 gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_stop ());
632 GST_DEBUG_OBJECT (wvparse, "Performing seek to %" GST_TIME_FORMAT " sample %"
633 G_GINT64_FORMAT, GST_TIME_ARGS (segment.start * GST_SECOND / rate),
636 ret = gst_wavpack_parse_scan_to_find_sample (wvparse, segment.start,
637 &byte_offset, &chunk_start);
640 GST_DEBUG_OBJECT (wvparse, "new offset: %" G_GINT64_FORMAT, byte_offset);
641 wvparse->current_offset = byte_offset;
642 /* we want to send a newsegment event with the actual seek position
643 * as start, even though our first buffer might start before the
644 * configured segment. We leave it up to the decoder or sink to crop
645 * the output buffers accordingly */
646 wvparse->segment = segment;
647 wvparse->segment.last_stop = chunk_start;
648 wvparse->need_newsegment = TRUE;
649 wvparse->discont = (last_stop != chunk_start) ? TRUE : FALSE;
651 /* if we're doing a segment seek, post a SEGMENT_START message */
652 if (wvparse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
653 gst_element_post_message (GST_ELEMENT_CAST (wvparse),
654 gst_message_new_segment_start (GST_OBJECT_CAST (wvparse),
655 wvparse->segment.format, wvparse->segment.last_stop));
658 GST_DEBUG_OBJECT (wvparse, "seek failed: don't know where to seek to");
661 GST_PAD_STREAM_UNLOCK (wvparse->sinkpad);
662 GST_OBJECT_UNLOCK (wvparse);
664 gst_pad_start_task (wvparse->sinkpad,
665 (GstTaskFunction) gst_wavpack_parse_loop, wvparse);
671 gst_wavpack_parse_sink_event (GstPad * pad, GstEvent * event)
673 GstWavpackParse *parse;
677 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
679 switch (GST_EVENT_TYPE (event)) {
680 case GST_EVENT_FLUSH_STOP:{
681 if (parse->adapter) {
682 gst_adapter_clear (parse->adapter);
684 if (parse->pending_buffer) {
685 gst_buffer_unref (parse->pending_buffer);
686 parse->pending_buffer = NULL;
687 parse->pending_offset = 0;
689 ret = gst_pad_push_event (parse->srcpad, event);
692 case GST_EVENT_NEWSEGMENT:{
693 parse->need_newsegment = TRUE;
694 gst_event_unref (event);
699 if (parse->adapter) {
700 /* remove all bytes that are left in the adapter after EOS. They can't
701 * be a complete Wavpack block and we can't do anything with them */
702 gst_adapter_clear (parse->adapter);
704 if (parse->pending_buffer) {
705 gst_buffer_unref (parse->pending_buffer);
706 parse->pending_buffer = NULL;
707 parse->pending_offset = 0;
709 ret = gst_pad_push_event (parse->srcpad, event);
713 /* stream lock is recursive, should be fine for all events */
714 GST_PAD_STREAM_LOCK (pad);
715 if (parse->srcpad == NULL) {
716 parse->queued_events = g_list_append (parse->queued_events, event);
718 ret = gst_pad_push_event (parse->srcpad, event);
720 GST_PAD_STREAM_UNLOCK (pad);
725 gst_object_unref (parse);
730 gst_wavpack_parse_src_event (GstPad * pad, GstEvent * event)
732 GstWavpackParse *parse;
736 parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
738 switch (GST_EVENT_TYPE (event)) {
740 ret = gst_wavpack_parse_handle_seek_event (parse, event);
743 ret = gst_pad_event_default (pad, event);
747 gst_object_unref (parse);
752 gst_wavpack_parse_init (GstWavpackParse * parse, GstWavpackParseClass * gclass)
754 GstElementClass *klass = GST_ELEMENT_GET_CLASS (parse);
756 GstPadTemplate *tmpl;
758 tmpl = gst_element_class_get_pad_template (klass, "sink");
759 parse->sinkpad = gst_pad_new_from_template (tmpl, "sink");
761 gst_pad_set_activate_function (parse->sinkpad,
762 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate));
763 gst_pad_set_activatepull_function (parse->sinkpad,
764 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate_pull));
765 gst_pad_set_event_function (parse->sinkpad,
766 GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_event));
767 gst_pad_set_chain_function (parse->sinkpad,
768 GST_DEBUG_FUNCPTR (gst_wavpack_parse_chain));
770 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
772 parse->srcpad = NULL;
773 gst_wavpack_parse_reset (parse);
777 gst_wavpack_parse_get_upstream_length (GstWavpackParse * parse)
781 GstFormat format = GST_FORMAT_BYTES;
783 if (!gst_pad_query_peer_duration (parse->sinkpad, &format, &length)) {
786 GST_DEBUG ("upstream length: %" G_GINT64_FORMAT, length);
792 gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse, gint64 offset,
793 guint size, GstFlowReturn * flow)
795 GstFlowReturn flow_ret;
797 GstBuffer *buf = NULL;
799 if (offset + size > wvparse->upstream_length) {
800 wvparse->upstream_length = gst_wavpack_parse_get_upstream_length (wvparse);
801 if (offset + size > wvparse->upstream_length) {
802 GST_DEBUG_OBJECT (wvparse, "EOS: %" G_GINT64_FORMAT " + %u > %"
803 G_GINT64_FORMAT, offset, size, wvparse->upstream_length);
804 flow_ret = GST_FLOW_UNEXPECTED;
809 flow_ret = gst_pad_pull_range (wvparse->sinkpad, offset, size, &buf);
811 if (flow_ret != GST_FLOW_OK) {
812 GST_DEBUG_OBJECT (wvparse, "pull_range (%" G_GINT64_FORMAT ", %u) "
813 "failed, flow: %s", offset, size, gst_flow_get_name (flow_ret));
818 if (GST_BUFFER_SIZE (buf) < size) {
819 GST_DEBUG_OBJECT (wvparse, "Short read at offset %" G_GINT64_FORMAT
820 ", got only %u of %u bytes", offset, GST_BUFFER_SIZE (buf), size);
821 gst_buffer_unref (buf);
823 flow_ret = GST_FLOW_UNEXPECTED;
833 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
834 WavpackHeader * header)
836 GstWavpackMetadata meta;
838 GstCaps *caps = NULL;
842 g_assert (wvparse->srcpad == NULL);
844 bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
846 while (gst_wavpack_read_metadata (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
848 case ID_WVC_BITSTREAM:{
849 caps = gst_caps_new_simple ("audio/x-wavpack-correction",
850 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
852 gst_pad_new_from_template (gst_element_class_get_pad_template
853 (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
856 case ID_WV_BITSTREAM:
857 case ID_WVX_BITSTREAM:{
858 WavpackStreamReader *stream_reader = gst_wavpack_stream_reader_new ();
868 rid.buffer = GST_BUFFER_DATA (buf);
869 rid.length = GST_BUFFER_SIZE (buf);
873 WavpackOpenFileInputEx (stream_reader, &rid, NULL, error_msg, 0, 0);
878 wvparse->samplerate = WavpackGetSampleRate (wpc);
879 wvparse->channels = WavpackGetNumChannels (wpc);
880 wvparse->total_samples =
881 (header->total_samples ==
882 0xffffffff) ? G_GINT64_CONSTANT (-1) : header->total_samples;
884 caps = gst_caps_new_simple ("audio/x-wavpack",
885 "width", G_TYPE_INT, WavpackGetBitsPerSample (wpc),
886 "channels", G_TYPE_INT, wvparse->channels,
887 "rate", G_TYPE_INT, wvparse->samplerate,
888 "framed", G_TYPE_BOOLEAN, TRUE, NULL);
889 #ifdef WAVPACK_OLD_API
890 channel_mask = wpc->config.channel_mask;
892 channel_mask = WavpackGetChannelMask (wpc);
894 if (channel_mask == 0)
896 gst_wavpack_get_default_channel_mask (wvparse->channels);
898 if (channel_mask != 0) {
899 if (!gst_wavpack_set_channel_layout (caps, channel_mask)) {
900 GST_WARNING_OBJECT (wvparse, "Failed to set channel layout");
901 gst_caps_unref (caps);
903 WavpackCloseFile (wpc);
904 g_free (stream_reader);
910 gst_pad_new_from_template (gst_element_class_get_pad_template
911 (GST_ELEMENT_GET_CLASS (wvparse), "src"), "src");
912 WavpackCloseFile (wpc);
913 g_free (stream_reader);
917 GST_LOG_OBJECT (wvparse, "unhandled ID: 0x%02x", meta.id);
925 if (caps == NULL || wvparse->srcpad == NULL)
928 GST_DEBUG_OBJECT (wvparse, "Added src pad with caps %" GST_PTR_FORMAT, caps);
930 gst_pad_set_query_function (wvparse->srcpad,
931 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_query));
932 gst_pad_set_query_type_function (wvparse->srcpad,
933 GST_DEBUG_FUNCPTR (gst_wavpack_parse_get_src_query_types));
934 gst_pad_set_event_function (wvparse->srcpad,
935 GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_event));
937 gst_pad_set_caps (wvparse->srcpad, caps);
938 gst_caps_unref (caps);
939 gst_pad_use_fixed_caps (wvparse->srcpad);
941 gst_object_ref (wvparse->srcpad);
942 gst_pad_set_active (wvparse->srcpad, TRUE);
943 gst_element_add_pad (GST_ELEMENT (wvparse), wvparse->srcpad);
944 gst_element_no_more_pads (GST_ELEMENT (wvparse));
950 gst_wavpack_parse_push_buffer (GstWavpackParse * wvparse, GstBuffer * buf,
951 WavpackHeader * header)
954 wvparse->current_offset += header->ckSize + 8;
956 wvparse->segment.last_stop = header->block_index;
958 if (wvparse->need_newsegment) {
959 if (gst_wavpack_parse_send_newsegment (wvparse, FALSE))
960 wvparse->need_newsegment = FALSE;
963 /* send any queued events */
964 if (wvparse->queued_events) {
967 for (l = wvparse->queued_events; l != NULL; l = l->next) {
968 gst_pad_push_event (wvparse->srcpad, GST_EVENT (l->data));
970 g_list_free (wvparse->queued_events);
971 wvparse->queued_events = NULL;
974 if (wvparse->pending_buffer == NULL) {
975 wvparse->pending_buffer = buf;
976 wvparse->pending_offset = header->block_index;
977 } else if (wvparse->pending_offset == header->block_index) {
978 wvparse->pending_buffer = gst_buffer_join (wvparse->pending_buffer, buf);
980 GST_ERROR ("Got incomplete block, dropping");
981 gst_buffer_unref (wvparse->pending_buffer);
982 wvparse->pending_buffer = buf;
983 wvparse->pending_offset = header->block_index;
986 if (!(header->flags & FINAL_BLOCK))
989 buf = wvparse->pending_buffer;
990 wvparse->pending_buffer = NULL;
992 GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (header->block_index,
993 GST_SECOND, wvparse->samplerate);
994 GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (header->block_samples,
995 GST_SECOND, wvparse->samplerate);
996 GST_BUFFER_OFFSET (buf) = header->block_index;
997 GST_BUFFER_OFFSET_END (buf) = header->block_index + header->block_samples;
999 if (wvparse->discont || wvparse->next_block_index != header->block_index) {
1000 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1001 wvparse->discont = FALSE;
1004 wvparse->next_block_index = header->block_index + header->block_samples;
1006 gst_buffer_set_caps (buf, GST_PAD_CAPS (wvparse->srcpad));
1008 GST_LOG_OBJECT (wvparse, "Pushing buffer with time %" GST_TIME_FORMAT,
1009 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1011 ret = gst_pad_push (wvparse->srcpad, buf);
1013 wvparse->segment.last_stop = wvparse->next_block_index;
1019 gst_wavpack_parse_find_marker (guint8 * buf, guint size)
1025 if (G_UNLIKELY (size < 4))
1028 for (i = 0; i < size - 4; i++) {
1029 if (memcmp (buf + i, "wvpk", 4) == 0) {
1037 static GstFlowReturn
1038 gst_wavpack_parse_resync_loop (GstWavpackParse * parse, WavpackHeader * header)
1040 GstFlowReturn flow_ret = GST_FLOW_UNEXPECTED;
1042 GstBuffer *buf = NULL;
1044 /* loop until we have a frame header or reach the end of the stream */
1046 guint8 *data, *marker;
1051 gst_buffer_unref (buf);
1055 if (parse->upstream_length == 0 ||
1056 parse->upstream_length <= parse->current_offset) {
1057 parse->upstream_length = gst_wavpack_parse_get_upstream_length (parse);
1058 if (parse->upstream_length == 0 ||
1059 parse->upstream_length <= parse->current_offset) {
1064 len = MIN (parse->upstream_length - parse->current_offset, 2048);
1066 GST_LOG_OBJECT (parse, "offset: %" G_GINT64_FORMAT, parse->current_offset);
1068 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1071 /* whatever the problem is, there's nothing more for us to do for now */
1072 if (flow_ret != GST_FLOW_OK)
1075 data = GST_BUFFER_DATA (buf);
1076 size = GST_BUFFER_SIZE (buf);
1078 /* not enough data for a header? */
1079 if (size < sizeof (WavpackHeader))
1082 /* got a header right where we are at now? */
1083 if (gst_wavpack_read_header (header, data))
1086 /* nope, let's see if we can find one */
1087 marker = gst_wavpack_parse_find_marker (data + 1, size - 1);
1090 parse->current_offset += marker - data;
1091 /* do one more loop iteration to make sure we pull enough
1092 * data for a full header, we'll bail out then */
1094 parse->current_offset += len - 4;
1099 gst_buffer_unref (buf);
1105 gst_wavpack_parse_loop (GstElement * element)
1107 GstWavpackParse *parse = GST_WAVPACK_PARSE (element);
1109 GstFlowReturn flow_ret;
1110 WavpackHeader header = { {0,}, 0, };
1111 GstBuffer *buf = NULL;
1113 flow_ret = gst_wavpack_parse_resync_loop (parse, &header);
1115 if (flow_ret != GST_FLOW_OK)
1118 GST_LOG_OBJECT (parse, "Read header at offset %" G_GINT64_FORMAT
1119 ": chunk size = %u+8", parse->current_offset, header.ckSize);
1121 buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1122 header.ckSize + 8, &flow_ret);
1124 if (flow_ret != GST_FLOW_OK)
1127 if (parse->srcpad == NULL) {
1128 if (!gst_wavpack_parse_create_src_pad (parse, buf, &header)) {
1129 GST_ERROR_OBJECT (parse, "Failed to create src pad");
1130 flow_ret = GST_FLOW_ERROR;
1134 if (header.flags & INITIAL_BLOCK)
1135 gst_wavpack_parse_index_append_entry (parse, parse->current_offset,
1136 header.block_index, header.block_samples);
1138 flow_ret = gst_wavpack_parse_push_buffer (parse, buf, &header);
1139 if (flow_ret != GST_FLOW_OK)
1146 const gchar *reason = gst_flow_get_name (flow_ret);
1148 GST_LOG_OBJECT (parse, "pausing task, reason %s", reason);
1149 gst_pad_pause_task (parse->sinkpad);
1151 if (flow_ret == GST_FLOW_UNEXPECTED && parse->srcpad) {
1152 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1155 GST_LOG_OBJECT (parse, "Sending segment done");
1157 if ((stop = parse->segment.stop) == -1)
1158 stop = parse->segment.duration;
1160 gst_element_post_message (GST_ELEMENT_CAST (parse),
1161 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
1162 parse->segment.format, stop));
1164 GST_LOG_OBJECT (parse, "Sending EOS, at end of stream");
1165 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1167 } else if (flow_ret == GST_FLOW_NOT_LINKED
1168 || flow_ret < GST_FLOW_UNEXPECTED) {
1169 GST_ELEMENT_ERROR (parse, STREAM, FAILED,
1170 (_("Internal data stream error.")), ("stream stopped, reason %s",
1173 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1180 gst_wavpack_parse_resync_adapter (GstAdapter * adapter)
1182 const guint8 *buf, *marker;
1184 guint avail = gst_adapter_available (adapter);
1189 /* if the marker is at the beginning don't do the expensive search */
1190 buf = gst_adapter_peek (adapter, 4);
1191 if (memcmp (buf, "wvpk", 4) == 0)
1197 /* search for the marker in the complete content of the adapter */
1198 buf = gst_adapter_peek (adapter, avail);
1199 if (buf && (marker = gst_wavpack_parse_find_marker ((guint8 *) buf, avail))) {
1200 gst_adapter_flush (adapter, marker - buf);
1204 /* flush everything except the last 4 bytes. they could contain
1205 * the start of a new marker */
1206 gst_adapter_flush (adapter, avail - 4);
1211 static GstFlowReturn
1212 gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf)
1214 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (GST_PAD_PARENT (pad));
1216 GstFlowReturn ret = GST_FLOW_OK;
1220 const guint8 *tmp_buf;
1222 if (!wvparse->adapter) {
1223 wvparse->adapter = gst_adapter_new ();
1226 if (GST_BUFFER_IS_DISCONT (buf)) {
1227 gst_adapter_clear (wvparse->adapter);
1228 wvparse->discont = TRUE;
1231 gst_adapter_push (wvparse->adapter, buf);
1233 if (gst_adapter_available (wvparse->adapter) < sizeof (WavpackHeader))
1236 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1239 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1240 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1242 while (gst_adapter_available (wvparse->adapter) >= wph.ckSize + 4 * 1 + 4) {
1244 gst_adapter_take_buffer (wvparse->adapter, wph.ckSize + 4 * 1 + 4);
1247 return GST_FLOW_ERROR;
1249 if (wvparse->srcpad == NULL) {
1250 if (!gst_wavpack_parse_create_src_pad (wvparse, outbuf, &wph)) {
1251 GST_ERROR_OBJECT (wvparse, "Failed to create src pad");
1252 ret = GST_FLOW_ERROR;
1257 ret = gst_wavpack_parse_push_buffer (wvparse, outbuf, &wph);
1259 if (ret != GST_FLOW_OK)
1262 if (gst_adapter_available (wvparse->adapter) >= sizeof (WavpackHeader)) {
1263 tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1265 if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1268 gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1275 static GstStateChangeReturn
1276 gst_wavpack_parse_change_state (GstElement * element, GstStateChange transition)
1278 GstWavpackParse *wvparse = GST_WAVPACK_PARSE (element);
1280 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1282 switch (transition) {
1283 case GST_STATE_CHANGE_READY_TO_PAUSED:
1284 gst_segment_init (&wvparse->segment, GST_FORMAT_DEFAULT);
1285 wvparse->segment.last_stop = 0;
1290 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1291 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1293 switch (transition) {
1294 case GST_STATE_CHANGE_PAUSED_TO_READY:
1295 gst_wavpack_parse_reset (wvparse);
1305 gst_wavpack_parse_sink_activate (GstPad * sinkpad)
1307 if (gst_pad_check_pull_range (sinkpad)) {
1308 return gst_pad_activate_pull (sinkpad, TRUE);
1310 return gst_pad_activate_push (sinkpad, TRUE);
1315 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
1320 result = gst_pad_start_task (sinkpad,
1321 (GstTaskFunction) gst_wavpack_parse_loop, GST_PAD_PARENT (sinkpad));
1323 result = gst_pad_stop_task (sinkpad);
1330 gst_wavpack_parse_plugin_init (GstPlugin * plugin)
1332 if (!gst_element_register (plugin, "wavpackparse",
1333 GST_RANK_PRIMARY, GST_TYPE_WAVPACK_PARSE)) {
1337 GST_DEBUG_CATEGORY_INIT (gst_wavpack_parse_debug, "wavpack_parse", 0,
1338 "Wavpack file parser");