flacparse: do not leak uid after parsing TOC event
[platform/upstream/gst-plugins-good.git] / gst / audioparsers / gstwavpackparse.c
1 /* GStreamer Wavpack parser
2  * Copyright (C) 2012 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
3  * Copyright (C) 2012 Nokia Corporation. All rights reserved.
4  *   Contact: Stefan Kost <stefan.kost@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 /**
22  * SECTION:element-wavpackparse
23  * @short_description: Wavpack parser
24  * @see_also: #GstAmrParse, #GstAACParse
25  *
26  * This is an Wavpack parser.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch-1.0 filesrc location=abc.wavpack ! wavpackparse ! wavpackdec ! audioresample ! audioconvert ! autoaudiosink
32  * ]|
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <string.h>
41
42 #include "gstwavpackparse.h"
43
44 #include <gst/base/base.h>
45 #include <gst/pbutils/pbutils.h>
46 #include <gst/audio/audio.h>
47
48 GST_DEBUG_CATEGORY_STATIC (wavpack_parse_debug);
49 #define GST_CAT_DEFAULT wavpack_parse_debug
50
51 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("audio/x-wavpack, "
55         "depth = (int) [ 1, 32 ], "
56         "channels = (int) [ 1, 8 ], "
57         "rate = (int) [ 6000, 192000 ], " "framed = (boolean) TRUE; "
58         "audio/x-wavpack-correction, " "framed = (boolean) TRUE")
59     );
60
61 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("audio/x-wavpack"));
65
66 static void gst_wavpack_parse_finalize (GObject * object);
67
68 static gboolean gst_wavpack_parse_start (GstBaseParse * parse);
69 static gboolean gst_wavpack_parse_stop (GstBaseParse * parse);
70 static GstFlowReturn gst_wavpack_parse_handle_frame (GstBaseParse * parse,
71     GstBaseParseFrame * frame, gint * skipsize);
72 static GstCaps *gst_wavpack_parse_get_sink_caps (GstBaseParse * parse,
73     GstCaps * filter);
74 static GstFlowReturn gst_wavpack_parse_pre_push_frame (GstBaseParse * parse,
75     GstBaseParseFrame * frame);
76
77 #define gst_wavpack_parse_parent_class parent_class
78 G_DEFINE_TYPE (GstWavpackParse, gst_wavpack_parse, GST_TYPE_BASE_PARSE);
79
80 static void
81 gst_wavpack_parse_class_init (GstWavpackParseClass * klass)
82 {
83   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
84   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
85   GObjectClass *object_class = G_OBJECT_CLASS (klass);
86
87   GST_DEBUG_CATEGORY_INIT (wavpack_parse_debug, "wavpackparse", 0,
88       "Wavpack audio stream parser");
89
90   object_class->finalize = gst_wavpack_parse_finalize;
91
92   parse_class->start = GST_DEBUG_FUNCPTR (gst_wavpack_parse_start);
93   parse_class->stop = GST_DEBUG_FUNCPTR (gst_wavpack_parse_stop);
94   parse_class->handle_frame =
95       GST_DEBUG_FUNCPTR (gst_wavpack_parse_handle_frame);
96   parse_class->get_sink_caps =
97       GST_DEBUG_FUNCPTR (gst_wavpack_parse_get_sink_caps);
98   parse_class->pre_push_frame =
99       GST_DEBUG_FUNCPTR (gst_wavpack_parse_pre_push_frame);
100
101   gst_element_class_add_pad_template (element_class,
102       gst_static_pad_template_get (&sink_template));
103   gst_element_class_add_pad_template (element_class,
104       gst_static_pad_template_get (&src_template));
105
106   gst_element_class_set_static_metadata (element_class,
107       "Wavpack audio stream parser", "Codec/Parser/Audio",
108       "Wavpack parser", "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
109 }
110
111 static void
112 gst_wavpack_parse_reset (GstWavpackParse * wvparse)
113 {
114   wvparse->channels = -1;
115   wvparse->channel_mask = 0;
116   wvparse->sample_rate = -1;
117   wvparse->width = -1;
118   wvparse->total_samples = 0;
119   wvparse->sent_codec_tag = FALSE;
120 }
121
122 static void
123 gst_wavpack_parse_init (GstWavpackParse * wvparse)
124 {
125   gst_wavpack_parse_reset (wvparse);
126   GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (wvparse));
127 }
128
129 static void
130 gst_wavpack_parse_finalize (GObject * object)
131 {
132   G_OBJECT_CLASS (parent_class)->finalize (object);
133 }
134
135 static gboolean
136 gst_wavpack_parse_start (GstBaseParse * parse)
137 {
138   GstWavpackParse *wvparse = GST_WAVPACK_PARSE (parse);
139
140   GST_DEBUG_OBJECT (parse, "starting");
141
142   gst_wavpack_parse_reset (wvparse);
143
144   /* need header at least */
145   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (wvparse),
146       sizeof (WavpackHeader));
147
148   /* inform baseclass we can come up with ts, based on counters in packets */
149   gst_base_parse_set_has_timing_info (GST_BASE_PARSE_CAST (wvparse), TRUE);
150   gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (wvparse), TRUE);
151
152   return TRUE;
153 }
154
155 static gboolean
156 gst_wavpack_parse_stop (GstBaseParse * parse)
157 {
158   GST_DEBUG_OBJECT (parse, "stopping");
159
160   return TRUE;
161 }
162
163 static gint
164 gst_wavpack_get_default_channel_mask (gint nchannels)
165 {
166   gint channel_mask = 0;
167
168   /* Set the default channel mask for the given number of channels.
169    * It's the same as for WAVE_FORMAT_EXTENDED:
170    * http://www.microsoft.com/whdc/device/audio/multichaud.mspx
171    */
172   switch (nchannels) {
173     case 11:
174       channel_mask |= 0x00400;
175       channel_mask |= 0x00200;
176     case 9:
177       channel_mask |= 0x00100;
178     case 8:
179       channel_mask |= 0x00080;
180       channel_mask |= 0x00040;
181     case 6:
182       channel_mask |= 0x00020;
183       channel_mask |= 0x00010;
184     case 4:
185       channel_mask |= 0x00008;
186     case 3:
187       channel_mask |= 0x00004;
188     case 2:
189       channel_mask |= 0x00002;
190       channel_mask |= 0x00001;
191       break;
192     case 1:
193       /* For mono use front center */
194       channel_mask |= 0x00004;
195       break;
196   }
197
198   return channel_mask;
199 }
200
201 static const struct
202 {
203   const guint32 ms_mask;
204   const GstAudioChannelPosition gst_pos;
205 } layout_mapping[] = {
206   {
207   0x00001, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT}, {
208   0x00002, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
209   0x00004, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}, {
210   0x00008, GST_AUDIO_CHANNEL_POSITION_LFE1}, {
211   0x00010, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT}, {
212   0x00020, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
213   0x00040, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER}, {
214   0x00080, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
215   0x00100, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}, {
216   0x00200, GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT}, {
217   0x00400, GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}, {
218   0x00800, GST_AUDIO_CHANNEL_POSITION_TOP_CENTER}, {
219   0x01000, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT}, {
220   0x02000, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_CENTER}, {
221   0x04000, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT}, {
222   0x08000, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT}, {
223   0x10000, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_CENTER}, {
224   0x20000, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT}
225 };
226
227 #define MAX_CHANNEL_POSITIONS G_N_ELEMENTS (layout_mapping)
228
229 static gboolean
230 gst_wavpack_get_channel_positions (gint num_channels, gint layout,
231     GstAudioChannelPosition * pos)
232 {
233   gint i, p;
234
235   if (num_channels == 1 && layout == 0x00004) {
236     pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
237     return TRUE;
238   }
239
240   p = 0;
241   for (i = 0; i < MAX_CHANNEL_POSITIONS; ++i) {
242     if ((layout & layout_mapping[i].ms_mask) != 0) {
243       if (p >= num_channels) {
244         GST_WARNING ("More bits set in the channel layout map than there "
245             "are channels! Broken file");
246         return FALSE;
247       }
248       if (layout_mapping[i].gst_pos == GST_AUDIO_CHANNEL_POSITION_INVALID) {
249         GST_WARNING ("Unsupported channel position (mask 0x%08x) in channel "
250             "layout map - ignoring those channels", layout_mapping[i].ms_mask);
251         /* what to do? just ignore it and let downstream deal with a channel
252          * layout that has INVALID positions in it for now ... */
253       }
254       pos[p] = layout_mapping[i].gst_pos;
255       ++p;
256     }
257   }
258
259   if (p != num_channels) {
260     GST_WARNING ("Only %d bits set in the channel layout map, but there are "
261         "supposed to be %d channels! Broken file", p, num_channels);
262     return FALSE;
263   }
264
265   return TRUE;
266 }
267
268 static const guint32 sample_rates[] = {
269   6000, 8000, 9600, 11025, 12000, 16000, 22050,
270   24000, 32000, 44100, 48000, 64000, 88200, 96000, 192000
271 };
272
273 #define CHECK(call) { \
274   if (!call) \
275     goto read_failed; \
276 }
277
278 /* caller ensures properly sync'ed with enough data */
279 static gboolean
280 gst_wavpack_parse_frame_metadata (GstWavpackParse * parse, GstBuffer * buf,
281     gint skip, WavpackHeader * wph, WavpackInfo * wpi)
282 {
283   GstByteReader br;
284   gint i;
285   GstMapInfo map;
286
287   g_return_val_if_fail (wph != NULL || wpi != NULL, FALSE);
288   g_return_val_if_fail (gst_buffer_get_size (buf) >=
289       skip + sizeof (WavpackHeader), FALSE);
290
291   gst_buffer_map (buf, &map, GST_MAP_READ);
292
293   gst_byte_reader_init (&br, map.data + skip, wph->ckSize + 8);
294   /* skip past header */
295   gst_byte_reader_skip_unchecked (&br, sizeof (WavpackHeader));
296
297   /* get some basics from header */
298   i = (wph->flags >> 23) & 0xF;
299   if (!wpi->rate)
300     wpi->rate = (i < G_N_ELEMENTS (sample_rates)) ? sample_rates[i] : 44100;
301   wpi->width = ((wph->flags & 0x3) + 1) * 8;
302   if (!wpi->channels)
303     wpi->channels = (wph->flags & 0x4) ? 1 : 2;
304   if (!wpi->channel_mask)
305     wpi->channel_mask = 5 - wpi->channels;
306
307   /* need to dig metadata blocks for some more */
308   while (gst_byte_reader_get_remaining (&br)) {
309     gint size = 0;
310     guint16 size2 = 0;
311     guint8 c, id;
312     const guint8 *data;
313     GstByteReader mbr;
314
315     CHECK (gst_byte_reader_get_uint8 (&br, &id));
316     CHECK (gst_byte_reader_get_uint8 (&br, &c));
317     if (id & ID_LARGE)
318       CHECK (gst_byte_reader_get_uint16_le (&br, &size2));
319     size = size2;
320     size <<= 8;
321     size += c;
322     size <<= 1;
323     if (id & ID_ODD_SIZE)
324       size--;
325
326     CHECK (gst_byte_reader_get_data (&br, size + (size & 1), &data));
327     gst_byte_reader_init (&mbr, data, size);
328
329     switch (id) {
330       case ID_WVC_BITSTREAM:
331         GST_LOG_OBJECT (parse, "correction bitstream");
332         wpi->correction = TRUE;
333         break;
334       case ID_WV_BITSTREAM:
335       case ID_WVX_BITSTREAM:
336         break;
337       case ID_SAMPLE_RATE:
338         if (size == 3) {
339           CHECK (gst_byte_reader_get_uint24_le (&mbr, &wpi->rate));
340           GST_LOG_OBJECT (parse, "updated with custom rate %d", wpi->rate);
341         } else {
342           GST_DEBUG_OBJECT (parse, "unexpected size for SAMPLE_RATE metadata");
343         }
344         break;
345       case ID_CHANNEL_INFO:
346       {
347         guint16 channels;
348         guint32 mask = 0;
349
350         if (size == 6) {
351           CHECK (gst_byte_reader_get_uint16_le (&mbr, &channels));
352           channels = channels & 0xFFF;
353           CHECK (gst_byte_reader_get_uint24_le (&mbr, &mask));
354         } else if (size) {
355           CHECK (gst_byte_reader_get_uint8 (&mbr, &c));
356           channels = c;
357           while (gst_byte_reader_get_uint8 (&mbr, &c))
358             mask |= (((guint32) c) << 8);
359         } else {
360           GST_DEBUG_OBJECT (parse, "unexpected size for CHANNEL_INFO metadata");
361           break;
362         }
363         wpi->channels = channels;
364         wpi->channel_mask = mask;
365         break;
366       }
367       default:
368         GST_LOG_OBJECT (parse, "unparsed ID 0x%x", id);
369         break;
370     }
371   }
372
373   gst_buffer_unmap (buf, &map);
374
375   return TRUE;
376
377   /* ERRORS */
378 read_failed:
379   {
380     gst_buffer_unmap (buf, &map);
381     GST_DEBUG_OBJECT (parse, "short read while parsing metadata");
382     /* let's look the other way anyway */
383     return TRUE;
384   }
385 }
386
387 /* caller ensures properly sync'ed with enough data */
388 static gboolean
389 gst_wavpack_parse_frame_header (GstWavpackParse * parse, GstBuffer * buf,
390     gint skip, WavpackHeader * _wph)
391 {
392   GstByteReader br;
393   WavpackHeader wph = { {0,}, 0, };
394   GstMapInfo map;
395   gboolean hdl = TRUE;
396
397   g_return_val_if_fail (gst_buffer_get_size (buf) >=
398       skip + sizeof (WavpackHeader), FALSE);
399
400   gst_buffer_map (buf, &map, GST_MAP_READ);
401   gst_byte_reader_init (&br, map.data, map.size);
402
403   /* marker */
404   gst_byte_reader_skip_unchecked (&br, skip + 4);
405
406   /* read */
407   hdl &= gst_byte_reader_get_uint32_le (&br, &wph.ckSize);
408   hdl &= gst_byte_reader_get_uint16_le (&br, &wph.version);
409   hdl &= gst_byte_reader_get_uint8 (&br, &wph.track_no);
410   hdl &= gst_byte_reader_get_uint8 (&br, &wph.index_no);
411   hdl &= gst_byte_reader_get_uint32_le (&br, &wph.total_samples);
412   hdl &= gst_byte_reader_get_uint32_le (&br, &wph.block_index);
413   hdl &= gst_byte_reader_get_uint32_le (&br, &wph.block_samples);
414   hdl &= gst_byte_reader_get_uint32_le (&br, &wph.flags);
415   hdl &= gst_byte_reader_get_uint32_le (&br, &wph.crc);
416
417   if (!hdl)
418     GST_WARNING_OBJECT (parse, "Error reading header");
419
420   /* dump */
421   GST_LOG_OBJECT (parse, "size %d", wph.ckSize);
422   GST_LOG_OBJECT (parse, "version 0x%x", wph.version);
423   GST_LOG_OBJECT (parse, "total samples %d", wph.total_samples);
424   GST_LOG_OBJECT (parse, "block index %d", wph.block_index);
425   GST_LOG_OBJECT (parse, "block samples %d", wph.block_samples);
426   GST_LOG_OBJECT (parse, "flags 0x%x", wph.flags);
427   GST_LOG_OBJECT (parse, "crc 0x%x", wph.flags);
428
429   if (!parse->total_samples && wph.block_index == 0 && wph.total_samples != -1) {
430     GST_DEBUG_OBJECT (parse, "determined duration of %u samples",
431         wph.total_samples);
432     parse->total_samples = wph.total_samples;
433   }
434
435   if (_wph)
436     *_wph = wph;
437
438   gst_buffer_unmap (buf, &map);
439
440   return TRUE;
441 }
442
443 static GstFlowReturn
444 gst_wavpack_parse_handle_frame (GstBaseParse * parse,
445     GstBaseParseFrame * frame, gint * skipsize)
446 {
447   GstWavpackParse *wvparse = GST_WAVPACK_PARSE (parse);
448   GstBuffer *buf = frame->buffer;
449   GstByteReader reader;
450   gint off;
451   guint rate, chans, width, mask;
452   gboolean lost_sync, draining, final;
453   guint frmsize = 0;
454   WavpackHeader wph;
455   WavpackInfo wpi = { 0, };
456   GstMapInfo map;
457
458   if (G_UNLIKELY (gst_buffer_get_size (buf) < sizeof (WavpackHeader)))
459     return FALSE;
460
461   gst_buffer_map (buf, &map, GST_MAP_READ);
462   gst_byte_reader_init (&reader, map.data, map.size);
463
464   /* scan for 'wvpk' marker */
465   off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffffffff, 0x7776706b,
466       0, map.size);
467
468   GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
469
470   /* didn't find anything that looks like a sync word, skip */
471   if (off < 0) {
472     *skipsize = map.size - 3;
473     goto skip;
474   }
475
476   /* possible frame header, but not at offset 0? skip bytes before sync */
477   if (off > 0) {
478     *skipsize = off;
479     goto skip;
480   }
481
482   /* make sure the values in the frame header look sane */
483   gst_wavpack_parse_frame_header (wvparse, buf, 0, &wph);
484   frmsize = wph.ckSize + 8;
485
486   /* need the entire frame for parsing */
487   if (gst_byte_reader_get_remaining (&reader) < frmsize)
488     goto more;
489
490   /* got a frame, now we can dig for some more metadata */
491   GST_LOG_OBJECT (parse, "got frame");
492   gst_wavpack_parse_frame_metadata (wvparse, buf, 0, &wph, &wpi);
493
494   lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
495   draining = GST_BASE_PARSE_DRAINING (parse);
496
497   while (!(final = (wph.flags & FLAG_FINAL_BLOCK)) || (lost_sync && !draining)) {
498     guint32 word = 0;
499
500     GST_LOG_OBJECT (wvparse, "checking next frame syncword; "
501         "lost_sync: %d, draining: %d, final: %d", lost_sync, draining, final);
502
503     if (!gst_byte_reader_skip (&reader, wph.ckSize + 8) ||
504         !gst_byte_reader_peek_uint32_be (&reader, &word)) {
505       GST_DEBUG_OBJECT (wvparse, "... but not sufficient data");
506       frmsize += 4;
507       goto more;
508     } else {
509       if (word != 0x7776706b) {
510         GST_DEBUG_OBJECT (wvparse, "0x%x not OK", word);
511         *skipsize = off + 2;
512         goto skip;
513       }
514       /* need to parse each frame/block for metadata if several ones */
515       if (!final) {
516         gint av;
517
518         GST_LOG_OBJECT (wvparse, "checking frame at offset %d (0x%x)",
519             frmsize, frmsize);
520         av = gst_byte_reader_get_remaining (&reader);
521         if (av < sizeof (WavpackHeader)) {
522           frmsize += sizeof (WavpackHeader);
523           goto more;
524         }
525         gst_wavpack_parse_frame_header (wvparse, buf, frmsize, &wph);
526         off = frmsize;
527         frmsize += wph.ckSize + 8;
528         if (av < wph.ckSize + 8)
529           goto more;
530         gst_wavpack_parse_frame_metadata (wvparse, buf, off, &wph, &wpi);
531         /* could also check for matching block_index and block_samples ?? */
532       }
533     }
534
535     /* resynced if we make it here */
536     lost_sync = FALSE;
537   }
538
539   rate = wpi.rate;
540   width = wpi.width;
541   chans = wpi.channels;
542   mask = wpi.channel_mask;
543
544   GST_LOG_OBJECT (parse, "rate: %u, width: %u, chans: %u", rate, width, chans);
545
546   GST_BUFFER_TIMESTAMP (buf) =
547       gst_util_uint64_scale_int (wph.block_index, GST_SECOND, rate);
548   GST_BUFFER_DURATION (buf) =
549       gst_util_uint64_scale_int (wph.block_index + wph.block_samples,
550       GST_SECOND, rate) - GST_BUFFER_TIMESTAMP (buf);
551
552   if (G_UNLIKELY (wvparse->sample_rate != rate || wvparse->channels != chans
553           || wvparse->width != width || wvparse->channel_mask != mask)) {
554     GstCaps *caps;
555
556     if (wpi.correction) {
557       caps = gst_caps_new_simple ("audio/x-wavpack-correction",
558           "framed", G_TYPE_BOOLEAN, TRUE, NULL);
559     } else {
560       caps = gst_caps_new_simple ("audio/x-wavpack",
561           "channels", G_TYPE_INT, chans,
562           "rate", G_TYPE_INT, rate,
563           "depth", G_TYPE_INT, width, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
564
565       if (!mask)
566         mask = gst_wavpack_get_default_channel_mask (wvparse->channels);
567       if (mask != 0) {
568         GstAudioChannelPosition pos[64] =
569             { GST_AUDIO_CHANNEL_POSITION_INVALID, };
570         guint64 gmask;
571
572         if (!gst_wavpack_get_channel_positions (chans, mask, pos)) {
573           GST_WARNING_OBJECT (wvparse, "Failed to determine channel layout");
574         } else {
575           gst_audio_channel_positions_to_mask (pos, chans, FALSE, &gmask);
576           if (gmask)
577             gst_caps_set_simple (caps,
578                 "channel-mask", GST_TYPE_BITMASK, gmask, NULL);
579         }
580       }
581     }
582
583     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
584     gst_caps_unref (caps);
585
586     wvparse->sample_rate = rate;
587     wvparse->channels = chans;
588     wvparse->width = width;
589     wvparse->channel_mask = mask;
590
591     if (wvparse->total_samples) {
592       GST_DEBUG_OBJECT (wvparse, "setting duration");
593       gst_base_parse_set_duration (GST_BASE_PARSE (wvparse),
594           GST_FORMAT_TIME, gst_util_uint64_scale_int (wvparse->total_samples,
595               GST_SECOND, wvparse->sample_rate), 0);
596     }
597   }
598
599   /* return to normal size */
600   gst_base_parse_set_min_frame_size (parse, sizeof (WavpackHeader));
601   gst_buffer_unmap (buf, &map);
602
603   return gst_base_parse_finish_frame (parse, frame, frmsize);
604
605 skip:
606   gst_buffer_unmap (buf, &map);
607   GST_LOG_OBJECT (wvparse, "skipping %d", *skipsize);
608   return GST_FLOW_OK;
609
610 more:
611   gst_buffer_unmap (buf, &map);
612   GST_LOG_OBJECT (wvparse, "need at least %u", frmsize);
613   gst_base_parse_set_min_frame_size (parse, frmsize);
614   *skipsize = 0;
615   return GST_FLOW_OK;
616 }
617
618 static void
619 remove_fields (GstCaps * caps)
620 {
621   guint i, n;
622
623   n = gst_caps_get_size (caps);
624   for (i = 0; i < n; i++) {
625     GstStructure *s = gst_caps_get_structure (caps, i);
626
627     gst_structure_remove_field (s, "framed");
628   }
629 }
630
631 static GstCaps *
632 gst_wavpack_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
633 {
634   GstCaps *peercaps, *templ;
635   GstCaps *res;
636
637   templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
638   if (filter) {
639     GstCaps *fcopy = gst_caps_copy (filter);
640     /* Remove the fields we convert */
641     remove_fields (fcopy);
642     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
643     gst_caps_unref (fcopy);
644   } else
645     peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
646
647   if (peercaps) {
648     /* Remove the framed field */
649     peercaps = gst_caps_make_writable (peercaps);
650     remove_fields (peercaps);
651
652     res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
653     gst_caps_unref (peercaps);
654     gst_caps_unref (templ);
655   } else {
656     res = templ;
657   }
658
659   if (filter) {
660     GstCaps *intersection;
661
662     intersection =
663         gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
664     gst_caps_unref (res);
665     res = intersection;
666   }
667
668   return res;
669 }
670
671 static GstFlowReturn
672 gst_wavpack_parse_pre_push_frame (GstBaseParse * parse,
673     GstBaseParseFrame * frame)
674 {
675   GstWavpackParse *wavpackparse = GST_WAVPACK_PARSE (parse);
676
677   if (!wavpackparse->sent_codec_tag) {
678     GstTagList *taglist;
679     GstCaps *caps;
680
681     taglist = gst_tag_list_new_empty ();
682
683     /* codec tag */
684     caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
685     gst_pb_utils_add_codec_description_to_tag_list (taglist,
686         GST_TAG_AUDIO_CODEC, caps);
687     gst_caps_unref (caps);
688
689     gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (wavpackparse),
690         gst_event_new_tag (taglist));
691
692     /* also signals the end of first-frame processing */
693     wavpackparse->sent_codec_tag = TRUE;
694   }
695
696   return GST_FLOW_OK;
697 }