Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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
45 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
46  * with newer GLib versions (>= 2.31.0) */
47 #define GLIB_DISABLE_DEPRECATION_WARNINGS
48
49 #include <gst/gst.h>
50 #include <gst/gst-i18n-plugin.h>
51
52 #include <math.h>
53 #include <string.h>
54
55 #include <wavpack/wavpack.h>
56 #include "gstwavpackparse.h"
57 #include "gstwavpackstreamreader.h"
58 #include "gstwavpackcommon.h"
59
60 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_parse_debug);
61 #define GST_CAT_DEFAULT gst_wavpack_parse_debug
62
63 static inline GstWavpackParseIndexEntry *
64 gst_wavpack_parse_index_entry_new (void)
65 {
66   return g_slice_new (GstWavpackParseIndexEntry);
67 }
68
69 static inline void
70 gst_wavpack_parse_index_entry_free (GstWavpackParseIndexEntry * entry)
71 {
72   g_slice_free (GstWavpackParseIndexEntry, entry);
73 }
74
75 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
76     GST_PAD_SINK,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS ("audio/x-wavpack, "
79         "framed = (boolean) false; "
80         "audio/x-wavpack-correction, " "framed = (boolean) false")
81     );
82
83 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
84     GST_PAD_SRC,
85     GST_PAD_SOMETIMES,
86     GST_STATIC_CAPS ("audio/x-wavpack, "
87         "width = (int) [ 1, 32 ], "
88         "channels = (int) [ 1, 8 ], "
89         "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
90     );
91
92 static GstStaticPadTemplate wvc_src_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
93     GST_PAD_SRC,
94     GST_PAD_SOMETIMES,
95     GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) true")
96     );
97
98 static gboolean gst_wavpack_parse_sink_activate (GstPad * sinkpad);
99
100 static gboolean
101 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active);
102
103 static void gst_wavpack_parse_loop (GstElement * element);
104
105 static GstStateChangeReturn gst_wavpack_parse_change_state (GstElement *
106     element, GstStateChange transition);
107 static void gst_wavpack_parse_reset (GstWavpackParse * parse);
108
109 static gint64 gst_wavpack_parse_get_upstream_length (GstWavpackParse * wvparse);
110
111 static GstBuffer *gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse,
112     gint64 offset, guint size, GstFlowReturn * flow);
113 static GstFlowReturn gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf);
114
115 GST_BOILERPLATE (GstWavpackParse, gst_wavpack_parse, GstElement,
116     GST_TYPE_ELEMENT);
117
118 static void
119 gst_wavpack_parse_base_init (gpointer klass)
120 {
121   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
122
123   gst_element_class_add_static_pad_template (element_class, &src_factory);
124   gst_element_class_add_static_pad_template (element_class,
125       &wvc_src_factory);
126   gst_element_class_add_static_pad_template (element_class, &sink_factory);
127
128   gst_element_class_set_details_simple (element_class, "Wavpack parser",
129       "Codec/Demuxer/Audio",
130       "Parses Wavpack files",
131       "Arwed v. Merkatz <v.merkatz@gmx.net>, "
132       "Sebastian Dröge <slomo@circular-chaos.org>");
133 }
134
135 static void
136 gst_wavpack_parse_finalize (GObject * object)
137 {
138   gst_wavpack_parse_reset (GST_WAVPACK_PARSE (object));
139
140   G_OBJECT_CLASS (parent_class)->finalize (object);
141 }
142
143 static void
144 gst_wavpack_parse_class_init (GstWavpackParseClass * klass)
145 {
146   GObjectClass *gobject_class;
147
148   GstElementClass *gstelement_class;
149
150   gobject_class = (GObjectClass *) klass;
151   gstelement_class = (GstElementClass *) klass;
152
153   gobject_class->finalize = gst_wavpack_parse_finalize;
154   gstelement_class->change_state =
155       GST_DEBUG_FUNCPTR (gst_wavpack_parse_change_state);
156 }
157
158 static GstWavpackParseIndexEntry *
159 gst_wavpack_parse_index_get_last_entry (GstWavpackParse * wvparse)
160 {
161   g_assert (wvparse->entries != NULL);
162
163   return wvparse->entries->data;
164 }
165
166 static GstWavpackParseIndexEntry *
167 gst_wavpack_parse_index_get_entry_from_sample (GstWavpackParse * wvparse,
168     gint64 sample_offset)
169 {
170   gint i;
171
172   GSList *node;
173
174   if (wvparse->entries == NULL)
175     return NULL;
176
177   for (node = wvparse->entries, i = 0; node; node = node->next, i++) {
178     GstWavpackParseIndexEntry *entry;
179
180     entry = node->data;
181
182     GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
183         " byte %" G_GINT64_FORMAT, i, entry->sample_offset, entry->byte_offset);
184
185     if (entry->sample_offset <= sample_offset &&
186         sample_offset < entry->sample_offset_end) {
187       GST_LOG_OBJECT (wvparse, "found match");
188       return entry;
189     }
190
191     /* as the list is sorted and we first look at the latest entry
192      * we can abort searching for an entry if the sample we want is
193      * after the latest one */
194     if (sample_offset >= entry->sample_offset_end)
195       break;
196   }
197   GST_LOG_OBJECT (wvparse, "no match in index");
198   return NULL;
199 }
200
201 static void
202 gst_wavpack_parse_index_append_entry (GstWavpackParse * wvparse,
203     gint64 byte_offset, gint64 sample_offset, gint64 num_samples)
204 {
205   GstWavpackParseIndexEntry *entry;
206
207   /* do we have this one already? */
208   if (wvparse->entries) {
209     entry = gst_wavpack_parse_index_get_last_entry (wvparse);
210     if (entry->byte_offset >= byte_offset
211         || entry->sample_offset >= sample_offset)
212       return;
213   }
214
215   GST_LOG_OBJECT (wvparse, "Adding index entry %8" G_GINT64_FORMAT " - %"
216       GST_TIME_FORMAT " @ offset 0x%08" G_GINT64_MODIFIER "x", sample_offset,
217       GST_TIME_ARGS (gst_util_uint64_scale_int (sample_offset,
218               GST_SECOND, wvparse->samplerate)), byte_offset);
219
220   entry = gst_wavpack_parse_index_entry_new ();
221   entry->byte_offset = byte_offset;
222   entry->sample_offset = sample_offset;
223   entry->sample_offset_end = sample_offset + num_samples;
224   wvparse->entries = g_slist_prepend (wvparse->entries, entry);
225 }
226
227 static void
228 gst_wavpack_parse_reset (GstWavpackParse * parse)
229 {
230   parse->total_samples = G_GINT64_CONSTANT (-1);
231   parse->samplerate = 0;
232   parse->channels = 0;
233
234   gst_segment_init (&parse->segment, GST_FORMAT_UNDEFINED);
235   parse->next_block_index = 0;
236
237   parse->current_offset = 0;
238   parse->need_newsegment = TRUE;
239   parse->discont = TRUE;
240   parse->upstream_length = -1;
241
242   if (parse->entries) {
243     g_slist_foreach (parse->entries, (GFunc) gst_wavpack_parse_index_entry_free,
244         NULL);
245     g_slist_free (parse->entries);
246     parse->entries = NULL;
247   }
248
249   if (parse->adapter) {
250     gst_adapter_clear (parse->adapter);
251     g_object_unref (parse->adapter);
252     parse->adapter = NULL;
253   }
254
255   if (parse->srcpad != NULL) {
256     gboolean res;
257
258     GST_DEBUG_OBJECT (parse, "Removing src pad");
259     res = gst_element_remove_pad (GST_ELEMENT (parse), parse->srcpad);
260     g_return_if_fail (res != FALSE);
261     gst_object_unref (parse->srcpad);
262     parse->srcpad = NULL;
263   }
264
265   g_list_foreach (parse->queued_events, (GFunc) gst_mini_object_unref, NULL);
266   g_list_free (parse->queued_events);
267   parse->queued_events = NULL;
268
269   if (parse->pending_buffer)
270     gst_buffer_unref (parse->pending_buffer);
271
272   parse->pending_buffer = NULL;
273 }
274
275 static const GstQueryType *
276 gst_wavpack_parse_get_src_query_types (GstPad * pad)
277 {
278   static const GstQueryType types[] = {
279     GST_QUERY_POSITION,
280     GST_QUERY_DURATION,
281     GST_QUERY_SEEKING,
282     0
283   };
284
285   return types;
286 }
287
288 static gboolean
289 gst_wavpack_parse_src_query (GstPad * pad, GstQuery * query)
290 {
291   GstWavpackParse *parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
292
293   GstFormat format;
294
295   gboolean ret = FALSE;
296
297   switch (GST_QUERY_TYPE (query)) {
298     case GST_QUERY_POSITION:{
299       gint64 cur;
300
301       guint rate;
302
303       GST_OBJECT_LOCK (parse);
304       cur = parse->segment.last_stop;
305       rate = parse->samplerate;
306       GST_OBJECT_UNLOCK (parse);
307
308       if (rate == 0) {
309         GST_DEBUG_OBJECT (parse, "haven't read header yet");
310         break;
311       }
312
313       gst_query_parse_position (query, &format, NULL);
314
315       switch (format) {
316         case GST_FORMAT_TIME:
317           cur = gst_util_uint64_scale_int (cur, GST_SECOND, rate);
318           gst_query_set_position (query, GST_FORMAT_TIME, cur);
319           ret = TRUE;
320           break;
321         case GST_FORMAT_DEFAULT:
322           gst_query_set_position (query, GST_FORMAT_DEFAULT, cur);
323           ret = TRUE;
324           break;
325         default:
326           GST_DEBUG_OBJECT (parse, "cannot handle position query in "
327               "%s format. Forwarding upstream.", gst_format_get_name (format));
328           ret = gst_pad_query_default (pad, query);
329           break;
330       }
331       break;
332     }
333     case GST_QUERY_DURATION:{
334       gint64 len;
335
336       guint rate;
337
338       GST_OBJECT_LOCK (parse);
339       rate = parse->samplerate;
340       len = parse->total_samples;
341       GST_OBJECT_UNLOCK (parse);
342
343       if (rate == 0) {
344         GST_DEBUG_OBJECT (parse, "haven't read header yet");
345         break;
346       }
347
348       gst_query_parse_duration (query, &format, NULL);
349
350       switch (format) {
351         case GST_FORMAT_TIME:
352           if (len != G_GINT64_CONSTANT (-1))
353             len = gst_util_uint64_scale_int (len, GST_SECOND, rate);
354           gst_query_set_duration (query, GST_FORMAT_TIME, len);
355           ret = TRUE;
356           break;
357         case GST_FORMAT_DEFAULT:
358           gst_query_set_duration (query, GST_FORMAT_DEFAULT, len);
359           ret = TRUE;
360           break;
361         default:
362           GST_DEBUG_OBJECT (parse, "cannot handle duration query in "
363               "%s format. Forwarding upstream.", gst_format_get_name (format));
364           ret = gst_pad_query_default (pad, query);
365           break;
366       }
367       break;
368     }
369     case GST_QUERY_SEEKING:{
370       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
371       if (format == GST_FORMAT_TIME || format == GST_FORMAT_DEFAULT) {
372         gboolean seekable;
373
374         gint64 duration = -1;
375
376         /* only fails if we didn't read the headers yet and can't say
377          * anything about our seeking capabilities */
378         if (!gst_pad_query_duration (pad, &format, &duration))
379           break;
380
381         /* can't seek in streaming mode yet */
382         GST_OBJECT_LOCK (parse);
383         seekable = (parse->adapter == NULL);
384         GST_OBJECT_UNLOCK (parse);
385
386         gst_query_set_seeking (query, format, seekable, 0, duration);
387         ret = TRUE;
388       }
389       break;
390     }
391     default:{
392       ret = gst_pad_query_default (pad, query);
393       break;
394     }
395   }
396
397   gst_object_unref (parse);
398   return ret;
399
400 }
401
402 /* returns TRUE on success, with byte_offset set to the offset of the
403  * wavpack chunk containing the sample requested. start_sample will be
404  * set to the first sample in the chunk starting at byte_offset.
405  * Scanning from the last known header offset to the wanted position
406  * when seeking forward isn't very clever, but seems fast enough in
407  * practice and has the nice side effect of populating our index
408  * table */
409 static gboolean
410 gst_wavpack_parse_scan_to_find_sample (GstWavpackParse * parse,
411     gint64 sample, gint64 * byte_offset, gint64 * start_sample)
412 {
413   GstWavpackParseIndexEntry *entry;
414
415   GstFlowReturn ret;
416
417   gint64 off = 0;
418
419   /* first, check if we have to scan at all */
420   entry = gst_wavpack_parse_index_get_entry_from_sample (parse, sample);
421   if (entry) {
422     *byte_offset = entry->byte_offset;
423     *start_sample = entry->sample_offset;
424     GST_LOG_OBJECT (parse, "Found index entry: sample %" G_GINT64_FORMAT
425         " @ offset %" G_GINT64_FORMAT, entry->sample_offset,
426         entry->byte_offset);
427     return TRUE;
428   }
429
430   GST_LOG_OBJECT (parse, "No matching entry in index, scanning file ...");
431
432   /* if we have an index, we can start scanning from the last known offset
433    * in there, after all we know our wanted sample is not in the index */
434   if (parse->entries) {
435     GstWavpackParseIndexEntry *entry;
436
437     entry = gst_wavpack_parse_index_get_last_entry (parse);
438     off = entry->byte_offset;
439   }
440
441   /* now scan forward until we find the chunk we're looking for or hit EOS */
442   do {
443     WavpackHeader header;
444
445     GstBuffer *buf;
446
447     buf = gst_wavpack_parse_pull_buffer (parse, off, sizeof (WavpackHeader),
448         &ret);
449
450     if (buf == NULL)
451       break;
452
453     gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
454     gst_buffer_unref (buf);
455
456     if (header.flags & INITIAL_BLOCK)
457       gst_wavpack_parse_index_append_entry (parse, off, header.block_index,
458           header.block_samples);
459     else
460       continue;
461
462     if (header.block_index <= sample &&
463         sample < (header.block_index + header.block_samples)) {
464       *byte_offset = off;
465       *start_sample = header.block_index;
466       return TRUE;
467     }
468
469     off += header.ckSize + 8;
470   } while (1);
471
472   GST_DEBUG_OBJECT (parse, "scan failed: %s (off=0x%08" G_GINT64_MODIFIER "x)",
473       gst_flow_get_name (ret), off);
474
475   return FALSE;
476 }
477
478 static gboolean
479 gst_wavpack_parse_send_newsegment (GstWavpackParse * wvparse, gboolean update)
480 {
481   GstSegment *s = &wvparse->segment;
482
483   gboolean ret;
484
485   gint64 stop_time = -1;
486
487   gint64 start_time = 0;
488
489   gint64 cur_pos_time;
490
491   gint64 diff;
492
493   /* segment is in DEFAULT format, but we want to send a TIME newsegment */
494   start_time = gst_util_uint64_scale_int (s->start, GST_SECOND,
495       wvparse->samplerate);
496
497   if (s->stop != -1) {
498     stop_time = gst_util_uint64_scale_int (s->stop, GST_SECOND,
499         wvparse->samplerate);
500   }
501
502   GST_DEBUG_OBJECT (wvparse, "sending newsegment from %" GST_TIME_FORMAT
503       " to %" GST_TIME_FORMAT, GST_TIME_ARGS (start_time),
504       GST_TIME_ARGS (stop_time));
505
506   /* after a seek, s->last_stop will point to a chunk boundary, ie. from
507    * which sample we will start sending data again, while s->start will
508    * point to the sample we actually want to seek to and want to start
509    * playing right after the seek. Adjust clock-time for the difference
510    * so we start playing from start_time */
511   cur_pos_time = gst_util_uint64_scale_int (s->last_stop, GST_SECOND,
512       wvparse->samplerate);
513   diff = start_time - cur_pos_time;
514
515   ret = gst_pad_push_event (wvparse->srcpad,
516       gst_event_new_new_segment (update, s->rate, GST_FORMAT_TIME,
517           start_time, stop_time, start_time - diff));
518
519   return ret;
520 }
521
522 static gboolean
523 gst_wavpack_parse_handle_seek_event (GstWavpackParse * wvparse,
524     GstEvent * event)
525 {
526   GstSeekFlags seek_flags;
527
528   GstSeekType start_type;
529
530   GstSeekType stop_type;
531
532   GstSegment segment;
533
534   GstFormat format;
535
536   gboolean only_update;
537
538   gboolean flush, ret;
539
540   gdouble speed;
541
542   gint64 stop;
543
544   gint64 start;                 /* sample we want to seek to                  */
545
546   gint64 byte_offset;           /* byte offset the chunk we seek to starts at */
547
548   gint64 chunk_start;           /* first sample in chunk we seek to           */
549
550   guint rate;
551
552   gint64 last_stop;
553
554   if (wvparse->adapter) {
555     GST_DEBUG_OBJECT (wvparse, "seeking in streaming mode not implemented yet");
556     return FALSE;
557   }
558
559   gst_event_parse_seek (event, &speed, &format, &seek_flags, &start_type,
560       &start, &stop_type, &stop);
561
562   if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME) {
563     GST_DEBUG ("seeking is only supported in TIME or DEFAULT format");
564     return FALSE;
565   }
566
567   if (speed < 0.0) {
568     GST_DEBUG ("only forward playback supported, rate %f not allowed", speed);
569     return FALSE;
570   }
571
572   GST_OBJECT_LOCK (wvparse);
573
574   rate = wvparse->samplerate;
575   if (rate == 0) {
576     GST_OBJECT_UNLOCK (wvparse);
577     GST_DEBUG ("haven't read header yet");
578     return FALSE;
579   }
580
581   /* figure out the last position we need to play. If it's configured (stop !=
582    * -1), use that, else we play until the total duration of the file */
583   if (stop == -1)
584     stop = wvparse->segment.duration;
585
586   /* convert from time to samples if necessary */
587   if (format == GST_FORMAT_TIME) {
588     if (start_type != GST_SEEK_TYPE_NONE)
589       start = gst_util_uint64_scale_int (start, rate, GST_SECOND);
590     if (stop_type != GST_SEEK_TYPE_NONE)
591       stop = gst_util_uint64_scale_int (stop, rate, GST_SECOND);
592   }
593
594   if (start < 0) {
595     GST_OBJECT_UNLOCK (wvparse);
596     GST_DEBUG_OBJECT (wvparse, "Invalid start sample %" G_GINT64_FORMAT, start);
597     return FALSE;
598   }
599
600   flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
601
602   /* operate on segment copy until we know the seek worked */
603   segment = wvparse->segment;
604
605   gst_segment_set_seek (&segment, speed, GST_FORMAT_DEFAULT,
606       seek_flags, start_type, start, stop_type, stop, &only_update);
607
608 #if 0
609   if (only_update) {
610     wvparse->segment = segment;
611     gst_wavpack_parse_send_newsegment (wvparse, TRUE);
612     goto done;
613   }
614 #endif
615
616   gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_start ());
617
618   if (flush) {
619     gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_start ());
620   } else {
621     gst_pad_pause_task (wvparse->sinkpad);
622   }
623
624   GST_PAD_STREAM_LOCK (wvparse->sinkpad);
625
626   /* Save current position */
627   last_stop = wvparse->segment.last_stop;
628
629   gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_stop ());
630
631   if (flush) {
632     gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_stop ());
633   }
634
635   GST_DEBUG_OBJECT (wvparse, "Performing seek to %" GST_TIME_FORMAT " sample %"
636       G_GINT64_FORMAT, GST_TIME_ARGS (segment.start * GST_SECOND / rate),
637       start);
638
639   ret = gst_wavpack_parse_scan_to_find_sample (wvparse, segment.start,
640       &byte_offset, &chunk_start);
641
642   if (ret) {
643     GST_DEBUG_OBJECT (wvparse, "new offset: %" G_GINT64_FORMAT, byte_offset);
644     wvparse->current_offset = byte_offset;
645     /* we want to send a newsegment event with the actual seek position
646      * as start, even though our first buffer might start before the
647      * configured segment. We leave it up to the decoder or sink to crop
648      * the output buffers accordingly */
649     wvparse->segment = segment;
650     wvparse->segment.last_stop = chunk_start;
651     wvparse->need_newsegment = TRUE;
652     wvparse->discont = (last_stop != chunk_start) ? TRUE : FALSE;
653
654     /* if we're doing a segment seek, post a SEGMENT_START message */
655     if (wvparse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
656       gst_element_post_message (GST_ELEMENT_CAST (wvparse),
657           gst_message_new_segment_start (GST_OBJECT_CAST (wvparse),
658               wvparse->segment.format, wvparse->segment.last_stop));
659     }
660   } else {
661     GST_DEBUG_OBJECT (wvparse, "seek failed: don't know where to seek to");
662   }
663
664   GST_PAD_STREAM_UNLOCK (wvparse->sinkpad);
665   GST_OBJECT_UNLOCK (wvparse);
666
667   gst_pad_start_task (wvparse->sinkpad,
668       (GstTaskFunction) gst_wavpack_parse_loop, wvparse);
669
670   return ret;
671 }
672
673 static gboolean
674 gst_wavpack_parse_sink_event (GstPad * pad, GstEvent * event)
675 {
676   GstWavpackParse *parse;
677
678   gboolean ret = TRUE;
679
680   parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
681
682   switch (GST_EVENT_TYPE (event)) {
683     case GST_EVENT_FLUSH_STOP:{
684       if (parse->adapter) {
685         gst_adapter_clear (parse->adapter);
686       }
687       if (parse->pending_buffer) {
688         gst_buffer_unref (parse->pending_buffer);
689         parse->pending_buffer = NULL;
690         parse->pending_offset = 0;
691       }
692       ret = gst_pad_push_event (parse->srcpad, event);
693       break;
694     }
695     case GST_EVENT_NEWSEGMENT:{
696       parse->need_newsegment = TRUE;
697       gst_event_unref (event);
698       ret = TRUE;
699       break;
700     }
701     case GST_EVENT_EOS:{
702       if (parse->adapter) {
703         /* remove all bytes that are left in the adapter after EOS. They can't
704          * be a complete Wavpack block and we can't do anything with them */
705         gst_adapter_clear (parse->adapter);
706       }
707       if (parse->pending_buffer) {
708         gst_buffer_unref (parse->pending_buffer);
709         parse->pending_buffer = NULL;
710         parse->pending_offset = 0;
711       }
712       ret = gst_pad_push_event (parse->srcpad, event);
713       break;
714     }
715     default:{
716       /* stream lock is recursive, should be fine for all events */
717       GST_PAD_STREAM_LOCK (pad);
718       if (parse->srcpad == NULL) {
719         parse->queued_events = g_list_append (parse->queued_events, event);
720       } else {
721         ret = gst_pad_push_event (parse->srcpad, event);
722       }
723       GST_PAD_STREAM_UNLOCK (pad);
724     }
725   }
726
727
728   gst_object_unref (parse);
729   return ret;
730 }
731
732 static gboolean
733 gst_wavpack_parse_src_event (GstPad * pad, GstEvent * event)
734 {
735   GstWavpackParse *parse;
736
737   gboolean ret;
738
739   parse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
740
741   switch (GST_EVENT_TYPE (event)) {
742     case GST_EVENT_SEEK:
743       ret = gst_wavpack_parse_handle_seek_event (parse, event);
744       break;
745     default:
746       ret = gst_pad_event_default (pad, event);
747       break;
748   }
749
750   gst_object_unref (parse);
751   return ret;
752 }
753
754 static void
755 gst_wavpack_parse_init (GstWavpackParse * parse, GstWavpackParseClass * gclass)
756 {
757   GstElementClass *klass = GST_ELEMENT_GET_CLASS (parse);
758
759   GstPadTemplate *tmpl;
760
761   tmpl = gst_element_class_get_pad_template (klass, "sink");
762   parse->sinkpad = gst_pad_new_from_template (tmpl, "sink");
763
764   gst_pad_set_activate_function (parse->sinkpad,
765       GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate));
766   gst_pad_set_activatepull_function (parse->sinkpad,
767       GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_activate_pull));
768   gst_pad_set_event_function (parse->sinkpad,
769       GST_DEBUG_FUNCPTR (gst_wavpack_parse_sink_event));
770   gst_pad_set_chain_function (parse->sinkpad,
771       GST_DEBUG_FUNCPTR (gst_wavpack_parse_chain));
772
773   gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
774
775   parse->srcpad = NULL;
776   gst_wavpack_parse_reset (parse);
777 }
778
779 static gint64
780 gst_wavpack_parse_get_upstream_length (GstWavpackParse * parse)
781 {
782   gint64 length = -1;
783
784   GstFormat format = GST_FORMAT_BYTES;
785
786   if (!gst_pad_query_peer_duration (parse->sinkpad, &format, &length)) {
787     length = -1;
788   } else {
789     GST_DEBUG ("upstream length: %" G_GINT64_FORMAT, length);
790   }
791   return length;
792 }
793
794 static GstBuffer *
795 gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse, gint64 offset,
796     guint size, GstFlowReturn * flow)
797 {
798   GstFlowReturn flow_ret;
799
800   GstBuffer *buf = NULL;
801
802   if (offset + size > wvparse->upstream_length) {
803     wvparse->upstream_length = gst_wavpack_parse_get_upstream_length (wvparse);
804     if (offset + size > wvparse->upstream_length) {
805       GST_DEBUG_OBJECT (wvparse, "EOS: %" G_GINT64_FORMAT " + %u > %"
806           G_GINT64_FORMAT, offset, size, wvparse->upstream_length);
807       flow_ret = GST_FLOW_UNEXPECTED;
808       goto done;
809     }
810   }
811
812   flow_ret = gst_pad_pull_range (wvparse->sinkpad, offset, size, &buf);
813
814   if (flow_ret != GST_FLOW_OK) {
815     GST_DEBUG_OBJECT (wvparse, "pull_range (%" G_GINT64_FORMAT ", %u) "
816         "failed, flow: %s", offset, size, gst_flow_get_name (flow_ret));
817     buf = NULL;
818     goto done;
819   }
820
821   if (GST_BUFFER_SIZE (buf) < size) {
822     GST_DEBUG_OBJECT (wvparse, "Short read at offset %" G_GINT64_FORMAT
823         ", got only %u of %u bytes", offset, GST_BUFFER_SIZE (buf), size);
824     gst_buffer_unref (buf);
825     buf = NULL;
826     flow_ret = GST_FLOW_UNEXPECTED;
827   }
828
829 done:
830   if (flow)
831     *flow = flow_ret;
832   return buf;
833 }
834
835 static gboolean
836 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
837     WavpackHeader * header)
838 {
839   GstWavpackMetadata meta;
840
841   GstCaps *caps = NULL;
842
843   guchar *bufptr;
844
845   g_assert (wvparse->srcpad == NULL);
846
847   bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
848
849   while (gst_wavpack_read_metadata (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
850     switch (meta.id) {
851       case ID_WVC_BITSTREAM:{
852         caps = gst_caps_new_simple ("audio/x-wavpack-correction",
853             "framed", G_TYPE_BOOLEAN, TRUE, NULL);
854         wvparse->srcpad =
855             gst_pad_new_from_template (gst_element_class_get_pad_template
856             (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
857         break;
858       }
859       case ID_WV_BITSTREAM:
860       case ID_WVX_BITSTREAM:{
861         WavpackStreamReader *stream_reader = gst_wavpack_stream_reader_new ();
862
863         WavpackContext *wpc;
864
865         gchar error_msg[80];
866
867         read_id rid;
868
869         gint channel_mask;
870
871         rid.buffer = GST_BUFFER_DATA (buf);
872         rid.length = GST_BUFFER_SIZE (buf);
873         rid.position = 0;
874
875         wpc =
876             WavpackOpenFileInputEx (stream_reader, &rid, NULL, error_msg, 0, 0);
877
878         if (!wpc)
879           return FALSE;
880
881         wvparse->samplerate = WavpackGetSampleRate (wpc);
882         wvparse->channels = WavpackGetNumChannels (wpc);
883         wvparse->total_samples =
884             (header->total_samples ==
885             0xffffffff) ? G_GINT64_CONSTANT (-1) : header->total_samples;
886
887         caps = gst_caps_new_simple ("audio/x-wavpack",
888             "width", G_TYPE_INT, WavpackGetBitsPerSample (wpc),
889             "channels", G_TYPE_INT, wvparse->channels,
890             "rate", G_TYPE_INT, wvparse->samplerate,
891             "framed", G_TYPE_BOOLEAN, TRUE, NULL);
892 #ifdef WAVPACK_OLD_API
893         channel_mask = wpc->config.channel_mask;
894 #else
895         channel_mask = WavpackGetChannelMask (wpc);
896 #endif
897         if (channel_mask == 0)
898           channel_mask =
899               gst_wavpack_get_default_channel_mask (wvparse->channels);
900
901         if (channel_mask != 0) {
902           if (!gst_wavpack_set_channel_layout (caps, channel_mask)) {
903             GST_WARNING_OBJECT (wvparse, "Failed to set channel layout");
904             gst_caps_unref (caps);
905             caps = NULL;
906             WavpackCloseFile (wpc);
907             g_free (stream_reader);
908             break;
909           }
910         }
911
912         wvparse->srcpad =
913             gst_pad_new_from_template (gst_element_class_get_pad_template
914             (GST_ELEMENT_GET_CLASS (wvparse), "src"), "src");
915         WavpackCloseFile (wpc);
916         g_free (stream_reader);
917         break;
918       }
919       default:{
920         GST_LOG_OBJECT (wvparse, "unhandled ID: 0x%02x", meta.id);
921         break;
922       }
923     }
924     if (caps != NULL)
925       break;
926   }
927
928   if (caps == NULL || wvparse->srcpad == NULL)
929     return FALSE;
930
931   GST_DEBUG_OBJECT (wvparse, "Added src pad with caps %" GST_PTR_FORMAT, caps);
932
933   gst_pad_set_query_function (wvparse->srcpad,
934       GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_query));
935   gst_pad_set_query_type_function (wvparse->srcpad,
936       GST_DEBUG_FUNCPTR (gst_wavpack_parse_get_src_query_types));
937   gst_pad_set_event_function (wvparse->srcpad,
938       GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_event));
939
940   gst_pad_set_caps (wvparse->srcpad, caps);
941   gst_caps_unref (caps);
942   gst_pad_use_fixed_caps (wvparse->srcpad);
943
944   gst_object_ref (wvparse->srcpad);
945   gst_pad_set_active (wvparse->srcpad, TRUE);
946   gst_element_add_pad (GST_ELEMENT (wvparse), wvparse->srcpad);
947   gst_element_no_more_pads (GST_ELEMENT (wvparse));
948
949   return TRUE;
950 }
951
952 static GstFlowReturn
953 gst_wavpack_parse_push_buffer (GstWavpackParse * wvparse, GstBuffer * buf,
954     WavpackHeader * header)
955 {
956   GstFlowReturn ret;
957   wvparse->current_offset += header->ckSize + 8;
958
959   wvparse->segment.last_stop = header->block_index;
960
961   if (wvparse->need_newsegment) {
962     if (gst_wavpack_parse_send_newsegment (wvparse, FALSE))
963       wvparse->need_newsegment = FALSE;
964   }
965
966   /* send any queued events */
967   if (wvparse->queued_events) {
968     GList *l;
969
970     for (l = wvparse->queued_events; l != NULL; l = l->next) {
971       gst_pad_push_event (wvparse->srcpad, GST_EVENT (l->data));
972     }
973     g_list_free (wvparse->queued_events);
974     wvparse->queued_events = NULL;
975   }
976
977   if (wvparse->pending_buffer == NULL) {
978     wvparse->pending_buffer = buf;
979     wvparse->pending_offset = header->block_index;
980   } else if (wvparse->pending_offset == header->block_index) {
981     wvparse->pending_buffer = gst_buffer_join (wvparse->pending_buffer, buf);
982   } else {
983     GST_ERROR ("Got incomplete block, dropping");
984     gst_buffer_unref (wvparse->pending_buffer);
985     wvparse->pending_buffer = buf;
986     wvparse->pending_offset = header->block_index;
987   }
988
989   if (!(header->flags & FINAL_BLOCK))
990     return GST_FLOW_OK;
991
992   buf = wvparse->pending_buffer;
993   wvparse->pending_buffer = NULL;
994
995   GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (header->block_index,
996       GST_SECOND, wvparse->samplerate);
997   GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (header->block_samples,
998       GST_SECOND, wvparse->samplerate);
999   GST_BUFFER_OFFSET (buf) = header->block_index;
1000   GST_BUFFER_OFFSET_END (buf) = header->block_index + header->block_samples;
1001
1002   if (wvparse->discont || wvparse->next_block_index != header->block_index) {
1003     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1004     wvparse->discont = FALSE;
1005   }
1006
1007   wvparse->next_block_index = header->block_index + header->block_samples;
1008
1009   gst_buffer_set_caps (buf, GST_PAD_CAPS (wvparse->srcpad));
1010
1011   GST_LOG_OBJECT (wvparse, "Pushing buffer with time %" GST_TIME_FORMAT,
1012       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1013
1014   ret = gst_pad_push (wvparse->srcpad, buf);
1015
1016   wvparse->segment.last_stop = wvparse->next_block_index;
1017
1018   return ret;
1019 }
1020
1021 static guint8 *
1022 gst_wavpack_parse_find_marker (guint8 * buf, guint size)
1023 {
1024   int i;
1025
1026   guint8 *ret = NULL;
1027
1028   if (G_UNLIKELY (size < 4))
1029     return NULL;
1030
1031   for (i = 0; i < size - 4; i++) {
1032     if (memcmp (buf + i, "wvpk", 4) == 0) {
1033       ret = buf + i;
1034       break;
1035     }
1036   }
1037   return ret;
1038 }
1039
1040 static GstFlowReturn
1041 gst_wavpack_parse_resync_loop (GstWavpackParse * parse, WavpackHeader * header)
1042 {
1043   GstFlowReturn flow_ret = GST_FLOW_UNEXPECTED;
1044
1045   GstBuffer *buf = NULL;
1046
1047   /* loop until we have a frame header or reach the end of the stream */
1048   while (1) {
1049     guint8 *data, *marker;
1050
1051     guint len, size;
1052
1053     if (buf) {
1054       gst_buffer_unref (buf);
1055       buf = NULL;
1056     }
1057
1058     if (parse->upstream_length == 0 ||
1059         parse->upstream_length <= parse->current_offset) {
1060       parse->upstream_length = gst_wavpack_parse_get_upstream_length (parse);
1061       if (parse->upstream_length == 0 ||
1062           parse->upstream_length <= parse->current_offset) {
1063         break;
1064       }
1065     }
1066
1067     len = MIN (parse->upstream_length - parse->current_offset, 2048);
1068
1069     GST_LOG_OBJECT (parse, "offset: %" G_GINT64_FORMAT, parse->current_offset);
1070
1071     buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1072         len, &flow_ret);
1073
1074     /* whatever the problem is, there's nothing more for us to do for now */
1075     if (flow_ret != GST_FLOW_OK)
1076       break;
1077
1078     data = GST_BUFFER_DATA (buf);
1079     size = GST_BUFFER_SIZE (buf);
1080
1081     /* not enough data for a header? */
1082     if (size < sizeof (WavpackHeader))
1083       break;
1084
1085     /* got a header right where we are at now? */
1086     if (gst_wavpack_read_header (header, data))
1087       break;
1088
1089     /* nope, let's see if we can find one */
1090     marker = gst_wavpack_parse_find_marker (data + 1, size - 1);
1091
1092     if (marker) {
1093       parse->current_offset += marker - data;
1094       /* do one more loop iteration to make sure we pull enough
1095        * data for a full header, we'll bail out then */
1096     } else {
1097       parse->current_offset += len - 4;
1098     }
1099   }
1100
1101   if (buf)
1102     gst_buffer_unref (buf);
1103
1104   return flow_ret;
1105 }
1106
1107 static void
1108 gst_wavpack_parse_loop (GstElement * element)
1109 {
1110   GstWavpackParse *parse = GST_WAVPACK_PARSE (element);
1111
1112   GstFlowReturn flow_ret;
1113   WavpackHeader header = { {0,}, 0, };
1114   GstBuffer *buf = NULL;
1115
1116   flow_ret = gst_wavpack_parse_resync_loop (parse, &header);
1117
1118   if (flow_ret != GST_FLOW_OK)
1119     goto pause;
1120
1121   GST_LOG_OBJECT (parse, "Read header at offset %" G_GINT64_FORMAT
1122       ": chunk size = %u+8", parse->current_offset, header.ckSize);
1123
1124   buf = gst_wavpack_parse_pull_buffer (parse, parse->current_offset,
1125       header.ckSize + 8, &flow_ret);
1126
1127   if (flow_ret != GST_FLOW_OK)
1128     goto pause;
1129
1130   if (parse->srcpad == NULL) {
1131     if (!gst_wavpack_parse_create_src_pad (parse, buf, &header)) {
1132       GST_ERROR_OBJECT (parse, "Failed to create src pad");
1133       flow_ret = GST_FLOW_ERROR;
1134       goto pause;
1135     }
1136   }
1137   if (header.flags & INITIAL_BLOCK)
1138     gst_wavpack_parse_index_append_entry (parse, parse->current_offset,
1139         header.block_index, header.block_samples);
1140
1141   flow_ret = gst_wavpack_parse_push_buffer (parse, buf, &header);
1142   if (flow_ret != GST_FLOW_OK)
1143     goto pause;
1144
1145   return;
1146
1147 pause:
1148   {
1149     const gchar *reason = gst_flow_get_name (flow_ret);
1150
1151     GST_LOG_OBJECT (parse, "pausing task, reason %s", reason);
1152     gst_pad_pause_task (parse->sinkpad);
1153
1154     if (flow_ret == GST_FLOW_UNEXPECTED && parse->srcpad) {
1155       if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1156         GstClockTime stop;
1157
1158         GST_LOG_OBJECT (parse, "Sending segment done");
1159
1160         if ((stop = parse->segment.stop) == -1)
1161           stop = parse->segment.duration;
1162
1163         gst_element_post_message (GST_ELEMENT_CAST (parse),
1164             gst_message_new_segment_done (GST_OBJECT_CAST (parse),
1165                 parse->segment.format, stop));
1166       } else {
1167         GST_LOG_OBJECT (parse, "Sending EOS, at end of stream");
1168         gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1169       }
1170     } else if (flow_ret == GST_FLOW_NOT_LINKED
1171         || flow_ret < GST_FLOW_UNEXPECTED) {
1172       GST_ELEMENT_ERROR (parse, STREAM, FAILED,
1173           (_("Internal data stream error.")), ("stream stopped, reason %s",
1174               reason));
1175       if (parse->srcpad)
1176         gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1177     }
1178     return;
1179   }
1180 }
1181
1182 static gboolean
1183 gst_wavpack_parse_resync_adapter (GstAdapter * adapter)
1184 {
1185   const guint8 *buf, *marker;
1186
1187   guint avail = gst_adapter_available (adapter);
1188
1189   if (avail < 4)
1190     return FALSE;
1191
1192   /* if the marker is at the beginning don't do the expensive search */
1193   buf = gst_adapter_peek (adapter, 4);
1194   if (memcmp (buf, "wvpk", 4) == 0)
1195     return TRUE;
1196
1197   if (avail == 4)
1198     return FALSE;
1199
1200   /* search for the marker in the complete content of the adapter */
1201   buf = gst_adapter_peek (adapter, avail);
1202   if (buf && (marker = gst_wavpack_parse_find_marker ((guint8 *) buf, avail))) {
1203     gst_adapter_flush (adapter, marker - buf);
1204     return TRUE;
1205   }
1206
1207   /* flush everything except the last 4 bytes. they could contain
1208    * the start of a new marker */
1209   gst_adapter_flush (adapter, avail - 4);
1210
1211   return FALSE;
1212 }
1213
1214 static GstFlowReturn
1215 gst_wavpack_parse_chain (GstPad * pad, GstBuffer * buf)
1216 {
1217   GstWavpackParse *wvparse = GST_WAVPACK_PARSE (GST_PAD_PARENT (pad));
1218
1219   GstFlowReturn ret = GST_FLOW_OK;
1220
1221   WavpackHeader wph;
1222
1223   const guint8 *tmp_buf;
1224
1225   if (!wvparse->adapter) {
1226     wvparse->adapter = gst_adapter_new ();
1227   }
1228
1229   if (GST_BUFFER_IS_DISCONT (buf)) {
1230     gst_adapter_clear (wvparse->adapter);
1231     wvparse->discont = TRUE;
1232   }
1233
1234   gst_adapter_push (wvparse->adapter, buf);
1235
1236   if (gst_adapter_available (wvparse->adapter) < sizeof (WavpackHeader))
1237     return ret;
1238
1239   if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1240     return ret;
1241
1242   tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1243   gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1244
1245   while (gst_adapter_available (wvparse->adapter) >= wph.ckSize + 4 * 1 + 4) {
1246     GstBuffer *outbuf =
1247         gst_adapter_take_buffer (wvparse->adapter, wph.ckSize + 4 * 1 + 4);
1248
1249     if (!outbuf)
1250       return GST_FLOW_ERROR;
1251
1252     if (wvparse->srcpad == NULL) {
1253       if (!gst_wavpack_parse_create_src_pad (wvparse, outbuf, &wph)) {
1254         GST_ERROR_OBJECT (wvparse, "Failed to create src pad");
1255         ret = GST_FLOW_ERROR;
1256         break;
1257       }
1258     }
1259
1260     ret = gst_wavpack_parse_push_buffer (wvparse, outbuf, &wph);
1261
1262     if (ret != GST_FLOW_OK)
1263       break;
1264
1265     if (gst_adapter_available (wvparse->adapter) >= sizeof (WavpackHeader)) {
1266       tmp_buf = gst_adapter_peek (wvparse->adapter, sizeof (WavpackHeader));
1267
1268       if (!gst_wavpack_parse_resync_adapter (wvparse->adapter))
1269         break;
1270
1271       gst_wavpack_read_header (&wph, (guint8 *) tmp_buf);
1272     }
1273   }
1274
1275   return ret;
1276 }
1277
1278 static GstStateChangeReturn
1279 gst_wavpack_parse_change_state (GstElement * element, GstStateChange transition)
1280 {
1281   GstWavpackParse *wvparse = GST_WAVPACK_PARSE (element);
1282
1283   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1284
1285   switch (transition) {
1286     case GST_STATE_CHANGE_READY_TO_PAUSED:
1287       gst_segment_init (&wvparse->segment, GST_FORMAT_DEFAULT);
1288       wvparse->segment.last_stop = 0;
1289     default:
1290       break;
1291   }
1292
1293   if (GST_ELEMENT_CLASS (parent_class)->change_state)
1294     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1295
1296   switch (transition) {
1297     case GST_STATE_CHANGE_PAUSED_TO_READY:
1298       gst_wavpack_parse_reset (wvparse);
1299       break;
1300     default:
1301       break;
1302   }
1303
1304   return ret;
1305 }
1306
1307 static gboolean
1308 gst_wavpack_parse_sink_activate (GstPad * sinkpad)
1309 {
1310   if (gst_pad_check_pull_range (sinkpad)) {
1311     return gst_pad_activate_pull (sinkpad, TRUE);
1312   } else {
1313     return gst_pad_activate_push (sinkpad, TRUE);
1314   }
1315 }
1316
1317 static gboolean
1318 gst_wavpack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
1319 {
1320   gboolean result;
1321
1322   if (active) {
1323     result = gst_pad_start_task (sinkpad,
1324         (GstTaskFunction) gst_wavpack_parse_loop, GST_PAD_PARENT (sinkpad));
1325   } else {
1326     result = gst_pad_stop_task (sinkpad);
1327   }
1328
1329   return result;
1330 }
1331
1332 gboolean
1333 gst_wavpack_parse_plugin_init (GstPlugin * plugin)
1334 {
1335   if (!gst_element_register (plugin, "wavpackparse",
1336           GST_RANK_PRIMARY, GST_TYPE_WAVPACK_PARSE)) {
1337     return FALSE;
1338   }
1339
1340   GST_DEBUG_CATEGORY_INIT (gst_wavpack_parse_debug, "wavpack_parse", 0,
1341       "Wavpack file parser");
1342
1343   return TRUE;
1344 }