49f87a5fe6fe96fb4a27dd954de31d7265549a92
[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
74 gst_wavpack_parse_base_init (gpointer klass)
75 {
76   static const GstElementDetails plugin_details =
77       GST_ELEMENT_DETAILS ("WavePack parser",
78       "Codec/Demuxer/Audio",
79       "Parses Wavpack files",
80       "Arwed v. Merkatz <v.merkatz@gmx.net>");
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     };
337     GstBuffer *buf;
338
339     buf = gst_wavpack_parse_pull_buffer (parse, off, sizeof (WavpackHeader),
340         &ret);
341
342     if (buf == NULL)
343       break;
344
345     gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
346     gst_buffer_unref (buf);
347
348     gst_wavpack_parse_index_append_entry (parse, off, header.block_index,
349         header.block_samples);
350
351     if (header.block_index <= sample &&
352         sample < (header.block_index + header.block_samples)) {
353       *byte_offset = off;
354       *start_sample = header.block_index;
355       return TRUE;
356     }
357
358     off += header.ckSize + 8;
359   } while (1);
360
361   GST_DEBUG_OBJECT (parse, "scan failed: %s (off=0x%08" G_GINT64_MODIFIER "x)",
362       gst_flow_get_name (ret), off);
363
364   return FALSE;
365 }
366
367 static gboolean
368 gst_wavpack_parse_send_newsegment (GstWavpackParse * wvparse, gboolean update)
369 {
370   GstSegment *s = &wvparse->segment;
371   gboolean ret;
372   gint64 stop_time = -1;
373   gint64 start_time = 0;
374   gint64 cur_pos_time;
375   gint64 diff;
376
377   /* segment is in DEFAULT format, but we want to send a TIME newsegment */
378   start_time = gst_util_uint64_scale_int (s->start, GST_SECOND,
379       wvparse->samplerate);
380
381   if (s->stop != -1) {
382     stop_time = gst_util_uint64_scale_int (s->stop, GST_SECOND,
383         wvparse->samplerate);
384   }
385
386   GST_DEBUG_OBJECT (wvparse, "sending newsegment from %" GST_TIME_FORMAT
387       " to %" GST_TIME_FORMAT, GST_TIME_ARGS (start_time),
388       GST_TIME_ARGS (stop_time));
389
390   /* after a seek, s->last_stop will point to a chunk boundary, ie. from
391    * which sample we will start sending data again, while s->start will
392    * point to the sample we actually want to seek to and want to start
393    * playing right after the seek. Adjust clock-time for the difference
394    * so we start playing from start_time */
395   cur_pos_time = gst_util_uint64_scale_int (s->last_stop, GST_SECOND,
396       wvparse->samplerate);
397   diff = start_time - cur_pos_time;
398
399   ret = gst_pad_push_event (wvparse->srcpad,
400       gst_event_new_new_segment (update, s->rate, GST_FORMAT_TIME,
401           start_time, stop_time, start_time - diff));
402
403   return ret;
404 }
405
406 static gboolean
407 gst_wavpack_parse_handle_seek_event (GstWavpackParse * wvparse,
408     GstEvent * event)
409 {
410   GstSeekFlags seek_flags;
411   GstSeekType start_type;
412   GstSeekType stop_type;
413   GstSegment segment;
414   GstFormat format;
415   gboolean only_update;
416   gboolean flush, ret;
417   gdouble speed;
418   gint64 stop;
419   gint64 start;                 /* sample we want to seek to                  */
420   gint64 byte_offset;           /* byte offset the chunk we seek to starts at */
421   gint64 chunk_start;           /* first sample in chunk we seek to           */
422   guint rate;
423
424   gst_event_parse_seek (event, &speed, &format, &seek_flags, &start_type,
425       &start, &stop_type, &stop);
426
427   if (format != GST_FORMAT_DEFAULT && format != GST_FORMAT_TIME) {
428     GST_DEBUG ("seeking is only supported in TIME or DEFAULT format");
429     return FALSE;
430   }
431
432   if (speed < 0.0) {
433     GST_DEBUG ("only forward playback supported, rate %f not allowed", speed);
434     return FALSE;
435   }
436
437   GST_OBJECT_LOCK (wvparse);
438
439   rate = wvparse->samplerate;
440   if (rate == 0) {
441     GST_OBJECT_UNLOCK (wvparse);
442     GST_DEBUG ("haven't read header yet");
443     return FALSE;
444   }
445
446   /* convert from time to samples if necessary */
447   if (format == GST_FORMAT_TIME) {
448     if (start_type != GST_SEEK_TYPE_NONE)
449       start = gst_util_uint64_scale_int (start, rate, GST_SECOND);
450     if (stop_type != GST_SEEK_TYPE_NONE)
451       stop = gst_util_uint64_scale_int (stop, rate, GST_SECOND);
452   }
453
454   flush = ((seek_flags & GST_SEEK_FLAG_FLUSH) != 0);
455
456   if (start < 0) {
457     GST_OBJECT_UNLOCK (wvparse);
458     GST_DEBUG_OBJECT (wvparse, "Invalid start sample %" G_GINT64_FORMAT, start);
459     return FALSE;
460   }
461
462   /* operate on segment copy until we know the seek worked */
463   segment = wvparse->segment;
464
465   gst_segment_set_seek (&segment, speed, GST_FORMAT_DEFAULT,
466       seek_flags, start_type, start, stop_type, stop, &only_update);
467
468 #if 0
469   if (only_update) {
470     wvparse->segment = segment;
471     gst_wavpack_parse_send_newsegment (wvparse, TRUE);
472     goto done;
473   }
474 #endif
475
476   gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_start ());
477
478   if (flush) {
479     gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_start ());
480   } else {
481     gst_pad_stop_task (wvparse->sinkpad);
482   }
483
484   GST_PAD_STREAM_LOCK (wvparse->sinkpad);
485
486   gst_pad_push_event (wvparse->sinkpad, gst_event_new_flush_stop ());
487
488   if (flush) {
489     gst_pad_push_event (wvparse->srcpad, gst_event_new_flush_stop ());
490   }
491
492   GST_DEBUG_OBJECT (wvparse, "Performing seek to %" GST_TIME_FORMAT " sample %"
493       G_GINT64_FORMAT, GST_TIME_ARGS (segment.start * GST_SECOND / rate),
494       start);
495
496   ret = gst_wavpack_parse_scan_to_find_sample (wvparse, segment.start,
497       &byte_offset, &chunk_start);
498
499   if (ret) {
500     GST_DEBUG_OBJECT (wvparse, "new offset: %" G_GINT64_FORMAT, byte_offset);
501     wvparse->current_offset = byte_offset;
502     /* we want to send a newsegment event with the actual seek position
503      * as start, even though our first buffer might start before the
504      * configured segment. We leave it up to the decoder or sink to crop
505      * the output buffers accordingly */
506     wvparse->segment = segment;
507     wvparse->segment.last_stop = chunk_start;
508     gst_wavpack_parse_send_newsegment (wvparse, FALSE);
509   } else {
510     GST_DEBUG_OBJECT (wvparse, "seek failed: don't know where to seek to");
511   }
512
513   GST_PAD_STREAM_UNLOCK (wvparse->sinkpad);
514   GST_OBJECT_UNLOCK (wvparse);
515
516   gst_pad_start_task (wvparse->sinkpad,
517       (GstTaskFunction) gst_wavpack_parse_loop, wvparse);
518
519   return ret;
520 }
521
522 static gboolean
523 gst_wavpack_parse_src_event (GstPad * pad, GstEvent * event)
524 {
525   GstWavpackParse *wavpackparse;
526   gboolean ret;
527
528   wavpackparse = GST_WAVPACK_PARSE (gst_pad_get_parent (pad));
529
530   switch (GST_EVENT_TYPE (event)) {
531     case GST_EVENT_SEEK:
532       ret = gst_wavpack_parse_handle_seek_event (wavpackparse, event);
533       break;
534     default:
535       ret = gst_pad_event_default (pad, event);
536       break;
537   }
538
539   gst_object_unref (wavpackparse);
540   return ret;
541 }
542
543 static void
544 gst_wavpack_parse_init (GstWavpackParse * wavpackparse,
545     GstWavpackParseClass * gclass)
546 {
547   GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavpackparse);
548   GstPadTemplate *tmpl;
549
550   tmpl = gst_element_class_get_pad_template (klass, "sink");
551   wavpackparse->sinkpad = gst_pad_new_from_template (tmpl, "sink");
552
553   gst_pad_set_activate_function (wavpackparse->sinkpad,
554       GST_DEBUG_FUNCPTR (gst_wavepack_parse_sink_activate));
555
556   gst_pad_set_activatepull_function (wavpackparse->sinkpad,
557       GST_DEBUG_FUNCPTR (gst_wavepack_parse_sink_activate_pull));
558
559   gst_element_add_pad (GST_ELEMENT (wavpackparse), wavpackparse->sinkpad);
560
561   wavpackparse->srcpad = NULL;
562   gst_wavpack_parse_reset (wavpackparse);
563 }
564
565 static gint64
566 gst_wavpack_parse_get_upstream_length (GstWavpackParse * wavpackparse)
567 {
568   GstPad *peer;
569   gint64 length = -1;
570
571   peer = gst_pad_get_peer (wavpackparse->sinkpad);
572   if (peer) {
573     GstFormat format = GST_FORMAT_BYTES;
574
575     if (!gst_pad_query_duration (peer, &format, &length)) {
576       length = -1;
577     } else {
578       GST_DEBUG ("upstream length: %" G_GINT64_FORMAT, length);
579     }
580     gst_object_unref (peer);
581   } else {
582     GST_DEBUG ("no peer!");
583   }
584
585   return length;
586 }
587
588 static GstBuffer *
589 gst_wavpack_parse_pull_buffer (GstWavpackParse * wvparse, gint64 offset,
590     guint size, GstFlowReturn * flow)
591 {
592   GstFlowReturn flow_ret;
593   GstBuffer *buf = NULL;
594
595   if (offset + size >= wvparse->upstream_length) {
596     wvparse->upstream_length = gst_wavpack_parse_get_upstream_length (wvparse);
597     if (offset + size >= wvparse->upstream_length) {
598       GST_DEBUG_OBJECT (wvparse, "EOS: %" G_GINT64_FORMAT " + %u > %"
599           G_GINT64_FORMAT, offset, size, wvparse->upstream_length);
600       flow_ret = GST_FLOW_UNEXPECTED;
601       goto done;
602     }
603   }
604
605   flow_ret = gst_pad_pull_range (wvparse->sinkpad, offset, size, &buf);
606
607   if (flow_ret != GST_FLOW_OK) {
608     GST_DEBUG_OBJECT (wvparse, "pull_range (%" G_GINT64_FORMAT ", %u) "
609         "failed, flow: %s", offset, size, gst_flow_get_name (flow_ret));
610     return NULL;
611   }
612
613   if (GST_BUFFER_SIZE (buf) < size) {
614     GST_DEBUG_OBJECT (wvparse, "Short read at offset %" G_GINT64_FORMAT
615         ", got only %u of %u bytes", offset, GST_BUFFER_SIZE (buf), size);
616     gst_buffer_unref (buf);
617     buf = NULL;
618     flow_ret = GST_FLOW_UNEXPECTED;
619   }
620
621 done:
622   if (flow)
623     *flow = flow_ret;
624   return buf;
625 }
626
627 static gboolean
628 gst_wavpack_parse_create_src_pad (GstWavpackParse * wvparse, GstBuffer * buf,
629     WavpackHeader * header)
630 {
631   WavpackMetadata meta;
632   GstCaps *caps = NULL;
633   guchar *bufptr;
634
635   g_assert (wvparse->srcpad == NULL);
636
637   bufptr = GST_BUFFER_DATA (buf) + sizeof (WavpackHeader);
638
639   while (read_metadata_buff (&meta, GST_BUFFER_DATA (buf), &bufptr)) {
640     switch (meta.id) {
641       case ID_WVC_BITSTREAM:{
642         caps = gst_caps_new_simple ("audio/x-wavpack-correction",
643             "framed", G_TYPE_BOOLEAN, TRUE, NULL);
644         wvparse->srcpad =
645             gst_pad_new_from_template (gst_element_class_get_pad_template
646             (GST_ELEMENT_GET_CLASS (wvparse), "wvcsrc"), "wvcsrc");
647         break;
648       }
649       case ID_RIFF_HEADER:{
650         WaveHeader wheader;
651
652         /* skip RiffChunkHeader and ChunkHeader */
653         g_memmove (&wheader, meta.data + 20, sizeof (WaveHeader));
654         little_endian_to_native (&wheader, WaveHeaderFormat);
655         wvparse->samplerate = wheader.SampleRate;
656         wvparse->channels = wheader.NumChannels;
657         wvparse->total_samples = header->total_samples;
658         caps = gst_caps_new_simple ("audio/x-wavpack",
659             "width", G_TYPE_INT, wheader.BitsPerSample,
660             "channels", G_TYPE_INT, wvparse->channels,
661             "rate", G_TYPE_INT, wvparse->samplerate,
662             "framed", G_TYPE_BOOLEAN, TRUE, NULL);
663         wvparse->srcpad =
664             gst_pad_new_from_template (gst_element_class_get_pad_template
665             (GST_ELEMENT_GET_CLASS (wvparse), "src"), "src");
666         break;
667       }
668       default:{
669         GST_WARNING_OBJECT (wvparse, "unhandled ID: 0x%02x", meta.id);
670         break;
671       }
672     }
673     if (caps != NULL)
674       break;
675   }
676
677   if (caps == NULL || wvparse->srcpad == NULL)
678     return FALSE;
679
680   GST_DEBUG_OBJECT (wvparse, "Added src pad with caps %" GST_PTR_FORMAT, caps);
681
682   gst_pad_set_query_function (wvparse->srcpad,
683       GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_query));
684   gst_pad_set_event_function (wvparse->srcpad,
685       GST_DEBUG_FUNCPTR (gst_wavpack_parse_src_event));
686
687   gst_pad_set_caps (wvparse->srcpad, caps);
688   gst_pad_use_fixed_caps (wvparse->srcpad);
689
690   gst_object_ref (wvparse->srcpad);
691   gst_element_add_pad (GST_ELEMENT (wvparse), wvparse->srcpad);
692   gst_element_no_more_pads (GST_ELEMENT (wvparse));
693
694   return TRUE;
695 }
696
697 static void
698 gst_wavpack_parse_loop (GstElement * element)
699 {
700   GstWavpackParse *wavpackparse = GST_WAVPACK_PARSE (element);
701   GstFlowReturn flow_ret;
702   WavpackHeader header = { {0,}, 0, };
703   GstBuffer *buf = NULL;
704
705   GST_LOG_OBJECT (wavpackparse, "Current offset: %" G_GINT64_FORMAT,
706       wavpackparse->current_offset);
707
708   buf = gst_wavpack_parse_pull_buffer (wavpackparse,
709       wavpackparse->current_offset, sizeof (WavpackHeader), &flow_ret);
710
711   if (buf == NULL && flow_ret == GST_FLOW_UNEXPECTED) {
712     goto eos;
713   } else if (buf == NULL) {
714     goto pause;
715   }
716
717   gst_wavpack_read_header (&header, GST_BUFFER_DATA (buf));
718   gst_buffer_unref (buf);
719
720   GST_LOG_OBJECT (wavpackparse, "Read header at offset %" G_GINT64_FORMAT
721       ": chunk size = %u+8", wavpackparse->current_offset, header.ckSize);
722
723   buf = gst_wavpack_parse_pull_buffer (wavpackparse,
724       wavpackparse->current_offset, header.ckSize + 8, &flow_ret);
725
726   if (buf == NULL && flow_ret == GST_FLOW_UNEXPECTED) {
727     goto eos;
728   } else if (buf == NULL) {
729     goto pause;
730   }
731
732   if (wavpackparse->srcpad == NULL) {
733     if (!gst_wavpack_parse_create_src_pad (wavpackparse, buf, &header)) {
734       GST_ELEMENT_ERROR (wavpackparse, STREAM, DECODE, (NULL), (NULL));
735       goto pause;
736     }
737   }
738
739   gst_wavpack_parse_index_append_entry (wavpackparse,
740       wavpackparse->current_offset, header.block_index, header.block_samples);
741
742   wavpackparse->current_offset += header.ckSize + 8;
743
744   wavpackparse->segment.last_stop = header.block_index;
745
746   if (wavpackparse->need_newsegment) {
747     if (gst_wavpack_parse_send_newsegment (wavpackparse, FALSE))
748       wavpackparse->need_newsegment = FALSE;
749   }
750
751   GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (header.block_index,
752       GST_SECOND, wavpackparse->samplerate);
753   GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (header.block_samples,
754       GST_SECOND, wavpackparse->samplerate);
755   GST_BUFFER_OFFSET (buf) = header.block_index;
756   gst_buffer_set_caps (buf, GST_PAD_CAPS (wavpackparse->srcpad));
757
758   GST_LOG_OBJECT (wavpackparse, "Pushing buffer with time %" GST_TIME_FORMAT,
759       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
760
761   flow_ret = gst_pad_push (wavpackparse->srcpad, buf);
762   if (flow_ret != GST_FLOW_OK) {
763     GST_DEBUG_OBJECT (wavpackparse, "Push failed, flow: %s",
764         gst_flow_get_name (flow_ret));
765     goto pause;
766   }
767
768   return;
769
770 eos:
771   {
772     GST_DEBUG_OBJECT (wavpackparse, "sending EOS");
773     if (wavpackparse->srcpad) {
774       gst_pad_push_event (wavpackparse->srcpad, gst_event_new_eos ());
775     }
776     /* fall through and pause task */
777   }
778 pause:
779   {
780     GST_DEBUG_OBJECT (wavpackparse, "Pausing task");
781     gst_pad_pause_task (wavpackparse->sinkpad);
782     return;
783   }
784 }
785
786 static GstStateChangeReturn
787 gst_wavpack_parse_change_state (GstElement * element, GstStateChange transition)
788 {
789   GstWavpackParse *wvparse = GST_WAVPACK_PARSE (element);
790   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
791
792   switch (transition) {
793     case GST_STATE_CHANGE_READY_TO_PAUSED:
794       gst_segment_init (&wvparse->segment, GST_FORMAT_DEFAULT);
795       wvparse->segment.last_stop = 0;
796     default:
797       break;
798   }
799
800   if (GST_ELEMENT_CLASS (parent_class)->change_state)
801     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
802
803   switch (transition) {
804     case GST_STATE_CHANGE_PAUSED_TO_READY:
805       gst_wavpack_parse_reset (wvparse);
806       break;
807     default:
808       break;
809   }
810
811   return ret;
812 }
813
814
815 static gboolean
816 gst_wavepack_parse_sink_activate (GstPad * sinkpad)
817 {
818   if (gst_pad_check_pull_range (sinkpad)) {
819     return gst_pad_activate_pull (sinkpad, TRUE);
820   } else {
821     return FALSE;
822   }
823 }
824
825 static gboolean
826 gst_wavepack_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
827 {
828   gboolean result;
829
830   if (active) {
831     result = gst_pad_start_task (sinkpad,
832         (GstTaskFunction) gst_wavpack_parse_loop, GST_PAD_PARENT (sinkpad));
833   } else {
834     result = gst_pad_stop_task (sinkpad);
835   }
836
837   return result;
838 }
839
840 gboolean
841 gst_wavpack_parse_plugin_init (GstPlugin * plugin)
842 {
843   if (!gst_element_register (plugin, "wavpackparse",
844           GST_RANK_PRIMARY, GST_TYPE_WAVPACK_PARSE)) {
845     return FALSE;
846   }
847
848   GST_DEBUG_CATEGORY_INIT (gst_wavpack_parse_debug, "wavpackparse", 0,
849       "wavpack file parser");
850
851   return TRUE;
852 }