Unify the long descriptions in the plugin details (#337263).
[platform/upstream/gst-plugins-good.git] / ext / wavpack / gstwavpackparse.c
1 /* GStreamer wavpack plugin
2  * (c) 2005 Arwed v. Merkatz <v.merkatz@gmx.net>
3  * (c) 2006 Tim-Philipp Müller <tim centricular net>
4  *
5  * gstwavpackparse.c: wavpack file parser
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <gst/gst.h>
24
25 #include <math.h>
26 #include <string.h>
27
28 #include <wavpack/wavpack.h>
29 #include "gstwavpackparse.h"
30 #include "gstwavpackcommon.h"
31
32 GST_DEBUG_CATEGORY_STATIC (gst_wavpack_parse_debug);
33 #define GST_CAT_DEFAULT gst_wavpack_parse_debug
34
35 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
36     GST_PAD_SINK,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("audio/x-wavpack, "
39         "framed = (boolean) false; "
40         "audio/x-wavpack-correction, " "framed = (boolean) false")
41     );
42
43 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
44     GST_PAD_SRC,
45     GST_PAD_SOMETIMES,
46     GST_STATIC_CAPS ("audio/x-wavpack, "
47         "width = (int) { 8, 16, 24, 32 }, "
48         "channels = (int) { 1, 2 }, "
49         "rate = (int) [ 6000, 192000 ], " "framed = (boolean) true")
50     );
51
52 static GstStaticPadTemplate wvc_src_factory = GST_STATIC_PAD_TEMPLATE ("wvcsrc",
53     GST_PAD_SRC,
54     GST_PAD_SOMETIMES,
55     GST_STATIC_CAPS ("audio/x-wavpack-correction, " "framed = (boolean) true")
56     );
57
58 static gboolean gst_wavepack_parse_sink_activate (GstPad * sinkpad);
59 static gboolean
60 gst_wavepack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active);
61
62 static void gst_wavpack_parse_loop (GstElement * element);
63 static GstStateChangeReturn gst_wavpack_parse_change_state (GstElement *
64     element, GstStateChange transition);
65 static void gst_wavpack_parse_reset (GstWavpackParse * wavpackparse);
66 static gint64 gst_wavpack_parse_get_upstream_length (GstWavpackParse * wvparse);
67 static GstBuffer *gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse,
68     gint64 offset, guint size, GstFlowReturn * flow);
69
70 GST_BOILERPLATE (GstWavpackParse, gst_wavpack_parse, GstElement,
71     GST_TYPE_ELEMENT)
72
73      static void gst_wavpack_parse_base_init (gpointer klass)
74 {
75   static GstElementDetails plugin_details =
76       GST_ELEMENT_DETAILS ("WavePack parser",
77       "Codec/Demuxer/Audio",
78       "Parses Wavpack files",
79       "Arwed v. Merkatz <v.merkatz@gmx.net>");
80   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
81
82   gst_element_class_add_pad_template (element_class,
83       gst_static_pad_template_get (&src_factory));
84   gst_element_class_add_pad_template (element_class,
85       gst_static_pad_template_get (&wvc_src_factory));
86   gst_element_class_add_pad_template (element_class,
87       gst_static_pad_template_get (&sink_factory));
88   gst_element_class_set_details (element_class, &plugin_details);
89 }
90
91 static void
92 gst_wavpack_parse_dispose (GObject * object)
93 {
94   gst_wavpack_parse_reset (GST_WAVPACK_PARSE (object));
95   G_OBJECT_CLASS (parent_class)->dispose (object);
96 }
97
98 static void
99 gst_wavpack_parse_class_init (GstWavpackParseClass * klass)
100 {
101   GObjectClass *gobject_class;
102   GstElementClass *gstelement_class;
103
104   gobject_class = (GObjectClass *) klass;
105   gstelement_class = (GstElementClass *) klass;
106
107   gobject_class->dispose = gst_wavpack_parse_dispose;
108   gstelement_class->change_state =
109       GST_DEBUG_FUNCPTR (gst_wavpack_parse_change_state);
110 }
111
112 static GstWavpackParseIndexEntry *
113 gst_wavpack_parse_index_get_last_entry (GstWavpackParse * wvparse)
114 {
115   gint last;
116
117   g_assert (wvparse->entries != NULL);
118   g_assert (wvparse->entries->len > 0);
119
120   last = wvparse->entries->len - 1;
121   return &g_array_index (wvparse->entries, GstWavpackParseIndexEntry, last);
122 }
123
124 static GstWavpackParseIndexEntry *
125 gst_wavpack_parse_index_get_entry_from_sample (GstWavpackParse * wvparse,
126     gint64 sample_offset)
127 {
128   gint i;
129
130   if (wvparse->entries == NULL || wvparse->entries->len == 0)
131     return NULL;
132
133   for (i = wvparse->entries->len - 1; i >= 0; --i) {
134     GstWavpackParseIndexEntry *entry;
135
136     entry = &g_array_index (wvparse->entries, GstWavpackParseIndexEntry, i);
137
138     GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
139         " byte %" G_GINT64_FORMAT, entry->sample_offset, entry->byte_offset);
140
141     if (entry->sample_offset <= sample_offset &&
142         sample_offset < entry->sample_offset_end) {
143       GST_LOG_OBJECT (wvparse, "found match");
144       return entry;
145     }
146   }
147   GST_LOG_OBJECT (wvparse, "no match in index");
148   return NULL;
149 }
150
151 static void
152 gst_wavpack_parse_index_append_entry (GstWavpackParse * wvparse,
153     gint64 byte_offset, gint64 sample_offset, gint64 num_samples)
154 {
155   GstWavpackParseIndexEntry entry;
156
157   if (wvparse->entries == NULL) {
158     wvparse->entries = g_array_new (FALSE, TRUE,
159         sizeof (GstWavpackParseIndexEntry));
160   } else {
161     /* do we have this one already? */
162     entry = *gst_wavpack_parse_index_get_last_entry (wvparse);
163     if (entry.byte_offset >= byte_offset)
164       return;
165   }
166
167   GST_LOG_OBJECT (wvparse, "Adding index entry %8" G_GINT64_FORMAT " - %"
168       GST_TIME_FORMAT " @ offset 0x%08" G_GINT64_MODIFIER "x", sample_offset,
169       GST_TIME_ARGS (gst_util_uint64_scale_int (sample_offset,
170               GST_SECOND, wvparse->samplerate)), byte_offset);
171
172   entry.byte_offset = byte_offset;
173   entry.sample_offset = sample_offset;
174   entry.sample_offset_end = sample_offset + num_samples;
175   g_array_append_val (wvparse->entries, entry);
176 }
177
178 static void
179 gst_wavpack_parse_reset (GstWavpackParse * wavpackparse)
180 {
181   wavpackparse->total_samples = 0;
182   wavpackparse->samplerate = 0;
183   wavpackparse->channels = 0;
184
185   gst_segment_init (&wavpackparse->segment, GST_FORMAT_UNDEFINED);
186
187   wavpackparse->current_offset = 0;
188   wavpackparse->need_newsegment = TRUE;
189   wavpackparse->upstream_length = -1;
190
191   if (wavpackparse->entries) {
192     g_array_free (wavpackparse->entries, TRUE);
193     wavpackparse->entries = NULL;
194   }
195
196   if (wavpackparse->srcpad != NULL) {
197     gboolean res;
198
199     GST_DEBUG_OBJECT (wavpackparse, "Removing src pad");
200     res = gst_element_remove_pad (GST_ELEMENT (wavpackparse),
201         wavpackparse->srcpad);
202     g_return_if_fail (res != FALSE);
203     gst_object_unref (wavpackparse->srcpad);
204     wavpackparse->srcpad = NULL;
205   }
206 }
207
208 static gboolean
209 gst_wavpack_parse_src_query (GstPad * pad, GstQuery * query)
210 {
211   GstWavpackParse *wavpackparse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
212   GstFormat format;
213   gboolean ret = FALSE;
214
215   switch (GST_QUERY_TYPE (query)) {
216     case GST_QUERY_POSITION:{
217       gint64 cur, len;
218       guint rate;
219
220       GST_OBJECT_LOCK (wavpackparse);
221       cur = wavpackparse->segment.last_stop;
222       len = wavpackparse->total_samples;
223       rate = wavpackparse->samplerate;
224       GST_OBJECT_UNLOCK (wavpackparse);
225
226       if (len <= 0 || rate == 0) {
227         GST_DEBUG_OBJECT (wavpackparse, "haven't read header yet");
228         break;
229       }
230
231       gst_query_parse_position (query, &format, NULL);
232
233       switch (format) {
234         case GST_FORMAT_TIME:
235           cur = gst_util_uint64_scale_int (cur, GST_SECOND, rate);
236           gst_query_set_position (query, GST_FORMAT_TIME, cur);
237           ret = TRUE;
238           break;
239         case GST_FORMAT_DEFAULT:
240           gst_query_set_position (query, GST_FORMAT_DEFAULT, cur);
241           ret = TRUE;
242           break;
243         default:
244           GST_DEBUG_OBJECT (wavpackparse, "cannot handle position query in "
245               "%s format", gst_format_get_name (format));
246           break;
247       }
248       break;
249     }
250     case GST_QUERY_DURATION:{
251       gint64 len;
252       guint rate;
253
254       GST_OBJECT_LOCK (wavpackparse);
255       rate = wavpackparse->samplerate;
256       len = wavpackparse->total_samples;
257       GST_OBJECT_UNLOCK (wavpackparse);
258
259       if (len <= 0 || rate == 0) {
260         GST_DEBUG_OBJECT (wavpackparse, "haven't read header yet");
261         break;
262       }
263
264       gst_query_parse_duration (query, &format, NULL);
265
266       switch (format) {
267         case GST_FORMAT_TIME:
268           len = gst_util_uint64_scale_int (len, GST_SECOND, rate);
269           gst_query_set_duration (query, GST_FORMAT_TIME, len);
270           ret = TRUE;
271           break;
272         case GST_FORMAT_DEFAULT:
273           gst_query_set_duration (query, GST_FORMAT_DEFAULT, len);
274           ret = TRUE;
275           break;
276         default:
277           GST_DEBUG_OBJECT (wavpackparse, "cannot handle duration query in "
278               "%s format", gst_format_get_name (format));
279           break;
280       }
281       break;
282     }
283     default:{
284       ret = gst_pad_query_default (pad, query);
285       break;
286     }
287   }
288
289   gst_object_unref (wavpackparse);
290   return ret;
291
292 }
293
294 /* returns TRUE on success, with byte_offset set to the offset of the
295  * wavpack chunk containing the sample requested. start_sample will be
296  * set to the first sample in the chunk starting at byte_offset.
297  * Scanning from the last known header offset to the wanted position
298  * when seeking forward isn't very clever, but seems fast enough in
299  * practice and has the nice side effect of populating our index
300  * table */
301 static gboolean
302 gst_wavpack_parse_scan_to_find_sample (GstWavpackParse * parse,
303     gint64 sample, gint64 * byte_offset, gint64 * start_sample)
304 {
305   GstWavpackParseIndexEntry *entry;
306   GstFlowReturn ret;
307   gint64 off = 0;
308
309   /* first, check if we have to scan at all */
310   entry = gst_wavpack_parse_index_get_entry_from_sample (parse, sample);
311   if (entry) {
312     *byte_offset = entry->byte_offset;
313     *start_sample = entry->sample_offset;
314     GST_LOG_OBJECT (parse, "Found index entry: sample %" G_GINT64_FORMAT
315         " @ offset %" G_GINT64_FORMAT, entry->sample_offset,
316         entry->byte_offset);
317     return TRUE;
318   }
319
320   GST_LOG_OBJECT (parse, "No matching entry in index, scanning file ...");
321
322   /* if we have an index, we can start scanning from the last known offset
323    * in there, after all we know our wanted sample is not in the index */
324   if (parse->entries && parse->entries->len > 0) {
325     GstWavpackParseIndexEntry *entry;
326
327     entry = gst_wavpack_parse_index_get_last_entry (parse);
328     off = entry->byte_offset;
329   }
330
331   /* now scan forward until we find the chunk we're looking for or hit EOS */
332   do {
333     WavpackHeader header = { {0,}
334     , 0,
335     };
336     GstBuffer *buf;
337
338     buf = gst_wavpack_parse_pull_buffer (parse, off, sizeof (WavpackHeader),
339         &ret);
340
341     if (buf == NULL)
342       break;
343
344     gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
345     gst_buffer_unref (buf);
346
347     gst_wavpack_parse_index_append_entry (parse, off, header.block_index,
348         header.block_samples);
349
350     if (header.block_index <= sample &&
351         sample < (header.block_index + header.block_samples)) {
352       *byte_offset = off;
353       *start_sample = header.block_index;
354       return TRUE;
355     }
356
357     off += header.ckSize + 8;
358   } while (1);
359
360   GST_DEBUG_OBJECT (parse, "scan failed: %s (off=0x%08" G_GINT64_MODIFIER "x)",
361       gst_flow_get_name (ret), off);
362
363   return FALSE;
364 }
365
366 static gboolean
367 gst_wavpack_parse_send_newsegment (GstWavpackParse * wvparse, gboolean update)
368 {
369   GstSegment *s = &wvparse->segment;
370   gboolean ret;
371   gint64 stop_time = -1;
372   gint64 start_time = 0;
373   gint64 cur_pos_time;
374   gint64 diff;
375
376   /* segment is in DEFAULT format, but we want to send a TIME newsegment */
377   start_time = gst_util_uint64_scale_int (s->start, GST_SECOND,
378       wvparse->samplerate);
379
380   if (s->stop != -1) {
381     stop_time = gst_util_uint64_scale_int (s->stop, GST_SECOND,
382         wvparse->samplerate);
383   }
384
385   GST_DEBUG_OBJECT (wvparse, "sending newsegment from %" GST_TIME_FORMAT
386       " to %" GST_TIME_FORMAT, GST_TIME_ARGS (start_time),
387       GST_TIME_ARGS (stop_time));
388
389   /* after a seek, s->last_stop will point to a chunk boundary, ie. from
390    * which sample we will start sending data again, while s->start will
391    * point to the sample we actually want to seek to and want to start
392    * playing right after the seek. Adjust clock-time for the difference
393    * so we start playing from start_time */
394   cur_pos_time = gst_util_uint64_scale_int (s->last_stop, GST_SECOND,
395       wvparse->samplerate);
396   diff = start_time - cur_pos_time;
397
398   ret = gst_pad_push_event (wvparse->srcpad,
399       gst_event_new_new_segment (update, s->rate, GST_FORMAT_TIME,
400           start_time, stop_time, start_time - diff));
401
402   return ret;
403 }
404
405 static gboolean
406 gst_wavpack_parse_handle_seek_event (GstWavpackParse * wvparse,
407     GstEvent * event)
408 {
409   GstSeekFlags seek_flags;
410   GstSeekType start_type;
411   GstSeekType stop_type;
412   GstSegment segment;
413   GstFormat format;
414   gboolean only_update;
415   gboolean flush, ret;
416   gdouble speed;
417   gint64 stop;
418   gint64 start;                 /* sample we want to seek to                  */
419   gint64 byte_offset;           /* byte offset the chunk we seek to starts at */
420   gint64 chunk_start;           /* first sample in chunk we seek to           */
421   guint rate;
422
423   gst_event_parse_seek (event, &speed, &format, &seek_flags, &start_type,
424       &start, &stop_type, &stop);
425
426   if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME) {
427     GST_DEBUG ("seeking is only supported in TIME or DEFAULT format");
428     return FALSE;
429   }
430
431   if (speed < 0.0) {
432     GST_DEBUG ("only forward playback supported, rate %f not allowed", speed);
433     return FALSE;
434   }
435
436   GST_OBJECT_LOCK (wvparse);
437
438   rate = wvparse->samplerate;
439   if (rate == 0) {
440     GST_OBJECT_UNLOCK (wvparse);
441     GST_DEBUG ("haven't read header yet");
442     return FALSE;
443   }
444
445   /* convert from time to samples if necessary */
446   if (format == GST_FORMAT_TIME) {
447     if (start_type != GST_SEEK_TYPE_NONE)
448       start = gst_util_uint64_scale_int (start, rate, GST_SECOND);
449     if (stop_type != GST_SEEK_TYPE_NONE)
450       stop = gst_util_uint64_scale_int (stop, rate, GST_SECOND);
451   }
452
453   flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
454
455   if (start < 0) {
456     GST_OBJECT_UNLOCK (wvparse);
457     GST_DEBUG_OBJECT (wvparse, "Invalid start sample %" G_GINT64_FORMAT, start);
458     return FALSE;
459   }
460
461   /* operate on segment copy until we know the seek worked */
462   segment = wvparse->segment;
463
464   gst_segment_set_seek (&segment, speed, GST_FORMAT_DEFAULT,
465       seek_flags, start_type, start, stop_type, stop, &only_update);
466
467 #if 0
468   if (only_update) {
469     wvparse->segment = segment;
470     gst_wavpack_parse_send_newsegment (wvparse, TRUE);
471     goto done;
472   }
473 #endif
474
475   gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_start ());
476
477   if (flush) {
478     gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_start ());
479   } else {
480     gst_pad_stop_task (wvparse->sinkpad);
481   }
482
483   GST_PAD_STREAM_LOCK (wvparse->sinkpad);
484
485   gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_stop ());
486
487   if (flush) {
488     gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_stop ());
489   }
490
491   GST_DEBUG_OBJECT (wvparse, "Performing seek to %" GST_TIME_FORMAT " sample %"
492       G_GINT64_FORMAT, GST_TIME_ARGS (segment.start * GST_SECOND / rate),
493       start);
494
495   ret = gst_wavpack_parse_scan_to_find_sample (wvparse, segment.start,
496       &byte_offset, &chunk_start);
497
498   if (ret) {
499     GST_DEBUG_OBJECT (wvparse, "new offset: %" G_GINT64_FORMAT, byte_offset);
500     wvparse->current_offset = byte_offset;
501     /* we want to send a newsegment event with the actual seek position
502      * as start, even though our first buffer might start before the
503      * configured segment. We leave it up to the decoder or sink to crop
504      * the output buffers accordingly */
505     wvparse->segment = segment;
506     wvparse->segment.last_stop = chunk_start;
507     gst_wavpack_parse_send_newsegment (wvparse, FALSE);
508   } else {
509     GST_DEBUG_OBJECT (wvparse, "seek failed: don't know where to seek to");
510   }
511
512   GST_PAD_STREAM_UNLOCK (wvparse->sinkpad);
513   GST_OBJECT_UNLOCK (wvparse);
514
515   gst_pad_start_task (wvparse->sinkpad,
516       (GstTaskFunction) gst_wavpack_parse_loop, wvparse);
517
518   return ret;
519 }
520
521 static gboolean
522 gst_wavpack_parse_src_event (GstPad * pad, GstEvent * event)
523 {
524   GstWavpackParse *wavpackparse;
525   gboolean ret;
526
527   wavpackparse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
528
529   switch (GST_EVENT_TYPE (event)) {
530     case GST_EVENT_SEEK:
531       ret = gst_wavpack_parse_handle_seek_event (wavpackparse, event);
532       break;
533     default:
534       ret = gst_pad_event_default (pad, event);
535       break;
536   }
537
538   gst_object_unref (wavpackparse);
539   return ret;
540 }
541
542 static void
543 gst_wavpack_parse_init (GstWavpackParse * wavpackparse,
544     GstWavpackParseClass * gclass)
545 {
546   GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavpackparse);
547   GstPadTemplate *tmpl;
548
549   tmpl = gst_element_class_get_pad_template (klass, "sink");
550   wavpackparse->sinkpad = gst_pad_new_from_template (tmpl, "sink");
551
552   gst_pad_set_activate_function (wavpackparse->sinkpad,
553       GST_DEBUG_FUNCPTR (gst_wavepack_parse_sink_activate));
554
555   gst_pad_set_activatepull_function (wavpackparse->sinkpad,
556       GST_DEBUG_FUNCPTR (gst_wavepack_parse_sink_activate_pull));
557
558   gst_element_add_pad (GST_ELEMENT (wavpackparse), wavpackparse->sinkpad);
559
560   wavpackparse->srcpad = NULL;
561   gst_wavpack_parse_reset (wavpackparse);
562 }
563
564 static gint64
565 gst_wavpack_parse_get_upstream_length (GstWavpackParse * wavpackparse)
566 {
567   GstPad *peer;
568   gint64 length = -1;
569
570   peer = gst_pad_get_peer (wavpackparse->sinkpad);
571   if (peer) {
572     GstFormat format = GST_FORMAT_BYTES;
573
574     if (!gst_pad_query_duration (peer, &format, &length)) {
575       length = -1;
576     } else {
577       GST_DEBUG ("upstream length: %" G_GINT64_FORMAT, length);
578     }
579     gst_object_unref (peer);
580   } else {
581     GST_DEBUG ("no peer!");
582   }
583
584   return length;
585 }
586
587 static GstBuffer *
588 gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse, gint64 offset,
589     guint size, GstFlowReturn * flow)
590 {
591   GstFlowReturn flow_ret;
592   GstBuffer *buf = NULL;
593
594   if (offset + size >= wvparse->upstream_length) {
595     wvparse->upstream_length = gst_wavpack_parse_get_upstream_length (wvparse);
596     if (offset + size >= wvparse->upstream_length) {
597       GST_DEBUG_OBJECT (wvparse, "EOS: %" G_GINT64_FORMAT " + %u > %"
598           G_GINT64_FORMAT, offset, size, wvparse->upstream_length);
599       flow_ret = GST_FLOW_UNEXPECTED;
600       goto done;
601     }
602   }
603
604   flow_ret = gst_pad_pull_range (wvparse->sinkpad, offset, size, &buf);
605
606   if (flow_ret != GST_FLOW_OK) {
607     GST_DEBUG_OBJECT (wvparse, "pull_range (%" G_GINT64_FORMAT ", %u) "
608         "failed, flow: %s", offset, size, gst_flow_get_name (flow_ret));
609     return NULL;
610   }
611
612   if (GST_BUFFER_SIZE (buf) < size) {
613     GST_DEBUG_OBJECT (wvparse, "Short read at offset %" G_GINT64_FORMAT
614         ", got only %u of %u bytes", offset, GST_BUFFER_SIZE (buf), size);
615     gst_buffer_unref (buf);
616     buf = NULL;
617     flow_ret = GST_FLOW_UNEXPECTED;
618   }
619
620 done:
621   if (flow)
622     *flow = flow_ret;
623   return buf;
624 }
625
626 static gboolean
627 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
628     WavpackHeader * header)
629 {
630   WavpackMetadata meta;
631   GstCaps *caps = NULL;
632   guchar *bufptr;
633
634   g_assert (wvparse->srcpad == NULL);
635
636   bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
637
638   while (read_metadata_buff (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
639     switch (meta.id) {
640       case ID_WVC_BITSTREAM:{
641         caps = gst_caps_new_simple ("audio/x-wavpack-correction",
642             "framed", G_TYPE_BOOLEAN, TRUE, NULL);
643         wvparse->srcpad =
644             gst_pad_new_from_template (gst_element_class_get_pad_template
645             (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
646         break;
647       }
648       case ID_RIFF_HEADER:{
649         WaveHeader wheader;
650
651         /* skip RiffChunkHeader and ChunkHeader */
652         g_memmove (&wheader, meta.data + 20, sizeof (WaveHeader));
653         little_endian_to_native (&wheader, WaveHeaderFormat);
654         wvparse->samplerate = wheader.SampleRate;
655         wvparse->channels = wheader.NumChannels;
656         wvparse->total_samples = header->total_samples;
657         caps = gst_caps_new_simple ("audio/x-wavpack",
658             "width", G_TYPE_INT, wheader.BitsPerSample,
659             "channels", G_TYPE_INT, wvparse->channels,
660             "rate", G_TYPE_INT, wvparse->samplerate,
661             "framed", G_TYPE_BOOLEAN, TRUE, NULL);
662         wvparse->srcpad =
663             gst_pad_new_from_template (gst_element_class_get_pad_template
664             (GST_ELEMENT_GET_CLASS (wvparse), "src"), "src");
665         break;
666       }
667       default:{
668         GST_WARNING_OBJECT (wvparse, "unhandled ID: 0x%02x", meta.id);
669         break;
670       }
671     }
672     if (caps != NULL)
673       break;
674   }
675
676   if (caps == NULL || wvparse->srcpad == NULL)
677     return FALSE;
678
679   GST_DEBUG_OBJECT (wvparse, "Added src pad with caps %" GST_PTR_FORMAT, caps);
680
681   gst_pad_set_query_function (wvparse->srcpad,
682       GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_query));
683   gst_pad_set_event_function (wvparse->srcpad,
684       GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_event));
685
686   gst_pad_set_caps (wvparse->srcpad, caps);
687   gst_pad_use_fixed_caps (wvparse->srcpad);
688
689   gst_object_ref (wvparse->srcpad);
690   gst_element_add_pad (GST_ELEMENT (wvparse), wvparse->srcpad);
691   gst_element_no_more_pads (GST_ELEMENT (wvparse));
692
693   return TRUE;
694 }
695
696 static void
697 gst_wavpack_parse_loop (GstElement * element)
698 {
699   GstWavpackParse *wavpackparse = GST_WAVPACK_PARSE (element);
700   GstFlowReturn flow_ret;
701   WavpackHeader header = { {0,}, 0, };
702   GstBuffer *buf = NULL;
703
704   GST_LOG_OBJECT (wavpackparse, "Current offset: %" G_GINT64_FORMAT,
705       wavpackparse->current_offset);
706
707   buf = gst_wavpack_parse_pull_buffer (wavpackparse,
708       wavpackparse->current_offset, sizeof (WavpackHeader), &flow_ret);
709
710   if (buf == NULL && flow_ret == GST_FLOW_UNEXPECTED) {
711     goto eos;
712   } else if (buf == NULL) {
713     goto pause;
714   }
715
716   gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
717   gst_buffer_unref (buf);
718
719   GST_LOG_OBJECT (wavpackparse, "Read header at offset %" G_GINT64_FORMAT
720       ": chunk size = %u+8", wavpackparse->current_offset, header.ckSize);
721
722   buf = gst_wavpack_parse_pull_buffer (wavpackparse,
723       wavpackparse->current_offset, header.ckSize + 8, &flow_ret);
724
725   if (buf == NULL && flow_ret == GST_FLOW_UNEXPECTED) {
726     goto eos;
727   } else if (buf == NULL) {
728     goto pause;
729   }
730
731   if (wavpackparse->srcpad == NULL) {
732     if (!gst_wavpack_parse_create_src_pad (wavpackparse, buf, &header)) {
733       GST_ELEMENT_ERROR (wavpackparse, STREAM, DECODE, (NULL), (NULL));
734       goto pause;
735     }
736   }
737
738   gst_wavpack_parse_index_append_entry (wavpackparse,
739       wavpackparse->current_offset, header.block_index, header.block_samples);
740
741   wavpackparse->current_offset += header.ckSize + 8;
742
743   wavpackparse->segment.last_stop = header.block_index;
744
745   if (wavpackparse->need_newsegment) {
746     if (gst_wavpack_parse_send_newsegment (wavpackparse, FALSE))
747       wavpackparse->need_newsegment = FALSE;
748   }
749
750   GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (header.block_index,
751       GST_SECOND, wavpackparse->samplerate);
752   GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (header.block_samples,
753       GST_SECOND, wavpackparse->samplerate);
754   GST_BUFFER_OFFSET (buf) = header.block_index;
755   gst_buffer_set_caps (buf, GST_PAD_CAPS (wavpackparse->srcpad));
756
757   GST_LOG_OBJECT (wavpackparse, "Pushing buffer with time %" GST_TIME_FORMAT,
758       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
759
760   flow_ret = gst_pad_push (wavpackparse->srcpad, buf);
761   if (flow_ret != GST_FLOW_OK) {
762     GST_DEBUG_OBJECT (wavpackparse, "Push failed, flow: %s",
763         gst_flow_get_name (flow_ret));
764     goto pause;
765   }
766
767   return;
768
769 eos:
770   {
771     GST_DEBUG_OBJECT (wavpackparse, "sending EOS");
772     if (wavpackparse->srcpad) {
773       gst_pad_push_event (wavpackparse->srcpad, gst_event_new_eos ());
774     }
775     /* fall through and pause task */
776   }
777 pause:
778   {
779     GST_DEBUG_OBJECT (wavpackparse, "Pausing task");
780     gst_pad_pause_task (wavpackparse->sinkpad);
781     return;
782   }
783 }
784
785 static GstStateChangeReturn
786 gst_wavpack_parse_change_state (GstElement * element, GstStateChange transition)
787 {
788   GstWavpackParse *wvparse = GST_WAVPACK_PARSE (element);
789   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
790
791   switch (transition) {
792     case GST_STATE_CHANGE_READY_TO_PAUSED:
793       gst_segment_init (&wvparse->segment, GST_FORMAT_DEFAULT);
794       wvparse->segment.last_stop = 0;
795     default:
796       break;
797   }
798
799   if (GST_ELEMENT_CLASS (parent_class)->change_state)
800     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
801
802   switch (transition) {
803     case GST_STATE_CHANGE_PAUSED_TO_READY:
804       gst_wavpack_parse_reset (wvparse);
805       break;
806     default:
807       break;
808   }
809
810   return ret;
811 }
812
813
814 static gboolean
815 gst_wavepack_parse_sink_activate (GstPad * sinkpad)
816 {
817   if (gst_pad_check_pull_range (sinkpad)) {
818     return gst_pad_activate_pull (sinkpad, TRUE);
819   } else {
820     return FALSE;
821   }
822 }
823
824 static gboolean
825 gst_wavepack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
826 {
827   gboolean result;
828
829   if (active) {
830     result = gst_pad_start_task (sinkpad,
831         (GstTaskFunction) gst_wavpack_parse_loop, GST_PAD_PARENT (sinkpad));
832   } else {
833     result = gst_pad_stop_task (sinkpad);
834   }
835
836   return result;
837 }
838
839 gboolean
840 gst_wavpack_parse_plugin_init (GstPlugin * plugin)
841 {
842   if (!gst_element_register (plugin, "wavpackparse",
843           GST_RANK_PRIMARY, GST_TYPE_WAVPACK_PARSE)) {
844     return FALSE;
845   }
846
847   GST_DEBUG_CATEGORY_INIT (gst_wavpack_parse_debug, "wavpackparse", 0,
848       "wavpack file parser");
849
850   return TRUE;
851 }