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