gst_element_class_set_details => gst_element_class_set_details_simple
[platform/upstream/gst-plugins-good.git] / ext / wavpack / gstwavpackparse.c
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>
5  *
6  * gstwavpackparse.c: wavpack file parser
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:element-wavpackparse
26  *
27  * WavpackParse takes raw, unframed Wavpack streams and splits them into
28  * single Wavpack chunks with information like bit depth and the position
29  * in the stream.
30  * <ulink url="http://www.wavpack.com/">Wavpack</ulink> is an open-source
31  * audio codec that features both lossless and lossy encoding.
32  *
33  * <refsect2>
34  * <title>Example launch line</title>
35  * |[
36  * gst-launch filesrc location=test.wv ! wavpackparse ! wavpackdec ! fakesink
37  * ]| This pipeline decodes the Wavpack file test.wv into raw audio buffers.
38  * </refsect2>
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 #include <gst/gst.h>
45 #include <gst/gst-i18n-plugin.h>
46
47 #include <math.h>
48 #include <string.h>
49
50 #include <wavpack/wavpack.h>
51 #include "gstwavpackparse.h"
52 #include "gstwavpackstreamreader.h"
53 #include "gstwavpackcommon.h"
54
55 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_parse_debug);
56 #define GST_CAT_DEFAULT gst_wavpack_parse_debug
57
58 static inline GstWavpackParseIndexEntry *
59 gst_wavpack_parse_index_entry_new ()
60 {
61   return g_slice_new (GstWavpackParseIndexEntry);
62 }
63
64 static inline void
65 gst_wavpack_parse_index_entry_free (GstWavpackParseIndexEntry * entry)
66 {
67   g_slice_free (GstWavpackParseIndexEntry, entry);
68 }
69
70 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("audio/x-wavpack, "
74         "framed = (boolean) false; "
75         "audio/x-wavpack-correction, " "framed = (boolean) false")
76     );
77
78 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
79     GST_PAD_SRC,
80     GST_PAD_SOMETIMES,
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")
85     );
86
87 static GstStaticPadTemplate wvc_src_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
88     GST_PAD_SRC,
89     GST_PAD_SOMETIMES,
90     GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) true")
91     );
92
93 static gboolean gst_wavpack_parse_sink_activate (GstPad * sinkpad);
94
95 static gboolean
96 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active);
97
98 static void gst_wavpack_parse_loop (GstElement * element);
99
100 static GstStateChangeReturn gst_wavpack_parse_change_state (GstElement *
101     element, GstStateChange transition);
102 static void gst_wavpack_parse_reset (GstWavpackParse * parse);
103
104 static gint64 gst_wavpack_parse_get_upstream_length (GstWavpackParse * wvparse);
105
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);
109
110 GST_BOILERPLATE (GstWavpackParse, gst_wavpack_parse, GstElement,
111     GST_TYPE_ELEMENT);
112
113 static void
114 gst_wavpack_parse_base_init (gpointer klass)
115 {
116   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
117
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));
124
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>");
130 }
131
132 static void
133 gst_wavpack_parse_finalize (GObject * object)
134 {
135   gst_wavpack_parse_reset (GST_WAVPACK_PARSE (object));
136
137   G_OBJECT_CLASS (parent_class)->finalize (object);
138 }
139
140 static void
141 gst_wavpack_parse_class_init (GstWavpackParseClass * klass)
142 {
143   GObjectClass *gobject_class;
144
145   GstElementClass *gstelement_class;
146
147   gobject_class = (GObjectClass *) klass;
148   gstelement_class = (GstElementClass *) klass;
149
150   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wavpack_parse_finalize);
151   gstelement_class->change_state =
152       GST_DEBUG_FUNCPTR (gst_wavpack_parse_change_state);
153 }
154
155 static GstWavpackParseIndexEntry *
156 gst_wavpack_parse_index_get_last_entry (GstWavpackParse * wvparse)
157 {
158   g_assert (wvparse->entries != NULL);
159
160   return wvparse->entries->data;
161 }
162
163 static GstWavpackParseIndexEntry *
164 gst_wavpack_parse_index_get_entry_from_sample (GstWavpackParse * wvparse,
165     gint64 sample_offset)
166 {
167   gint i;
168
169   GSList *node;
170
171   if (wvparse->entries == NULL)
172     return NULL;
173
174   for (node = wvparse->entries, i = 0; node; node = node->next, i++) {
175     GstWavpackParseIndexEntry *entry;
176
177     entry = node->data;
178
179     GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
180         " byte %" G_GINT64_FORMAT, i, entry->sample_offset, entry->byte_offset);
181
182     if (entry->sample_offset <= sample_offset &&
183         sample_offset < entry->sample_offset_end) {
184       GST_LOG_OBJECT (wvparse, "found match");
185       return entry;
186     }
187
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)
192       break;
193   }
194   GST_LOG_OBJECT (wvparse, "no match in index");
195   return NULL;
196 }
197
198 static void
199 gst_wavpack_parse_index_append_entry (GstWavpackParse * wvparse,
200     gint64 byte_offset, gint64 sample_offset, gint64 num_samples)
201 {
202   GstWavpackParseIndexEntry *entry;
203
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)
209       return;
210   }
211
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);
216
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);
222 }
223
224 static void
225 gst_wavpack_parse_reset (GstWavpackParse * parse)
226 {
227   parse->total_samples = G_GINT64_CONSTANT (-1);
228   parse->samplerate = 0;
229   parse->channels = 0;
230
231   gst_segment_init (&parse->segment, GST_FORMAT_UNDEFINED);
232   parse->next_block_index = 0;
233
234   parse->current_offset = 0;
235   parse->need_newsegment = TRUE;
236   parse->discont = TRUE;
237   parse->upstream_length = -1;
238
239   if (parse->entries) {
240     g_slist_foreach (parse->entries, (GFunc) gst_wavpack_parse_index_entry_free,
241         NULL);
242     g_slist_free (parse->entries);
243     parse->entries = NULL;
244   }
245
246   if (parse->adapter) {
247     gst_adapter_clear (parse->adapter);
248     g_object_unref (parse->adapter);
249     parse->adapter = NULL;
250   }
251
252   if (parse->srcpad != NULL) {
253     gboolean res;
254
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;
260   }
261
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;
265
266   if (parse->pending_buffer)
267     gst_buffer_unref (parse->pending_buffer);
268
269   parse->pending_buffer = NULL;
270 }
271
272 static const GstQueryType *
273 gst_wavpack_parse_get_src_query_types (GstPad * pad)
274 {
275   static const GstQueryType types[] = {
276     GST_QUERY_POSITION,
277     GST_QUERY_DURATION,
278     GST_QUERY_SEEKING,
279     0
280   };
281
282   return types;
283 }
284
285 static gboolean
286 gst_wavpack_parse_src_query (GstPad * pad, GstQuery * query)
287 {
288   GstWavpackParse *parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
289
290   GstFormat format;
291
292   gboolean ret = FALSE;
293
294   switch (GST_QUERY_TYPE (query)) {
295     case GST_QUERY_POSITION:{
296       gint64 cur;
297
298       guint rate;
299
300       GST_OBJECT_LOCK (parse);
301       cur = parse->segment.last_stop;
302       rate = parse->samplerate;
303       GST_OBJECT_UNLOCK (parse);
304
305       if (rate == 0) {
306         GST_DEBUG_OBJECT (parse, "haven't read header yet");
307         break;
308       }
309
310       gst_query_parse_position (query, &format, NULL);
311
312       switch (format) {
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);
316           ret = TRUE;
317           break;
318         case GST_FORMAT_DEFAULT:
319           gst_query_set_position (query, GST_FORMAT_DEFAULT, cur);
320           ret = TRUE;
321           break;
322         default:
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);
326           break;
327       }
328       break;
329     }
330     case GST_QUERY_DURATION:{
331       gint64 len;
332
333       guint rate;
334
335       GST_OBJECT_LOCK (parse);
336       rate = parse->samplerate;
337       len = parse->total_samples;
338       GST_OBJECT_UNLOCK (parse);
339
340       if (rate == 0) {
341         GST_DEBUG_OBJECT (parse, "haven't read header yet");
342         break;
343       }
344
345       gst_query_parse_duration (query, &format, NULL);
346
347       switch (format) {
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);
352           ret = TRUE;
353           break;
354         case GST_FORMAT_DEFAULT:
355           gst_query_set_duration (query, GST_FORMAT_DEFAULT, len);
356           ret = TRUE;
357           break;
358         default:
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);
362           break;
363       }
364       break;
365     }
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) {
369         gboolean seekable;
370
371         gint64 duration = -1;
372
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))
376           break;
377
378         /* can't seek in streaming mode yet */
379         GST_OBJECT_LOCK (parse);
380         seekable = (parse->adapter == NULL);
381         GST_OBJECT_UNLOCK (parse);
382
383         gst_query_set_seeking (query, format, seekable, 0, duration);
384         ret = TRUE;
385       }
386       break;
387     }
388     default:{
389       ret = gst_pad_query_default (pad, query);
390       break;
391     }
392   }
393
394   gst_object_unref (parse);
395   return ret;
396
397 }
398
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
405  * table */
406 static gboolean
407 gst_wavpack_parse_scan_to_find_sample (GstWavpackParse * parse,
408     gint64 sample, gint64 * byte_offset, gint64 * start_sample)
409 {
410   GstWavpackParseIndexEntry *entry;
411
412   GstFlowReturn ret;
413
414   gint64 off = 0;
415
416   /* first, check if we have to scan at all */
417   entry = gst_wavpack_parse_index_get_entry_from_sample (parse, sample);
418   if (entry) {
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,
423         entry->byte_offset);
424     return TRUE;
425   }
426
427   GST_LOG_OBJECT (parse, "No matching entry in index, scanning file ...");
428
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;
433
434     entry = gst_wavpack_parse_index_get_last_entry (parse);
435     off = entry->byte_offset;
436   }
437
438   /* now scan forward until we find the chunk we're looking for or hit EOS */
439   do {
440     WavpackHeader header;
441
442     GstBuffer *buf;
443
444     buf = gst_wavpack_parse_pull_buffer (parse, off, sizeof (WavpackHeader),
445         &ret);
446
447     if (buf == NULL)
448       break;
449
450     gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
451     gst_buffer_unref (buf);
452
453     if (header.flags & INITIAL_BLOCK)
454       gst_wavpack_parse_index_append_entry (parse, off, header.block_index,
455           header.block_samples);
456     else
457       continue;
458
459     if (header.block_index <= sample &&
460         sample < (header.block_index + header.block_samples)) {
461       *byte_offset = off;
462       *start_sample = header.block_index;
463       return TRUE;
464     }
465
466     off += header.ckSize + 8;
467   } while (1);
468
469   GST_DEBUG_OBJECT (parse, "scan failed: %s (off=0x%08" G_GINT64_MODIFIER "x)",
470       gst_flow_get_name (ret), off);
471
472   return FALSE;
473 }
474
475 static gboolean
476 gst_wavpack_parse_send_newsegment (GstWavpackParse * wvparse, gboolean update)
477 {
478   GstSegment *s = &wvparse->segment;
479
480   gboolean ret;
481
482   gint64 stop_time = -1;
483
484   gint64 start_time = 0;
485
486   gint64 cur_pos_time;
487
488   gint64 diff;
489
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);
493
494   if (s->stop != -1) {
495     stop_time = gst_util_uint64_scale_int (s->stop, GST_SECOND,
496         wvparse->samplerate);
497   }
498
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));
502
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;
511
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));
515
516   return ret;
517 }
518
519 static gboolean
520 gst_wavpack_parse_handle_seek_event (GstWavpackParse * wvparse,
521     GstEvent * event)
522 {
523   GstSeekFlags seek_flags;
524
525   GstSeekType start_type;
526
527   GstSeekType stop_type;
528
529   GstSegment segment;
530
531   GstFormat format;
532
533   gboolean only_update;
534
535   gboolean flush, ret;
536
537   gdouble speed;
538
539   gint64 stop;
540
541   gint64 start;                 /* sample we want to seek to                  */
542
543   gint64 byte_offset;           /* byte offset the chunk we seek to starts at */
544
545   gint64 chunk_start;           /* first sample in chunk we seek to           */
546
547   guint rate;
548
549   gint64 last_stop;
550
551   if (wvparse->adapter) {
552     GST_DEBUG_OBJECT (wvparse, "seeking in streaming mode not implemented yet");
553     return FALSE;
554   }
555
556   gst_event_parse_seek (event, &speed, &format, &seek_flags, &start_type,
557       &start, &stop_type, &stop);
558
559   if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME) {
560     GST_DEBUG ("seeking is only supported in TIME or DEFAULT format");
561     return FALSE;
562   }
563
564   if (speed < 0.0) {
565     GST_DEBUG ("only forward playback supported, rate %f not allowed", speed);
566     return FALSE;
567   }
568
569   GST_OBJECT_LOCK (wvparse);
570
571   rate = wvparse->samplerate;
572   if (rate == 0) {
573     GST_OBJECT_UNLOCK (wvparse);
574     GST_DEBUG ("haven't read header yet");
575     return FALSE;
576   }
577
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 */
580   if (stop == -1)
581     stop = wvparse->segment.duration;
582
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);
589   }
590
591   if (start < 0) {
592     GST_OBJECT_UNLOCK (wvparse);
593     GST_DEBUG_OBJECT (wvparse, "Invalid start sample %" G_GINT64_FORMAT, start);
594     return FALSE;
595   }
596
597   flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
598
599   /* operate on segment copy until we know the seek worked */
600   segment = wvparse->segment;
601
602   gst_segment_set_seek (&segment, speed, GST_FORMAT_DEFAULT,
603       seek_flags, start_type, start, stop_type, stop, &only_update);
604
605 #if 0
606   if (only_update) {
607     wvparse->segment = segment;
608     gst_wavpack_parse_send_newsegment (wvparse, TRUE);
609     goto done;
610   }
611 #endif
612
613   gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_start ());
614
615   if (flush) {
616     gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_start ());
617   } else {
618     gst_pad_pause_task (wvparse->sinkpad);
619   }
620
621   GST_PAD_STREAM_LOCK (wvparse->sinkpad);
622
623   /* Save current position */
624   last_stop = wvparse->segment.last_stop;
625
626   gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_stop ());
627
628   if (flush) {
629     gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_stop ());
630   }
631
632   GST_DEBUG_OBJECT (wvparse, "Performing seek to %" GST_TIME_FORMAT " sample %"
633       G_GINT64_FORMAT, GST_TIME_ARGS (segment.start * GST_SECOND / rate),
634       start);
635
636   ret = gst_wavpack_parse_scan_to_find_sample (wvparse, segment.start,
637       &byte_offset, &chunk_start);
638
639   if (ret) {
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;
650
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));
656     }
657   } else {
658     GST_DEBUG_OBJECT (wvparse, "seek failed: don't know where to seek to");
659   }
660
661   GST_PAD_STREAM_UNLOCK (wvparse->sinkpad);
662   GST_OBJECT_UNLOCK (wvparse);
663
664   gst_pad_start_task (wvparse->sinkpad,
665       (GstTaskFunction) gst_wavpack_parse_loop, wvparse);
666
667   return ret;
668 }
669
670 static gboolean
671 gst_wavpack_parse_sink_event (GstPad * pad, GstEvent * event)
672 {
673   GstWavpackParse *parse;
674
675   gboolean ret = TRUE;
676
677   parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
678
679   switch (GST_EVENT_TYPE (event)) {
680     case GST_EVENT_FLUSH_STOP:{
681       if (parse->adapter) {
682         gst_adapter_clear (parse->adapter);
683       }
684       if (parse->pending_buffer) {
685         gst_buffer_unref (parse->pending_buffer);
686         parse->pending_buffer = NULL;
687         parse->pending_offset = 0;
688       }
689       ret = gst_pad_push_event (parse->srcpad, event);
690       break;
691     }
692     case GST_EVENT_NEWSEGMENT:{
693       parse->need_newsegment = TRUE;
694       gst_event_unref (event);
695       ret = TRUE;
696       break;
697     }
698     case GST_EVENT_EOS:{
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);
703       }
704       if (parse->pending_buffer) {
705         gst_buffer_unref (parse->pending_buffer);
706         parse->pending_buffer = NULL;
707         parse->pending_offset = 0;
708       }
709       ret = gst_pad_push_event (parse->srcpad, event);
710       break;
711     }
712     default:{
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);
717       } else {
718         ret = gst_pad_push_event (parse->srcpad, event);
719       }
720       GST_PAD_STREAM_UNLOCK (pad);
721     }
722   }
723
724
725   gst_object_unref (parse);
726   return ret;
727 }
728
729 static gboolean
730 gst_wavpack_parse_src_event (GstPad * pad, GstEvent * event)
731 {
732   GstWavpackParse *parse;
733
734   gboolean ret;
735
736   parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
737
738   switch (GST_EVENT_TYPE (event)) {
739     case GST_EVENT_SEEK:
740       ret = gst_wavpack_parse_handle_seek_event (parse, event);
741       break;
742     default:
743       ret = gst_pad_event_default (pad, event);
744       break;
745   }
746
747   gst_object_unref (parse);
748   return ret;
749 }
750
751 static void
752 gst_wavpack_parse_init (GstWavpackParse * parse, GstWavpackParseClass * gclass)
753 {
754   GstElementClass *klass = GST_ELEMENT_GET_CLASS (parse);
755
756   GstPadTemplate *tmpl;
757
758   tmpl = gst_element_class_get_pad_template (klass, "sink");
759   parse->sinkpad = gst_pad_new_from_template (tmpl, "sink");
760
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));
769
770   gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
771
772   parse->srcpad = NULL;
773   gst_wavpack_parse_reset (parse);
774 }
775
776 static gint64
777 gst_wavpack_parse_get_upstream_length (GstWavpackParse * parse)
778 {
779   gint64 length = -1;
780
781   GstFormat format = GST_FORMAT_BYTES;
782
783   if (!gst_pad_query_peer_duration (parse->sinkpad, &format, &length)) {
784     length = -1;
785   } else {
786     GST_DEBUG ("upstream length: %" G_GINT64_FORMAT, length);
787   }
788   return length;
789 }
790
791 static GstBuffer *
792 gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse, gint64 offset,
793     guint size, GstFlowReturn * flow)
794 {
795   GstFlowReturn flow_ret;
796
797   GstBuffer *buf = NULL;
798
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;
805       goto done;
806     }
807   }
808
809   flow_ret = gst_pad_pull_range (wvparse->sinkpad, offset, size, &buf);
810
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));
814     buf = NULL;
815     goto done;
816   }
817
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);
822     buf = NULL;
823     flow_ret = GST_FLOW_UNEXPECTED;
824   }
825
826 done:
827   if (flow)
828     *flow = flow_ret;
829   return buf;
830 }
831
832 static gboolean
833 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
834     WavpackHeader * header)
835 {
836   GstWavpackMetadata meta;
837
838   GstCaps *caps = NULL;
839
840   guchar *bufptr;
841
842   g_assert (wvparse->srcpad == NULL);
843
844   bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
845
846   while (gst_wavpack_read_metadata (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
847     switch (meta.id) {
848       case ID_WVC_BITSTREAM:{
849         caps = gst_caps_new_simple ("audio/x-wavpack-correction",
850             "framed", G_TYPE_BOOLEAN, TRUE, NULL);
851         wvparse->srcpad =
852             gst_pad_new_from_template (gst_element_class_get_pad_template
853             (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
854         break;
855       }
856       case ID_WV_BITSTREAM:
857       case ID_WVX_BITSTREAM:{
858         WavpackStreamReader *stream_reader = gst_wavpack_stream_reader_new ();
859
860         WavpackContext *wpc;
861
862         gchar error_msg[80];
863
864         read_id rid;
865
866         gint channel_mask;
867
868         rid.buffer = GST_BUFFER_DATA (buf);
869         rid.length = GST_BUFFER_SIZE (buf);
870         rid.position = 0;
871
872         wpc =
873             WavpackOpenFileInputEx (stream_reader, &rid, NULL, error_msg, 0, 0);
874
875         if (!wpc)
876           return FALSE;
877
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;
883
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;
891 #else
892         channel_mask = WavpackGetChannelMask (wpc);
893 #endif
894         if (channel_mask == 0)
895           channel_mask =
896               gst_wavpack_get_default_channel_mask (wvparse->channels);
897
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);
902             caps = NULL;
903             WavpackCloseFile (wpc);
904             g_free (stream_reader);
905             break;
906           }
907         }
908
909         wvparse->srcpad =
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);
914         break;
915       }
916       default:{
917         GST_LOG_OBJECT (wvparse, "unhandled ID: 0x%02x", meta.id);
918         break;
919       }
920     }
921     if (caps != NULL)
922       break;
923   }
924
925   if (caps == NULL || wvparse->srcpad == NULL)
926     return FALSE;
927
928   GST_DEBUG_OBJECT (wvparse, "Added src pad with caps %" GST_PTR_FORMAT, caps);
929
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));
936
937   gst_pad_set_caps (wvparse->srcpad, caps);
938   gst_caps_unref (caps);
939   gst_pad_use_fixed_caps (wvparse->srcpad);
940
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));
945
946   return TRUE;
947 }
948
949 static GstFlowReturn
950 gst_wavpack_parse_push_buffer (GstWavpackParse * wvparse, GstBuffer * buf,
951     WavpackHeader * header)
952 {
953   GstFlowReturn ret;
954   wvparse->current_offset += header->ckSize + 8;
955
956   wvparse->segment.last_stop = header->block_index;
957
958   if (wvparse->need_newsegment) {
959     if (gst_wavpack_parse_send_newsegment (wvparse, FALSE))
960       wvparse->need_newsegment = FALSE;
961   }
962
963   /* send any queued events */
964   if (wvparse->queued_events) {
965     GList *l;
966
967     for (l = wvparse->queued_events; l != NULL; l = l->next) {
968       gst_pad_push_event (wvparse->srcpad, GST_EVENT (l->data));
969     }
970     g_list_free (wvparse->queued_events);
971     wvparse->queued_events = NULL;
972   }
973
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);
979   } else {
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;
984   }
985
986   if (!(header->flags & FINAL_BLOCK))
987     return GST_FLOW_OK;
988
989   buf = wvparse->pending_buffer;
990   wvparse->pending_buffer = NULL;
991
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;
998
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;
1002   }
1003
1004   wvparse->next_block_index = header->block_index + header->block_samples;
1005
1006   gst_buffer_set_caps (buf, GST_PAD_CAPS (wvparse->srcpad));
1007
1008   GST_LOG_OBJECT (wvparse, "Pushing buffer with time %" GST_TIME_FORMAT,
1009       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1010
1011   ret = gst_pad_push (wvparse->srcpad, buf);
1012
1013   wvparse->segment.last_stop = wvparse->next_block_index;
1014
1015   return ret;
1016 }
1017
1018 static guint8 *
1019 gst_wavpack_parse_find_marker (guint8 * buf, guint size)
1020 {
1021   int i;
1022
1023   guint8 *ret = NULL;
1024
1025   if (G_UNLIKELY (size < 4))
1026     return NULL;
1027
1028   for (i = 0; i < size - 4; i++) {
1029     if (memcmp (buf + i, "wvpk", 4) == 0) {
1030       ret = buf + i;
1031       break;
1032     }
1033   }
1034   return ret;
1035 }
1036
1037 static GstFlowReturn
1038 gst_wavpack_parse_resync_loop (GstWavpackParse * parse, WavpackHeader * header)
1039 {
1040   GstFlowReturn flow_ret = GST_FLOW_UNEXPECTED;
1041
1042   GstBuffer *buf = NULL;
1043
1044   /* loop until we have a frame header or reach the end of the stream */
1045   while (1) {
1046     guint8 *data, *marker;
1047
1048     guint len, size;
1049
1050     if (buf) {
1051       gst_buffer_unref (buf);
1052       buf = NULL;
1053     }
1054
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) {
1060         break;
1061       }
1062     }
1063
1064     len = MIN (parse->upstream_length - parse->current_offset, 2048);
1065
1066     GST_LOG_OBJECT (parse, "offset: %" G_GINT64_FORMAT, parse->current_offset);
1067
1068     buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1069         len, &flow_ret);
1070
1071     /* whatever the problem is, there's nothing more for us to do for now */
1072     if (flow_ret != GST_FLOW_OK)
1073       break;
1074
1075     data = GST_BUFFER_DATA (buf);
1076     size = GST_BUFFER_SIZE (buf);
1077
1078     /* not enough data for a header? */
1079     if (size < sizeof (WavpackHeader))
1080       break;
1081
1082     /* got a header right where we are at now? */
1083     if (gst_wavpack_read_header (header, data))
1084       break;
1085
1086     /* nope, let's see if we can find one */
1087     marker = gst_wavpack_parse_find_marker (data + 1, size - 1);
1088
1089     if (marker) {
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 */
1093     } else {
1094       parse->current_offset += len - 4;
1095     }
1096   }
1097
1098   if (buf)
1099     gst_buffer_unref (buf);
1100
1101   return flow_ret;
1102 }
1103
1104 static void
1105 gst_wavpack_parse_loop (GstElement * element)
1106 {
1107   GstWavpackParse *parse = GST_WAVPACK_PARSE (element);
1108
1109   GstFlowReturn flow_ret;
1110   WavpackHeader header = { {0,}, 0, };
1111   GstBuffer *buf = NULL;
1112
1113   flow_ret = gst_wavpack_parse_resync_loop (parse, &header);
1114
1115   if (flow_ret != GST_FLOW_OK)
1116     goto pause;
1117
1118   GST_LOG_OBJECT (parse, "Read header at offset %" G_GINT64_FORMAT
1119       ": chunk size = %u+8", parse->current_offset, header.ckSize);
1120
1121   buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1122       header.ckSize + 8, &flow_ret);
1123
1124   if (flow_ret != GST_FLOW_OK)
1125     goto pause;
1126
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;
1131       goto pause;
1132     }
1133   }
1134   if (header.flags & INITIAL_BLOCK)
1135     gst_wavpack_parse_index_append_entry (parse, parse->current_offset,
1136         header.block_index, header.block_samples);
1137
1138   flow_ret = gst_wavpack_parse_push_buffer (parse, buf, &header);
1139   if (flow_ret != GST_FLOW_OK)
1140     goto pause;
1141
1142   return;
1143
1144 pause:
1145   {
1146     const gchar *reason = gst_flow_get_name (flow_ret);
1147
1148     GST_LOG_OBJECT (parse, "pausing task, reason %s", reason);
1149     gst_pad_pause_task (parse->sinkpad);
1150
1151     if (GST_FLOW_IS_FATAL (flow_ret) || flow_ret == GST_FLOW_NOT_LINKED) {
1152       if (flow_ret == GST_FLOW_UNEXPECTED && parse->srcpad) {
1153         if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1154           GstClockTime stop;
1155
1156           GST_LOG_OBJECT (parse, "Sending segment done");
1157
1158           if ((stop = parse->segment.stop) == -1)
1159             stop = parse->segment.duration;
1160
1161           gst_element_post_message (GST_ELEMENT_CAST (parse),
1162               gst_message_new_segment_done (GST_OBJECT_CAST (parse),
1163                   parse->segment.format, stop));
1164         } else {
1165           GST_LOG_OBJECT (parse, "Sending EOS, at end of stream");
1166           gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1167         }
1168       } else {
1169         GST_ELEMENT_ERROR (parse, STREAM, FAILED,
1170             (_("Internal data stream error.")),
1171             ("stream stopped, reason %s", reason));
1172         if (parse->srcpad)
1173           gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1174       }
1175     }
1176     return;
1177   }
1178 }
1179
1180 static gboolean
1181 gst_wavpack_parse_resync_adapter (GstAdapter * adapter)
1182 {
1183   const guint8 *buf, *marker;
1184
1185   guint avail = gst_adapter_available (adapter);
1186
1187   if (avail < 4)
1188     return FALSE;
1189
1190   /* if the marker is at the beginning don't do the expensive search */
1191   buf = gst_adapter_peek (adapter, 4);
1192   if (memcmp (buf, "wvpk", 4) == 0)
1193     return TRUE;
1194
1195   if (avail == 4)
1196     return FALSE;
1197
1198   /* search for the marker in the complete content of the adapter */
1199   buf = gst_adapter_peek (adapter, avail);
1200   if (buf && (marker = gst_wavpack_parse_find_marker ((guint8 *) buf, avail))) {
1201     gst_adapter_flush (adapter, marker - buf);
1202     return TRUE;
1203   }
1204
1205   /* flush everything except the last 4 bytes. they could contain
1206    * the start of a new marker */
1207   gst_adapter_flush (adapter, avail - 4);
1208
1209   return FALSE;
1210 }
1211
1212 static GstFlowReturn
1213 gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf)
1214 {
1215   GstWavpackParse *wvparse = GST_WAVPACK_PARSE (GST_PAD_PARENT (pad));
1216
1217   GstFlowReturn ret = GST_FLOW_OK;
1218
1219   WavpackHeader wph;
1220
1221   const guint8 *tmp_buf;
1222
1223   if (!wvparse->adapter) {
1224     wvparse->adapter = gst_adapter_new ();
1225   }
1226
1227   if (GST_BUFFER_IS_DISCONT (buf)) {
1228     gst_adapter_clear (wvparse->adapter);
1229     wvparse->discont = TRUE;
1230   }
1231
1232   gst_adapter_push (wvparse->adapter, buf);
1233
1234   if (gst_adapter_available (wvparse->adapter) < sizeof (WavpackHeader))
1235     return ret;
1236
1237   if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1238     return ret;
1239
1240   tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1241   gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1242
1243   while (gst_adapter_available (wvparse->adapter) >= wph.ckSize + 4 * 1 + 4) {
1244     GstBuffer *outbuf =
1245         gst_adapter_take_buffer (wvparse->adapter, wph.ckSize + 4 * 1 + 4);
1246
1247     if (!outbuf)
1248       return GST_FLOW_ERROR;
1249
1250     if (wvparse->srcpad == NULL) {
1251       if (!gst_wavpack_parse_create_src_pad (wvparse, outbuf, &wph)) {
1252         GST_ERROR_OBJECT (wvparse, "Failed to create src pad");
1253         ret = GST_FLOW_ERROR;
1254         break;
1255       }
1256     }
1257
1258     ret = gst_wavpack_parse_push_buffer (wvparse, outbuf, &wph);
1259
1260     if (ret != GST_FLOW_OK)
1261       break;
1262
1263     if (gst_adapter_available (wvparse->adapter) >= sizeof (WavpackHeader)) {
1264       tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1265
1266       if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1267         break;
1268
1269       gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1270     }
1271   }
1272
1273   return ret;
1274 }
1275
1276 static GstStateChangeReturn
1277 gst_wavpack_parse_change_state (GstElement * element, GstStateChange transition)
1278 {
1279   GstWavpackParse *wvparse = GST_WAVPACK_PARSE (element);
1280
1281   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1282
1283   switch (transition) {
1284     case GST_STATE_CHANGE_READY_TO_PAUSED:
1285       gst_segment_init (&wvparse->segment, GST_FORMAT_DEFAULT);
1286       wvparse->segment.last_stop = 0;
1287     default:
1288       break;
1289   }
1290
1291   if (GST_ELEMENT_CLASS (parent_class)->change_state)
1292     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1293
1294   switch (transition) {
1295     case GST_STATE_CHANGE_PAUSED_TO_READY:
1296       gst_wavpack_parse_reset (wvparse);
1297       break;
1298     default:
1299       break;
1300   }
1301
1302   return ret;
1303 }
1304
1305 static gboolean
1306 gst_wavpack_parse_sink_activate (GstPad * sinkpad)
1307 {
1308   if (gst_pad_check_pull_range (sinkpad)) {
1309     return gst_pad_activate_pull (sinkpad, TRUE);
1310   } else {
1311     return gst_pad_activate_push (sinkpad, TRUE);
1312   }
1313 }
1314
1315 static gboolean
1316 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
1317 {
1318   gboolean result;
1319
1320   if (active) {
1321     result = gst_pad_start_task (sinkpad,
1322         (GstTaskFunction) gst_wavpack_parse_loop, GST_PAD_PARENT (sinkpad));
1323   } else {
1324     result = gst_pad_stop_task (sinkpad);
1325   }
1326
1327   return result;
1328 }
1329
1330 gboolean
1331 gst_wavpack_parse_plugin_init (GstPlugin * plugin)
1332 {
1333   if (!gst_element_register (plugin, "wavpackparse",
1334           GST_RANK_PRIMARY, GST_TYPE_WAVPACK_PARSE)) {
1335     return FALSE;
1336   }
1337
1338   GST_DEBUG_CATEGORY_INIT (gst_wavpack_parse_debug, "wavpack_parse", 0,
1339       "Wavpack file parser");
1340
1341   return TRUE;
1342 }