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