Debian Codegraph @

gst/audioparsers/gstwavpackparse.c

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