2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5 * Copyright (C) 2011-2012 Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * Based on the speexdec element.
28 * SECTION:element-opusdec
30 * @see_also: opusenc, oggdemux
32 * This element decodes a OPUS stream to raw integer audio.
34 * ## Example pipelines
36 * gst-launch-1.0 -v filesrc location=opus.ogg ! oggdemux ! opusdec ! audioconvert ! audioresample ! alsasink
38 * Decode an Ogg/Opus file. To create an Ogg/Opus file refer to the documentation of opusenc.
49 #include "gstopuselements.h"
50 #include "gstopusheader.h"
51 #include "gstopuscommon.h"
52 #include "gstopusdec.h"
53 #include <gst/pbutils/pbutils.h>
55 GST_DEBUG_CATEGORY_STATIC (opusdec_debug);
56 #define GST_CAT_DEFAULT opusdec_debug
58 static GstStaticPadTemplate opus_dec_src_factory =
59 GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("audio/x-raw, "
63 "format = (string) " GST_AUDIO_NE (S16) ", "
64 "layout = (string) interleaved, "
65 "rate = (int) { 48000, 24000, 16000, 12000, 8000 }, "
66 "channels = (int) [ 1, 8 ] ")
69 static GstStaticPadTemplate opus_dec_sink_factory =
70 GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS ("audio/x-opus, "
74 "channel-mapping-family = (int) 0; "
76 "channel-mapping-family = (int) [1, 255], "
77 "channels = (int) [1, 255], "
78 "stream-count = (int) [1, 255], " "coupled-count = (int) [0, 255]")
81 G_DEFINE_TYPE (GstOpusDec, gst_opus_dec, GST_TYPE_AUDIO_DECODER);
82 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (opusdec, "opusdec",
83 GST_RANK_PRIMARY, GST_TYPE_OPUS_DEC, opus_element_init (plugin));
85 #define DB_TO_LINEAR(x) pow (10., (x) / 20.)
87 #define DEFAULT_USE_INBAND_FEC FALSE
88 #define DEFAULT_APPLY_GAIN TRUE
89 #define DEFAULT_PHASE_INVERSION FALSE
101 static GstFlowReturn gst_opus_dec_parse_header (GstOpusDec * dec,
103 static gboolean gst_opus_dec_start (GstAudioDecoder * dec);
104 static gboolean gst_opus_dec_stop (GstAudioDecoder * dec);
105 static GstFlowReturn gst_opus_dec_handle_frame (GstAudioDecoder * dec,
107 static gboolean gst_opus_dec_set_format (GstAudioDecoder * bdec,
109 static void gst_opus_dec_get_property (GObject * object, guint prop_id,
110 GValue * value, GParamSpec * pspec);
111 static void gst_opus_dec_set_property (GObject * object, guint prop_id,
112 const GValue * value, GParamSpec * pspec);
113 static GstCaps *gst_opus_dec_getcaps (GstAudioDecoder * dec, GstCaps * filter);
117 gst_opus_dec_class_init (GstOpusDecClass * klass)
119 GObjectClass *gobject_class;
120 GstAudioDecoderClass *adclass;
121 GstElementClass *element_class;
123 gobject_class = (GObjectClass *) klass;
124 adclass = (GstAudioDecoderClass *) klass;
125 element_class = (GstElementClass *) klass;
127 gobject_class->set_property = gst_opus_dec_set_property;
128 gobject_class->get_property = gst_opus_dec_get_property;
130 adclass->start = GST_DEBUG_FUNCPTR (gst_opus_dec_start);
131 adclass->stop = GST_DEBUG_FUNCPTR (gst_opus_dec_stop);
132 adclass->handle_frame = GST_DEBUG_FUNCPTR (gst_opus_dec_handle_frame);
133 adclass->set_format = GST_DEBUG_FUNCPTR (gst_opus_dec_set_format);
134 adclass->getcaps = GST_DEBUG_FUNCPTR (gst_opus_dec_getcaps);
136 gst_element_class_add_static_pad_template (element_class,
137 &opus_dec_src_factory);
138 gst_element_class_add_static_pad_template (element_class,
139 &opus_dec_sink_factory);
140 gst_element_class_set_static_metadata (element_class, "Opus audio decoder",
141 "Codec/Decoder/Audio/Converter", "decode opus streams to audio",
142 "Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>");
143 g_object_class_install_property (gobject_class, PROP_USE_INBAND_FEC,
144 g_param_spec_boolean ("use-inband-fec", "Use in-band FEC",
145 "Use forward error correction if available (needs PLC enabled)",
146 DEFAULT_USE_INBAND_FEC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148 g_object_class_install_property (gobject_class, PROP_APPLY_GAIN,
149 g_param_spec_boolean ("apply-gain", "Apply gain",
150 "Apply gain if any is specified in the header", DEFAULT_APPLY_GAIN,
151 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
154 g_object_class_install_property (gobject_class, PROP_PHASE_INVERSION,
155 g_param_spec_boolean ("phase-inversion",
156 "Control Phase Inversion", "Set to true to enable phase inversion, "
157 "this will slightly improve stereo quality, but will have side "
158 "effects when downmixed to mono.", DEFAULT_PHASE_INVERSION,
159 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 * Various decoder statistics. This property returns a GstStructure
167 * with name application/x-opusdec-stats with the following fields:
169 * * #guint64 `num-pushed`: the number of packets pushed out.
170 * * #guint64 `num-gap`: the number of gap packets received.
171 * * #guint64 `plc-num-samples`: the number of samples generated using PLC
172 * * #guint64 `plc-duration`: the total duration, in ns, of samples generated using PLC
173 * * #guint32 `bandwidth`: decoder last bandpass, in kHz, or 0 if unknown
174 * * #guint32 `sample-rate`: decoder sampling rate, or 0 if unknown
175 * * #guint32 `gain`: decoder gain adjustement, in Q8 dB units, or 0 if unknown
176 * * #guint32 `last-packet-duration`: duration, in samples, of the last packet successfully decoded or concealed, or 0 if unknown
177 * * #guint `channels`: the number of channels
181 g_object_class_install_property (gobject_class, PROP_STATS,
182 g_param_spec_boxed ("stats", "Statistics",
183 "Various statistics", GST_TYPE_STRUCTURE,
184 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
186 GST_DEBUG_CATEGORY_INIT (opusdec_debug, "opusdec", 0,
187 "opus decoding element");
191 gst_opus_dec_reset (GstOpusDec * dec)
195 opus_multistream_decoder_destroy (dec->state);
199 gst_buffer_replace (&dec->streamheader, NULL);
200 gst_buffer_replace (&dec->vorbiscomment, NULL);
201 gst_buffer_replace (&dec->last_buffer, NULL);
206 dec->sample_rate = 0;
208 dec->leftover_plc_duration = 0;
209 dec->last_known_buffer_duration = GST_CLOCK_TIME_NONE;
213 gst_opus_dec_init (GstOpusDec * dec)
215 dec->use_inband_fec = FALSE;
216 dec->apply_gain = DEFAULT_APPLY_GAIN;
217 dec->phase_inversion = DEFAULT_PHASE_INVERSION;
219 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
220 gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
222 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
224 gst_opus_dec_reset (dec);
228 gst_opus_dec_start (GstAudioDecoder * dec)
230 GstOpusDec *odec = GST_OPUS_DEC (dec);
232 gst_opus_dec_reset (odec);
234 /* we know about concealment */
235 gst_audio_decoder_set_plc_aware (dec, TRUE);
237 if (odec->use_inband_fec) {
238 /* opusdec outputs samples directly from an input buffer, except if
239 * FEC is on, in which case it buffers one buffer in case one buffer
242 gst_audio_decoder_set_latency (dec, 120 * GST_MSECOND, 120 * GST_MSECOND);
245 GST_OBJECT_LOCK (dec);
246 odec->num_pushed = 0;
248 odec->plc_num_samples = 0;
249 odec->plc_duration = 0;
250 GST_OBJECT_UNLOCK (dec);
256 gst_opus_dec_stop (GstAudioDecoder * dec)
258 GstOpusDec *odec = GST_OPUS_DEC (dec);
260 gst_opus_dec_reset (odec);
266 gst_opus_dec_get_r128_gain (gint16 r128_gain)
268 return r128_gain / (double) (1 << 8);
272 gst_opus_dec_get_r128_volume (gint16 r128_gain)
274 return DB_TO_LINEAR (gst_opus_dec_get_r128_gain (r128_gain));
278 gst_opus_dec_negotiate (GstOpusDec * dec, const GstAudioChannelPosition * pos)
280 GstCaps *caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec));
285 gint rate = dec->sample_rate, channels = dec->n_channels;
286 GstCaps *constraint, *inter;
288 constraint = gst_caps_from_string ("audio/x-raw");
289 if (dec->n_channels <= 2) { /* including 0 */
290 gst_caps_set_simple (constraint, "channels", GST_TYPE_INT_RANGE, 1, 2,
293 gst_caps_set_simple (constraint, "channels", G_TYPE_INT, dec->n_channels,
297 inter = gst_caps_intersect (caps, constraint);
298 gst_caps_unref (constraint);
300 if (gst_caps_is_empty (inter)) {
301 GST_DEBUG_OBJECT (dec, "Empty intersection, failed to negotiate");
302 gst_caps_unref (inter);
303 gst_caps_unref (caps);
307 inter = gst_caps_truncate (inter);
308 s = gst_caps_get_structure (inter, 0);
309 rate = dec->sample_rate > 0 ? dec->sample_rate : 48000;
310 gst_structure_fixate_field_nearest_int (s, "rate", dec->sample_rate);
311 gst_structure_get_int (s, "rate", &rate);
312 channels = dec->n_channels > 0 ? dec->n_channels : 2;
313 gst_structure_fixate_field_nearest_int (s, "channels", channels);
314 gst_structure_get_int (s, "channels", &channels);
316 gst_caps_unref (inter);
318 dec->sample_rate = rate;
319 dec->n_channels = channels;
320 gst_caps_unref (caps);
323 if (dec->n_channels == 0) {
324 GST_DEBUG_OBJECT (dec, "Using a default of 2 channels");
329 if (dec->sample_rate == 0) {
330 GST_DEBUG_OBJECT (dec, "Using a default of 48kHz sample rate");
331 dec->sample_rate = 48000;
334 GST_INFO_OBJECT (dec, "Negotiated %d channels, %d Hz", dec->n_channels,
337 /* pass valid order to audio info */
339 memcpy (dec->opus_pos, pos, sizeof (pos[0]) * dec->n_channels);
340 gst_audio_channel_positions_to_valid_order (dec->opus_pos, dec->n_channels);
343 /* set up source format */
344 gst_audio_info_init (&info);
345 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16,
346 dec->sample_rate, dec->n_channels, pos ? dec->opus_pos : NULL);
347 gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info);
349 /* but we still need the opus order for later reordering */
351 memcpy (dec->opus_pos, pos, sizeof (pos[0]) * dec->n_channels);
353 dec->opus_pos[0] = GST_AUDIO_CHANNEL_POSITION_INVALID;
362 gst_opus_dec_parse_header (GstOpusDec * dec, GstBuffer * buf)
364 GstAudioChannelPosition pos[64];
365 const GstAudioChannelPosition *posn = NULL;
368 if (!gst_opus_header_is_id_header (buf)) {
369 GST_ELEMENT_ERROR (dec, STREAM, FORMAT, (NULL),
370 ("Header is not an Opus ID header"));
371 return GST_FLOW_ERROR;
374 if (!gst_codec_utils_opus_parse_header (buf,
377 &dec->channel_mapping_family,
379 &dec->n_stereo_streams,
380 dec->channel_mapping, &dec->pre_skip, &dec->r128_gain)) {
381 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
382 ("Failed to parse Opus ID header"));
383 return GST_FLOW_ERROR;
385 dec->n_channels = n_channels;
386 dec->r128_gain_volume = gst_opus_dec_get_r128_volume (dec->r128_gain);
388 GST_INFO_OBJECT (dec,
389 "Found pre-skip of %u samples, R128 gain %d (volume %f)",
390 dec->pre_skip, dec->r128_gain, dec->r128_gain_volume);
392 if (dec->channel_mapping_family == 1) {
393 GST_INFO_OBJECT (dec, "Channel mapping family 1, Vorbis mapping");
394 switch (dec->n_channels) {
405 posn = gst_opus_channel_positions[dec->n_channels - 1];
410 GST_ELEMENT_WARNING (GST_ELEMENT (dec), STREAM, DECODE,
411 (NULL), ("Using NONE channel layout for more than 8 channels"));
413 for (i = 0; i < dec->n_channels; i++)
414 pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
420 GST_INFO_OBJECT (dec, "Channel mapping family %d",
421 dec->channel_mapping_family);
424 if (!gst_opus_dec_negotiate (dec, posn))
425 return GST_FLOW_NOT_NEGOTIATED;
432 gst_opus_dec_parse_comments (GstOpusDec * dec, GstBuffer * buf)
437 /* adapted from ext/ogg/gstoggstream.c */
439 packet_duration_opus (const unsigned char *data, size_t bytes)
441 static const guint64 durations[32] = {
442 480, 960, 1920, 2880, /* Silk NB */
443 480, 960, 1920, 2880, /* Silk MB */
444 480, 960, 1920, 2880, /* Silk WB */
445 480, 960, /* Hybrid SWB */
446 480, 960, /* Hybrid FB */
447 120, 240, 480, 960, /* CELT NB */
448 120, 240, 480, 960, /* CELT NB */
449 120, 240, 480, 960, /* CELT NB */
450 120, 240, 480, 960, /* CELT NB */
454 gint64 frame_duration;
462 if (bytes >= 8 && !memcmp (data, "Opus", 4))
467 frame_duration = durations[toc >> 3];
480 GST_WARNING ("Code 3 Opus packet has less than 2 bytes");
483 nframes = data[1] & 63;
487 duration = nframes * frame_duration;
488 if (duration > 5760) {
489 GST_WARNING ("Opus packet duration > 120 ms, invalid");
492 GST_LOG ("Opus packet: frame size %.1f ms, %d frames, duration %.1f ms",
493 frame_duration / 48.f, nframes, duration / 48.f);
494 return duration / 48.f * 1000000;
498 opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buffer)
500 GstFlowReturn res = GST_FLOW_OK;
503 GstBuffer *outbuf, *bufd;
507 unsigned int packet_size;
509 GstMapInfo map, omap;
510 GstAudioClippingMeta *cmeta = NULL;
512 if (dec->state == NULL) {
513 /* If we did not get any headers, default to 2 channels */
514 if (dec->n_channels == 0) {
515 GST_INFO_OBJECT (dec, "No header, assuming single stream");
517 dec->sample_rate = 48000;
518 /* default stereo mapping */
519 dec->channel_mapping_family = 0;
520 dec->channel_mapping[0] = 0;
521 dec->channel_mapping[1] = 1;
523 dec->n_stereo_streams = 1;
525 if (!gst_opus_dec_negotiate (dec, NULL))
526 return GST_FLOW_NOT_NEGOTIATED;
529 if (dec->n_channels == 2 && dec->n_streams == 1
530 && dec->n_stereo_streams == 0) {
531 /* if we are automatically decoding 2 channels, but only have
532 a single encoded one, direct both channels to it */
533 dec->channel_mapping[1] = 0;
536 GST_DEBUG_OBJECT (dec, "Creating decoder with %d channels, %d Hz",
537 dec->n_channels, dec->sample_rate);
538 #ifndef GST_DISABLE_GST_DEBUG
539 gst_opus_common_log_channel_mapping_table (GST_ELEMENT (dec), opusdec_debug,
540 "Mapping table", dec->n_channels, dec->channel_mapping);
543 GST_DEBUG_OBJECT (dec, "%d streams, %d stereo", dec->n_streams,
544 dec->n_stereo_streams);
546 opus_multistream_decoder_create (dec->sample_rate, dec->n_channels,
547 dec->n_streams, dec->n_stereo_streams, dec->channel_mapping, &err);
548 if (!dec->state || err != OPUS_OK)
549 goto creation_failed;
551 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
554 err = opus_multistream_decoder_ctl (dec->state,
555 OPUS_SET_PHASE_INVERSION_DISABLED (!dec->phase_inversion));
557 GST_WARNING_OBJECT (dec, "Could not configure phase inversion: %s",
558 opus_strerror (err));
561 GST_WARNING_OBJECT (dec, "Phase inversion request is not support by this "
562 "version of the Opus Library");
567 GST_DEBUG_OBJECT (dec, "Received buffer of size %" G_GSIZE_FORMAT,
568 gst_buffer_get_size (buffer));
570 GST_DEBUG_OBJECT (dec, "Received missing buffer");
573 /* if using in-band FEC, we introdude one extra frame's delay as we need
574 to potentially wait for next buffer to decode a missing buffer */
575 if (dec->use_inband_fec && !dec->primed) {
576 GST_DEBUG_OBJECT (dec, "First buffer received in FEC mode, early out");
577 gst_buffer_replace (&dec->last_buffer, buffer);
582 /* That's the buffer we'll be sending to the opus decoder. */
583 buf = (dec->use_inband_fec
584 && gst_buffer_get_size (dec->last_buffer) >
585 0) ? dec->last_buffer : buffer;
587 /* That's the buffer we get duration from */
588 bufd = dec->use_inband_fec ? dec->last_buffer : buffer;
590 if (buf && gst_buffer_get_size (buf) > 0) {
591 gst_buffer_map (buf, &map, GST_MAP_READ);
594 GST_DEBUG_OBJECT (dec, "Using buffer of size %" G_GSIZE_FORMAT, size);
596 /* concealment data, pass NULL as the bits parameters */
597 GST_DEBUG_OBJECT (dec, "Using NULL buffer");
602 if (gst_buffer_get_size (bufd) == 0) {
603 GstClockTime const opus_plc_alignment = 2500 * GST_USECOND;
604 GstClockTime aligned_missing_duration;
605 GstClockTime missing_duration = GST_BUFFER_DURATION (bufd);
607 if (!GST_CLOCK_TIME_IS_VALID (missing_duration) || missing_duration == 0) {
608 if (GST_CLOCK_TIME_IS_VALID (dec->last_known_buffer_duration)) {
609 missing_duration = dec->last_known_buffer_duration;
610 GST_WARNING_OBJECT (dec,
611 "Missing duration, using last duration %" GST_TIME_FORMAT,
612 GST_TIME_ARGS (missing_duration));
614 GST_WARNING_OBJECT (dec,
615 "Missing buffer, but unknown duration, and no previously known duration, assuming 20 ms");
616 missing_duration = 20 * GST_MSECOND;
620 GST_DEBUG_OBJECT (dec,
621 "missing buffer, doing PLC duration %" GST_TIME_FORMAT
622 " plus leftover %" GST_TIME_FORMAT, GST_TIME_ARGS (missing_duration),
623 GST_TIME_ARGS (dec->leftover_plc_duration));
625 GST_OBJECT_LOCK (dec);
627 GST_OBJECT_UNLOCK (dec);
629 /* add the leftover PLC duration to that of the buffer */
630 missing_duration += dec->leftover_plc_duration;
632 /* align the combined buffer and leftover PLC duration to multiples
633 * of 2.5ms, rounding to nearest, and store excess duration for later */
634 aligned_missing_duration =
636 opus_plc_alignment / 2) / opus_plc_alignment) * opus_plc_alignment;
637 dec->leftover_plc_duration = missing_duration - aligned_missing_duration;
639 /* Opus' PLC cannot operate with less than 2.5ms; skip PLC
640 * and accumulate the missing duration in the leftover_plc_duration
641 * for the next PLC attempt */
642 if (aligned_missing_duration < opus_plc_alignment) {
643 GST_DEBUG_OBJECT (dec,
644 "current duration %" GST_TIME_FORMAT
645 " of missing data not enough for PLC (minimum needed: %"
646 GST_TIME_FORMAT ") - skipping", GST_TIME_ARGS (missing_duration),
647 GST_TIME_ARGS (opus_plc_alignment));
651 /* convert the duration (in nanoseconds) to sample count */
653 gst_util_uint64_scale_int (aligned_missing_duration, dec->sample_rate,
656 GST_DEBUG_OBJECT (dec,
657 "calculated PLC frame length: %" GST_TIME_FORMAT
658 " num frame samples: %d new leftover: %" GST_TIME_FORMAT,
659 GST_TIME_ARGS (aligned_missing_duration), samples,
660 GST_TIME_ARGS (dec->leftover_plc_duration));
662 GST_OBJECT_LOCK (dec);
663 dec->plc_num_samples += samples;
664 dec->plc_duration += aligned_missing_duration;
665 GST_OBJECT_UNLOCK (dec);
667 /* use maximum size (120 ms) as the number of returned samples is
668 not constant over the stream. */
669 samples = 120 * dec->sample_rate / 1000;
671 packet_size = samples * dec->n_channels * 2;
674 gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (dec),
681 dec->last_known_buffer_duration = packet_duration_opus (data, size);
683 gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
684 out_data = (gint16 *) omap.data;
687 if (dec->use_inband_fec) {
688 if (gst_buffer_get_size (dec->last_buffer) > 0) {
689 /* normal delayed decode */
690 GST_LOG_OBJECT (dec, "FEC enabled, decoding last delayed buffer");
691 n = opus_multistream_decode (dec->state, data, size, out_data, samples,
694 /* FEC reconstruction decode */
695 GST_LOG_OBJECT (dec, "FEC enabled, reconstructing last buffer");
696 n = opus_multistream_decode (dec->state, data, size, out_data, samples,
701 GST_LOG_OBJECT (dec, "FEC disabled, decoding buffer");
702 n = opus_multistream_decode (dec->state, data, size, out_data, samples,
705 if (n == OPUS_BUFFER_TOO_SMALL) {
706 /* if too small, add 2.5 milliseconds and try again, up to the
707 * Opus max size of 120 milliseconds */
708 if (samples >= 120 * dec->sample_rate / 1000)
710 samples += 25 * dec->sample_rate / 10000;
711 packet_size = samples * dec->n_channels * 2;
712 gst_buffer_unmap (outbuf, &omap);
713 gst_buffer_unref (outbuf);
715 gst_audio_decoder_allocate_output_buffer (GST_AUDIO_DECODER (dec),
720 gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
721 out_data = (gint16 *) omap.data;
723 } while (n == OPUS_BUFFER_TOO_SMALL);
724 gst_buffer_unmap (outbuf, &omap);
726 gst_buffer_unmap (buf, &map);
729 GstFlowReturn ret = GST_FLOW_ERROR;
731 gst_buffer_unref (outbuf);
732 GST_AUDIO_DECODER_ERROR (dec, 1, STREAM, DECODE, (NULL),
733 ("Decoding error (%d): %s", n, opus_strerror (n)), ret);
736 GST_DEBUG_OBJECT (dec, "decoded %d samples", n);
737 gst_buffer_set_size (outbuf, n * 2 * dec->n_channels);
738 GST_BUFFER_DURATION (outbuf) = samples * GST_SECOND / dec->sample_rate;
741 cmeta = gst_buffer_get_audio_clipping_meta (buf);
743 g_assert (!cmeta || cmeta->format == GST_FORMAT_DEFAULT);
745 /* Skip any samples that need skipping */
746 if (cmeta && cmeta->start) {
747 guint pre_skip = cmeta->start;
748 guint scaled_pre_skip = pre_skip * dec->sample_rate / 48000;
749 guint skip = scaled_pre_skip > n ? n : scaled_pre_skip;
750 guint scaled_skip = skip * 48000 / dec->sample_rate;
752 gst_buffer_resize (outbuf, skip * 2 * dec->n_channels, -1);
754 GST_INFO_OBJECT (dec,
755 "Skipping %u samples at the beginning (%u at 48000 Hz)",
759 if (cmeta && cmeta->end) {
760 guint post_skip = cmeta->end;
761 guint scaled_post_skip = post_skip * dec->sample_rate / 48000;
762 guint skip = scaled_post_skip > n ? n : scaled_post_skip;
763 guint scaled_skip = skip * 48000 / dec->sample_rate;
764 guint outsize = gst_buffer_get_size (outbuf);
765 guint skip_bytes = skip * 2 * dec->n_channels;
767 if (outsize > skip_bytes)
768 outsize -= skip_bytes;
772 gst_buffer_resize (outbuf, 0, outsize);
774 GST_INFO_OBJECT (dec,
775 "Skipping %u samples at the end (%u at 48000 Hz)", skip, scaled_skip);
778 if (gst_buffer_get_size (outbuf) == 0) {
779 gst_buffer_unref (outbuf);
781 } else if (dec->opus_pos[0] != GST_AUDIO_CHANNEL_POSITION_INVALID) {
782 gst_audio_buffer_reorder_channels (outbuf, GST_AUDIO_FORMAT_S16,
783 dec->n_channels, dec->opus_pos, dec->info.position);
787 /* Would be better off leaving this to a volume element, as this is
788 a naive conversion that does too many int/float conversions.
789 However, we don't have control over the pipeline...
790 So make it optional if the user program wants to use a volume,
791 but do it by default so the correct volume goes out by default */
792 if (dec->apply_gain && outbuf && dec->r128_gain) {
794 unsigned int i, nsamples;
795 double volume = dec->r128_gain_volume;
798 gst_buffer_map (outbuf, &omap, GST_MAP_READWRITE);
799 samples = (gint16 *) omap.data;
801 GST_DEBUG_OBJECT (dec, "Applying gain: volume %f", volume);
802 nsamples = rsize / 2;
803 for (i = 0; i < nsamples; ++i) {
804 int sample = (int) (samples[i] * volume + 0.5);
805 samples[i] = sample < -32768 ? -32768 : sample > 32767 ? 32767 : sample;
807 gst_buffer_unmap (outbuf, &omap);
810 if (dec->use_inband_fec) {
811 gst_buffer_replace (&dec->last_buffer, buffer);
814 GST_OBJECT_LOCK (dec);
816 GST_OBJECT_UNLOCK (dec);
818 res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
820 if (res != GST_FLOW_OK)
821 GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
827 GST_ELEMENT_ERROR (dec, LIBRARY, INIT, ("Failed to create Opus decoder"),
828 ("Failed to create Opus decoder (%d): %s", err, opus_strerror (err)));
829 return GST_FLOW_ERROR;
832 GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL),
833 ("Failed to create %u byte buffer", packet_size));
834 return GST_FLOW_ERROR;
838 gst_opus_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
840 GstOpusDec *dec = GST_OPUS_DEC (bdec);
843 const GValue *streamheader;
846 GST_DEBUG_OBJECT (dec, "set_format: %" GST_PTR_FORMAT, caps);
848 if ((old_caps = gst_pad_get_current_caps (GST_AUDIO_DECODER_SINK_PAD (bdec)))) {
849 if (gst_caps_is_equal (caps, old_caps)) {
850 gst_caps_unref (old_caps);
851 GST_DEBUG_OBJECT (dec, "caps didn't change");
855 GST_DEBUG_OBJECT (dec, "caps have changed, resetting decoder");
856 gst_opus_dec_reset (dec);
857 gst_caps_unref (old_caps);
860 s = gst_caps_get_structure (caps, 0);
861 if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
862 G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
863 gst_value_array_get_size (streamheader) >= 2) {
864 const GValue *header, *vorbiscomment;
866 GstFlowReturn res = GST_FLOW_OK;
868 header = gst_value_array_get_value (streamheader, 0);
869 if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
870 buf = gst_value_get_buffer (header);
871 res = gst_opus_dec_parse_header (dec, buf);
872 if (res != GST_FLOW_OK) {
876 gst_buffer_replace (&dec->streamheader, buf);
879 vorbiscomment = gst_value_array_get_value (streamheader, 1);
880 if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
881 buf = gst_value_get_buffer (vorbiscomment);
882 res = gst_opus_dec_parse_comments (dec, buf);
883 if (res != GST_FLOW_OK) {
887 gst_buffer_replace (&dec->vorbiscomment, buf);
890 const GstAudioChannelPosition *posn = NULL;
893 if (!gst_codec_utils_opus_parse_caps (caps, &dec->sample_rate,
894 &n_channels, &dec->channel_mapping_family,
895 &dec->n_streams, &dec->n_stereo_streams, dec->channel_mapping)) {
899 dec->n_channels = n_channels;
901 if (dec->channel_mapping_family == 1 && dec->n_channels <= 8)
902 posn = gst_opus_channel_positions[dec->n_channels - 1];
904 if (!gst_opus_dec_negotiate (dec, posn))
913 memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2)
919 size1 = gst_buffer_get_size (buf1);
920 size2 = gst_buffer_get_size (buf2);
925 gst_buffer_map (buf1, &map, GST_MAP_READ);
926 res = gst_buffer_memcmp (buf2, 0, map.data, map.size) == 0;
927 gst_buffer_unmap (buf1, &map);
933 gst_opus_dec_handle_frame (GstAudioDecoder * adec, GstBuffer * buf)
938 /* no fancy draining */
939 if (G_UNLIKELY (!buf))
942 dec = GST_OPUS_DEC (adec);
944 "Got buffer ts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
945 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
946 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
948 /* If we have the streamheader and vorbiscomment from the caps already
949 * ignore them here */
950 if (dec->streamheader && dec->vorbiscomment) {
951 if (memcmp_buffers (dec->streamheader, buf)) {
952 GST_DEBUG_OBJECT (dec, "found streamheader");
953 gst_audio_decoder_finish_frame (adec, NULL, 1);
955 } else if (memcmp_buffers (dec->vorbiscomment, buf)) {
956 GST_DEBUG_OBJECT (dec, "found vorbiscomments");
957 gst_audio_decoder_finish_frame (adec, NULL, 1);
960 res = opus_dec_chain_parse_data (dec, buf);
963 /* Otherwise fall back to packet counting and assume that the
964 * first two packets might be the headers, checking magic. */
965 switch (dec->packetno) {
967 if (gst_opus_header_is_header (buf, "OpusHead", 8)) {
968 GST_DEBUG_OBJECT (dec, "found streamheader");
969 res = gst_opus_dec_parse_header (dec, buf);
970 gst_audio_decoder_finish_frame (adec, NULL, 1);
972 res = opus_dec_chain_parse_data (dec, buf);
976 if (gst_opus_header_is_header (buf, "OpusTags", 8)) {
977 GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
978 res = gst_opus_dec_parse_comments (dec, buf);
979 gst_audio_decoder_finish_frame (adec, NULL, 1);
981 res = opus_dec_chain_parse_data (dec, buf);
986 res = opus_dec_chain_parse_data (dec, buf);
997 /* Called with object lock hold */
999 get_bandwidth (GstOpusDec * self)
1007 err = opus_multistream_decoder_ctl (self->state, OPUS_GET_BANDWIDTH (&bw));
1008 if (err != OPUS_OK) {
1009 GST_WARNING_OBJECT (self, "Could not retrieve bandwith: %s",
1010 opus_strerror (err));
1015 case OPUS_BANDWIDTH_NARROWBAND:
1017 case OPUS_BANDWIDTH_MEDIUMBAND:
1019 case OPUS_BANDWIDTH_WIDEBAND:
1021 case OPUS_BANDWIDTH_SUPERWIDEBAND:
1023 case OPUS_BANDWIDTH_FULLBAND:
1026 GST_WARNING_OBJECT (self, "Unknown bandwith enum: %d", bw);
1031 /* Called with object lock hold */
1033 get_sample_rate (GstOpusDec * self)
1042 opus_multistream_decoder_ctl (self->state, OPUS_GET_SAMPLE_RATE (&rate));
1043 if (err != OPUS_OK) {
1044 GST_WARNING_OBJECT (self, "Could not retrieve sample rate: %s",
1045 opus_strerror (err));
1052 /* Called with object lock hold */
1054 get_gain (GstOpusDec * self)
1062 err = opus_multistream_decoder_ctl (self->state, OPUS_GET_GAIN (&gain));
1063 if (err != OPUS_OK) {
1064 GST_WARNING_OBJECT (self, "Could not retrieve gain: %s",
1065 opus_strerror (err));
1072 /* Called with object lock hold */
1074 get_last_packet_duration (GstOpusDec * self)
1083 opus_multistream_decoder_ctl (self->state,
1084 OPUS_GET_LAST_PACKET_DURATION (&duration));
1085 if (err != OPUS_OK) {
1086 GST_WARNING_OBJECT (self, "Could not retrieve last packet duration: %s",
1087 opus_strerror (err));
1094 static GstStructure *
1095 gst_opus_dec_create_stats (GstOpusDec * self)
1099 GST_OBJECT_LOCK (self);
1101 s = gst_structure_new ("application/x-opusdec-stats",
1102 "num-pushed", G_TYPE_UINT64, self->num_pushed,
1103 "num-gap", G_TYPE_UINT64, self->num_gap,
1104 "plc-num-samples", G_TYPE_UINT64, self->plc_num_samples,
1105 "plc-duration", G_TYPE_UINT64, self->plc_duration,
1106 "bandwidth", G_TYPE_UINT, get_bandwidth (self),
1107 "sample-rate", G_TYPE_UINT, get_sample_rate (self),
1108 "gain", G_TYPE_UINT, get_gain (self),
1109 "last-packet-duration", G_TYPE_UINT, get_last_packet_duration (self),
1110 "channels", G_TYPE_UINT, self->n_channels, NULL);
1112 GST_OBJECT_UNLOCK (self);
1118 gst_opus_dec_get_property (GObject * object, guint prop_id, GValue * value,
1121 GstOpusDec *dec = GST_OPUS_DEC (object);
1124 case PROP_USE_INBAND_FEC:
1125 g_value_set_boolean (value, dec->use_inband_fec);
1127 case PROP_APPLY_GAIN:
1128 g_value_set_boolean (value, dec->apply_gain);
1130 case PROP_PHASE_INVERSION:
1131 g_value_set_boolean (value, dec->phase_inversion);
1134 g_value_take_boxed (value, gst_opus_dec_create_stats (dec));
1137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1143 gst_opus_dec_set_property (GObject * object, guint prop_id,
1144 const GValue * value, GParamSpec * pspec)
1146 GstOpusDec *dec = GST_OPUS_DEC (object);
1149 case PROP_USE_INBAND_FEC:
1150 dec->use_inband_fec = g_value_get_boolean (value);
1152 case PROP_APPLY_GAIN:
1153 dec->apply_gain = g_value_get_boolean (value);
1155 case PROP_PHASE_INVERSION:
1156 dec->phase_inversion = g_value_get_boolean (value);
1159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1164 /* caps must be writable */
1166 gst_opus_dec_caps_extend_channels_options (GstCaps * caps)
1171 for (n = 0; n < gst_caps_get_size (caps); ++n) {
1172 GstStructure *s = gst_caps_get_structure (caps, n);
1173 if (gst_structure_get_int (s, "channels", &channels)) {
1174 if (channels == 1 || channels == 2) {
1176 g_value_init (&v, GST_TYPE_INT_RANGE);
1177 gst_value_set_int_range (&v, 1, 2);
1178 gst_structure_set_value (s, "channels", &v);
1186 gst_opus_dec_value_list_append_int (GValue * list, gint i)
1190 g_value_init (&v, G_TYPE_INT);
1191 g_value_set_int (&v, i);
1192 gst_value_list_append_value (list, &v);
1197 gst_opus_dec_caps_extend_rate_options (GstCaps * caps)
1202 g_value_init (&v, GST_TYPE_LIST);
1203 gst_opus_dec_value_list_append_int (&v, 48000);
1204 gst_opus_dec_value_list_append_int (&v, 24000);
1205 gst_opus_dec_value_list_append_int (&v, 16000);
1206 gst_opus_dec_value_list_append_int (&v, 12000);
1207 gst_opus_dec_value_list_append_int (&v, 8000);
1209 for (n = 0; n < gst_caps_get_size (caps); ++n) {
1210 GstStructure *s = gst_caps_get_structure (caps, n);
1212 gst_structure_set_value (s, "rate", &v);
1218 gst_opus_dec_getcaps (GstAudioDecoder * dec, GstCaps * filter)
1220 GstCaps *caps, *proxy_filter = NULL, *ret;
1223 proxy_filter = gst_caps_copy (filter);
1224 gst_opus_dec_caps_extend_channels_options (proxy_filter);
1225 gst_opus_dec_caps_extend_rate_options (proxy_filter);
1227 caps = gst_audio_decoder_proxy_getcaps (dec, NULL, proxy_filter);
1229 gst_caps_unref (proxy_filter);
1231 caps = gst_caps_make_writable (caps);
1232 gst_opus_dec_caps_extend_channels_options (caps);
1233 gst_opus_dec_caps_extend_rate_options (caps);
1237 ret = gst_caps_intersect (caps, filter);
1238 gst_caps_unref (caps);