f00e337e6e808b9240f359cd006a67fce7e30e86
[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     "Wavpack file parser",
77     "Codec/Demuxer/Audio",
78     "Parses Wavpack files",
79     "Arwed v. Merkatz <v.merkatz@gmx.net>"
80   };
81   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
82
83   gst_element_class_add_pad_template (element_class,
84       gst_static_pad_template_get (&src_factory));
85   gst_element_class_add_pad_template (element_class,
86       gst_static_pad_template_get (&wvc_src_factory));
87   gst_element_class_add_pad_template (element_class,
88       gst_static_pad_template_get (&sink_factory));
89   gst_element_class_set_details (element_class, &plugin_details);
90 }
91
92 static void
93 gst_wavpack_parse_dispose (GObject * object)
94 {
95   gst_wavpack_parse_reset (GST_WAVPACK_PARSE (object));
96   G_OBJECT_CLASS (parent_class)->dispose (object);
97 }
98
99 static void
100 gst_wavpack_parse_class_init (GstWavpackParseClass * klass)
101 {
102   GObjectClass *gobject_class;
103   GstElementClass *gstelement_class;
104
105   gobject_class = (GObjectClass *) klass;
106   gstelement_class = (GstElementClass *) klass;
107
108   gobject_class->dispose = gst_wavpack_parse_dispose;
109   gstelement_class->change_state =
110       GST_DEBUG_FUNCPTR (gst_wavpack_parse_change_state);
111 }
112
113 static GstWavpackParseIndexEntry *
114 gst_wavpack_parse_index_get_last_entry (GstWavpackParse * wvparse)
115 {
116   gint last;
117
118   g_assert (wvparse->entries != NULL);
119   g_assert (wvparse->entries->len > 0);
120
121   last = wvparse->entries->len - 1;
122   return &g_array_index (wvparse->entries, GstWavpackParseIndexEntry, last);
123 }
124
125 static GstWavpackParseIndexEntry *
126 gst_wavpack_parse_index_get_entry_from_sample (GstWavpackParse * wvparse,
127     gint64 sample_offset)
128 {
129   gint i;
130
131   if (wvparse->entries == NULL || wvparse->entries->len == 0)
132     return NULL;
133
134   for (i = wvparse->entries->len - 1; i >= 0; --i) {
135     GstWavpackParseIndexEntry *entry;
136
137     entry = &g_array_index (wvparse->entries, GstWavpackParseIndexEntry, i);
138
139     GST_LOG_OBJECT (wvparse, "Index entry %03u: sample %" G_GINT64_FORMAT " @"
140         " byte %" G_GINT64_FORMAT, entry->sample_offset, entry->byte_offset);
141
142     if (entry->sample_offset <= sample_offset &&
143         sample_offset < entry->sample_offset_end) {
144       GST_LOG_OBJECT (wvparse, "found match");
145       return entry;
146     }
147   }
148   GST_LOG_OBJECT (wvparse, "no match in index");
149   return NULL;
150 }
151
152 static void
153 gst_wavpack_parse_index_append_entry (GstWavpackParse * wvparse,
154     gint64 byte_offset, gint64 sample_offset, gint64 num_samples)
155 {
156   GstWavpackParseIndexEntry entry;
157
158   if (wvparse->entries == NULL) {
159     wvparse->entries = g_array_new (FALSE, TRUE,
160         sizeof (GstWavpackParseIndexEntry));
161   } else {
162     /* do we have this one already? */
163     entry = *gst_wavpack_parse_index_get_last_entry (wvparse);
164     if (entry.byte_offset >= byte_offset)
165       return;
166   }
167
168   GST_LOG_OBJECT (wvparse, "Adding index entry %8" G_GINT64_FORMAT " - %"
169       GST_TIME_FORMAT " @ offset 0x%08" G_GINT64_MODIFIER "x", sample_offset,
170       GST_TIME_ARGS (gst_util_uint64_scale_int (sample_offset,
171               GST_SECOND, wvparse->samplerate)), byte_offset);
172
173   entry.byte_offset = byte_offset;
174   entry.sample_offset = sample_offset;
175   entry.sample_offset_end = sample_offset + num_samples;
176   g_array_append_val (wvparse->entries, entry);
177 }
178
179 static void
180 gst_wavpack_parse_reset (GstWavpackParse * wavpackparse)
181 {
182   wavpackparse->total_samples = 0;
183   wavpackparse->samplerate = 0;
184   wavpackparse->channels = 0;
185
186   gst_segment_init (&wavpackparse->segment, GST_FORMAT_UNDEFINED);
187
188   wavpackparse->current_offset = 0;
189   wavpackparse->need_newsegment = TRUE;
190   wavpackparse->upstream_length = -1;
191
192   if (wavpackparse->entries) {
193     g_array_free (wavpackparse->entries, TRUE);
194     wavpackparse->entries = NULL;
195   }
196
197   if (wavpackparse->srcpad != NULL) {
198     gboolean res;
199
200     GST_DEBUG_OBJECT (wavpackparse, "Removing src pad");
201     res = gst_element_remove_pad (GST_ELEMENT (wavpackparse),
202         wavpackparse->srcpad);
203     g_return_if_fail (res != FALSE);
204     gst_object_unref (wavpackparse->srcpad);
205     wavpackparse->srcpad = NULL;
206   }
207 }
208
209 static gboolean
210 gst_wavpack_parse_src_query (GstPad * pad, GstQuery * query)
211 {
212   GstWavpackParse *wavpackparse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
213   GstFormat format;
214   gboolean ret = FALSE;
215
216   switch (GST_QUERY_TYPE (query)) {
217     case GST_QUERY_POSITION:{
218       gint64 cur, len;
219       guint rate;
220
221       GST_OBJECT_LOCK (wavpackparse);
222       cur = wavpackparse->segment.last_stop;
223       len = wavpackparse->total_samples;
224       rate = wavpackparse->samplerate;
225       GST_OBJECT_UNLOCK (wavpackparse);
226
227       if (len <= 0 || rate == 0) {
228         GST_DEBUG_OBJECT (wavpackparse, "haven't read header yet");
229         break;
230       }
231
232       gst_query_parse_position (query, &format, NULL);
233
234       switch (format) {
235         case GST_FORMAT_TIME:
236           cur = gst_util_uint64_scale_int (cur, GST_SECOND, rate);
237           gst_query_set_position (query, GST_FORMAT_TIME, cur);
238           ret = TRUE;
239           break;
240         case GST_FORMAT_DEFAULT:
241           gst_query_set_position (query, GST_FORMAT_DEFAULT, cur);
242           ret = TRUE;
243           break;
244         default:
245           GST_DEBUG_OBJECT (wavpackparse, "cannot handle position query in "
246               "%s format", gst_format_get_name (format));
247           break;
248       }
249       break;
250     }
251     case GST_QUERY_DURATION:{
252       gint64 len;
253       guint rate;
254
255       GST_OBJECT_LOCK (wavpackparse);
256       rate = wavpackparse->samplerate;
257       len = wavpackparse->total_samples;
258       GST_OBJECT_UNLOCK (wavpackparse);
259
260       if (len <= 0 || rate == 0) {
261         GST_DEBUG_OBJECT (wavpackparse, "haven't read header yet");
262         break;
263       }
264
265       gst_query_parse_duration (query, &format, NULL);
266
267       switch (format) {
268         case GST_FORMAT_TIME:
269           len = gst_util_uint64_scale_int (len, GST_SECOND, rate);
270           gst_query_set_duration (query, GST_FORMAT_TIME, len);
271           ret = TRUE;
272           break;
273         case GST_FORMAT_DEFAULT:
274           gst_query_set_duration (query, GST_FORMAT_DEFAULT, len);
275           ret = TRUE;
276           break;
277         default:
278           GST_DEBUG_OBJECT (wavpackparse, "cannot handle duration query in "
279               "%s format", gst_format_get_name (format));
280           break;
281       }
282       break;
283     }
284     default:{
285       ret = gst_pad_query_default (pad, query);
286       break;
287     }
288   }
289
290   gst_object_unref (wavpackparse);
291   return ret;
292
293 }
294
295 /* returns TRUE on success, with byte_offset set to the offset of the
296  * wavpack chunk containing the sample requested. start_sample will be
297  * set to the first sample in the chunk starting at byte_offset.
298  * Scanning from the last known header offset to the wanted position
299  * when seeking forward isn't very clever, but seems fast enough in
300  * practice and has the nice side effect of populating our index
301  * table */
302 static gboolean
303 gst_wavpack_parse_scan_to_find_sample (GstWavpackParse * parse,
304     gint64 sample, gint64 * byte_offset, gint64 * start_sample)
305 {
306   GstWavpackParseIndexEntry *entry;
307   GstFlowReturn ret;
308   gint64 off = 0;
309
310   /* first, check if we have to scan at all */
311   entry = gst_wavpack_parse_index_get_entry_from_sample (parse, sample);
312   if (entry) {
313     *byte_offset = entry->byte_offset;
314     *start_sample = entry->sample_offset;
315     GST_LOG_OBJECT (parse, "Found index entry: sample %" G_GINT64_FORMAT
316         " @ offset %" G_GINT64_FORMAT, entry->sample_offset,
317         entry->byte_offset);
318     return TRUE;
319   }
320
321   GST_LOG_OBJECT (parse, "No matching entry in index, scanning file ...");
322
323   /* if we have an index, we can start scanning from the last known offset
324    * in there, after all we know our wanted sample is not in the index */
325   if (parse->entries && parse->entries->len > 0) {
326     GstWavpackParseIndexEntry *entry;
327
328     entry = gst_wavpack_parse_index_get_last_entry (parse);
329     off = entry->byte_offset;
330   }
331
332   /* now scan forward until we find the chunk we're looking for or hit EOS */
333   do {
334     WavpackHeader header = { {0,}
335     , 0, };
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 }