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