2011-06-05 07:54:19 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
|
|
|
|
* Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
|
|
|
|
* Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Based on the speexdec element.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-opusdec
|
|
|
|
* @see_also: opusenc, oggdemux
|
|
|
|
*
|
|
|
|
* This element decodes a OPUS stream to raw integer audio.
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example pipelines</title>
|
|
|
|
* |[
|
|
|
|
* gst-launch -v filesrc location=opus.ogg ! oggdemux ! opusdec ! audioconvert ! audioresample ! alsasink
|
|
|
|
* ]| Decode an Ogg/Opus file. To create an Ogg/Opus file refer to the documentation of opusenc.
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-12-07 05:06:11 +00:00
|
|
|
#include "config.h"
|
2011-06-05 07:54:19 +00:00
|
|
|
#endif
|
|
|
|
|
2011-11-23 13:22:12 +00:00
|
|
|
#include <math.h>
|
2011-06-05 07:54:19 +00:00
|
|
|
#include <string.h>
|
2011-11-21 17:48:54 +00:00
|
|
|
#include "gstopusheader.h"
|
2011-11-24 13:29:56 +00:00
|
|
|
#include "gstopuscommon.h"
|
2011-11-21 17:48:54 +00:00
|
|
|
#include "gstopusdec.h"
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (opusdec_debug);
|
|
|
|
#define GST_CAT_DEFAULT opusdec_debug
|
|
|
|
|
|
|
|
static GstStaticPadTemplate opus_dec_src_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-11-11 17:46:41 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw, "
|
2011-11-25 11:48:58 +00:00
|
|
|
"format = (string) { " GST_AUDIO_NE (S16) " }, "
|
2012-03-07 16:14:29 +00:00
|
|
|
"layout = (string) interleaved, "
|
2011-11-23 17:32:03 +00:00
|
|
|
"rate = (int) { 48000, 24000, 16000, 12000, 8000 }, "
|
2011-11-25 11:48:58 +00:00
|
|
|
"channels = (int) [ 1, 8 ] ")
|
2011-06-05 07:54:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate opus_dec_sink_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("audio/x-opus")
|
|
|
|
);
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
G_DEFINE_TYPE (GstOpusDec, gst_opus_dec, GST_TYPE_AUDIO_DECODER);
|
|
|
|
|
2011-11-23 13:22:12 +00:00
|
|
|
#define DB_TO_LINEAR(x) pow (10., (x) / 20.)
|
|
|
|
|
2011-11-23 11:58:54 +00:00
|
|
|
#define DEFAULT_USE_INBAND_FEC FALSE
|
2011-11-23 13:22:12 +00:00
|
|
|
#define DEFAULT_APPLY_GAIN TRUE
|
2011-11-23 11:58:54 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2011-11-23 13:22:12 +00:00
|
|
|
PROP_USE_INBAND_FEC,
|
|
|
|
PROP_APPLY_GAIN
|
2011-11-23 11:58:54 +00:00
|
|
|
};
|
|
|
|
|
2011-11-16 16:56:43 +00:00
|
|
|
|
|
|
|
static GstFlowReturn gst_opus_dec_parse_header (GstOpusDec * dec,
|
2011-06-05 07:54:19 +00:00
|
|
|
GstBuffer * buf);
|
2011-11-14 13:41:58 +00:00
|
|
|
static gboolean gst_opus_dec_start (GstAudioDecoder * dec);
|
|
|
|
static gboolean gst_opus_dec_stop (GstAudioDecoder * dec);
|
|
|
|
static GstFlowReturn gst_opus_dec_handle_frame (GstAudioDecoder * dec,
|
|
|
|
GstBuffer * buffer);
|
|
|
|
static gboolean gst_opus_dec_set_format (GstAudioDecoder * bdec,
|
|
|
|
GstCaps * caps);
|
2011-11-23 11:58:54 +00:00
|
|
|
static void gst_opus_dec_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_opus_dec_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
static void
|
2011-11-11 17:46:41 +00:00
|
|
|
gst_opus_dec_class_init (GstOpusDecClass * klass)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
2011-11-23 11:58:54 +00:00
|
|
|
GObjectClass *gobject_class;
|
2011-11-14 13:41:58 +00:00
|
|
|
GstAudioDecoderClass *adclass;
|
2011-11-11 17:46:41 +00:00
|
|
|
GstElementClass *element_class;
|
|
|
|
|
2011-11-23 11:58:54 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-11-14 13:41:58 +00:00
|
|
|
adclass = (GstAudioDecoderClass *) klass;
|
2011-11-11 17:46:41 +00:00
|
|
|
element_class = (GstElementClass *) klass;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-23 11:58:54 +00:00
|
|
|
gobject_class->set_property = gst_opus_dec_set_property;
|
|
|
|
gobject_class->get_property = gst_opus_dec_get_property;
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
adclass->start = GST_DEBUG_FUNCPTR (gst_opus_dec_start);
|
|
|
|
adclass->stop = GST_DEBUG_FUNCPTR (gst_opus_dec_stop);
|
|
|
|
adclass->handle_frame = GST_DEBUG_FUNCPTR (gst_opus_dec_handle_frame);
|
|
|
|
adclass->set_format = GST_DEBUG_FUNCPTR (gst_opus_dec_set_format);
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&opus_dec_src_factory));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&opus_dec_sink_factory));
|
|
|
|
gst_element_class_set_details_simple (element_class, "Opus audio decoder",
|
|
|
|
"Codec/Decoder/Audio",
|
|
|
|
"decode opus streams to audio",
|
|
|
|
"Sebastian Dröge <sebastian.droege@collabora.co.uk>");
|
2011-11-23 11:58:54 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_USE_INBAND_FEC,
|
|
|
|
g_param_spec_boolean ("use-inband-fec", "Use in-band FEC",
|
|
|
|
"Use forward error correction if available", DEFAULT_USE_INBAND_FEC,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2011-11-23 13:22:12 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_APPLY_GAIN,
|
|
|
|
g_param_spec_boolean ("apply-gain", "Apply gain",
|
|
|
|
"Apply gain if any is specified in the header", DEFAULT_APPLY_GAIN,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (opusdec_debug, "opusdec", 0,
|
|
|
|
"opus decoding element");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opus_dec_reset (GstOpusDec * dec)
|
|
|
|
{
|
|
|
|
dec->packetno = 0;
|
|
|
|
if (dec->state) {
|
2011-11-23 17:49:58 +00:00
|
|
|
opus_multistream_decoder_destroy (dec->state);
|
2011-06-05 07:54:19 +00:00
|
|
|
dec->state = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_replace (&dec->streamheader, NULL);
|
|
|
|
gst_buffer_replace (&dec->vorbiscomment, NULL);
|
2011-11-23 11:58:54 +00:00
|
|
|
gst_buffer_replace (&dec->last_buffer, NULL);
|
|
|
|
dec->primed = FALSE;
|
2011-11-21 12:50:22 +00:00
|
|
|
|
|
|
|
dec->pre_skip = 0;
|
2011-11-23 13:22:12 +00:00
|
|
|
dec->r128_gain = 0;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-11 17:46:41 +00:00
|
|
|
gst_opus_dec_init (GstOpusDec * dec)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
2011-11-16 13:26:35 +00:00
|
|
|
dec->sample_rate = 0;
|
|
|
|
dec->n_channels = 0;
|
2011-11-23 11:58:54 +00:00
|
|
|
dec->use_inband_fec = FALSE;
|
2011-11-23 13:22:12 +00:00
|
|
|
dec->apply_gain = DEFAULT_APPLY_GAIN;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
gst_opus_dec_reset (dec);
|
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
static gboolean
|
|
|
|
gst_opus_dec_start (GstAudioDecoder * dec)
|
|
|
|
{
|
|
|
|
GstOpusDec *odec = GST_OPUS_DEC (dec);
|
|
|
|
|
|
|
|
gst_opus_dec_reset (odec);
|
|
|
|
|
|
|
|
/* we know about concealment */
|
|
|
|
gst_audio_decoder_set_plc_aware (dec, TRUE);
|
|
|
|
|
2011-11-23 11:58:54 +00:00
|
|
|
if (odec->use_inband_fec) {
|
|
|
|
gst_audio_decoder_set_latency (dec, 2 * GST_MSECOND + GST_MSECOND / 2,
|
|
|
|
120 * GST_MSECOND);
|
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_opus_dec_stop (GstAudioDecoder * dec)
|
|
|
|
{
|
|
|
|
GstOpusDec *odec = GST_OPUS_DEC (dec);
|
|
|
|
|
|
|
|
gst_opus_dec_reset (odec);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-23 13:22:12 +00:00
|
|
|
static double
|
|
|
|
gst_opus_dec_get_r128_gain (gint16 r128_gain)
|
|
|
|
{
|
|
|
|
return r128_gain / (double) (1 << 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
gst_opus_dec_get_r128_volume (gint16 r128_gain)
|
|
|
|
{
|
|
|
|
return DB_TO_LINEAR (gst_opus_dec_get_r128_gain (r128_gain));
|
|
|
|
}
|
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
static void
|
|
|
|
gst_opus_dec_negotiate (GstOpusDec * dec, const GstAudioChannelPosition * pos)
|
2011-11-28 19:47:34 +00:00
|
|
|
{
|
|
|
|
GstCaps *caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec));
|
2011-12-07 05:06:11 +00:00
|
|
|
GstStructure *s;
|
2012-03-07 16:14:29 +00:00
|
|
|
GstAudioInfo info;
|
2011-12-07 05:06:11 +00:00
|
|
|
|
2012-03-12 16:06:11 +00:00
|
|
|
caps = gst_caps_truncate (caps);
|
2011-12-07 05:06:11 +00:00
|
|
|
|
2012-03-12 16:06:11 +00:00
|
|
|
caps = gst_caps_make_writable (caps);
|
2011-12-07 05:06:11 +00:00
|
|
|
s = gst_caps_get_structure (caps, 0);
|
2011-11-28 19:47:34 +00:00
|
|
|
gst_structure_fixate_field_nearest_int (s, "rate", 48000);
|
|
|
|
gst_structure_get_int (s, "rate", &dec->sample_rate);
|
|
|
|
gst_structure_fixate_field_nearest_int (s, "channels", dec->n_channels);
|
|
|
|
gst_structure_get_int (s, "channels", &dec->n_channels);
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (dec, "Negotiated %d channels, %d Hz", dec->n_channels,
|
|
|
|
dec->sample_rate);
|
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
/* pass valid order to audio info */
|
|
|
|
if (pos) {
|
|
|
|
memcpy (dec->opus_pos, pos, sizeof (pos[0]) * dec->n_channels);
|
|
|
|
gst_audio_channel_positions_to_valid_order (dec->opus_pos, dec->n_channels);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set up source format */
|
|
|
|
gst_audio_info_init (&info);
|
|
|
|
gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16,
|
|
|
|
dec->sample_rate, dec->n_channels, pos ? dec->opus_pos : NULL);
|
|
|
|
gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info);
|
|
|
|
|
|
|
|
/* but we still need the opus order for later reordering */
|
|
|
|
if (pos) {
|
|
|
|
memcpy (dec->opus_pos, pos, sizeof (pos[0]) * dec->n_channels);
|
|
|
|
gst_audio_channel_positions_to_valid_order (dec->opus_pos, dec->n_channels);
|
|
|
|
} else {
|
|
|
|
dec->opus_pos[0] = GST_AUDIO_CHANNEL_POSITION_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
dec->info = info;
|
2011-11-28 19:47:34 +00:00
|
|
|
}
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
static GstFlowReturn
|
2011-11-14 13:41:58 +00:00
|
|
|
gst_opus_dec_parse_header (GstOpusDec * dec, GstBuffer * buf)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
2011-11-25 11:48:58 +00:00
|
|
|
const guint8 *data;
|
2012-03-07 16:14:29 +00:00
|
|
|
GstAudioChannelPosition pos[64];
|
|
|
|
const GstAudioChannelPosition *posn = NULL;
|
|
|
|
GstMapInfo map;
|
2011-11-21 12:50:22 +00:00
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
g_return_val_if_fail (gst_opus_header_is_id_header (buf), GST_FLOW_ERROR);
|
2011-11-25 11:48:58 +00:00
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
|
|
|
data = map.data;
|
2011-11-25 11:48:58 +00:00
|
|
|
|
2011-11-25 12:39:20 +00:00
|
|
|
g_return_val_if_fail (dec->n_channels == 0
|
|
|
|
|| dec->n_channels == data[9], GST_FLOW_ERROR);
|
2011-11-21 12:50:22 +00:00
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
dec->n_channels = data[9];
|
|
|
|
dec->pre_skip = GST_READ_UINT16_LE (data + 10);
|
|
|
|
dec->r128_gain = GST_READ_UINT16_LE (data + 14);
|
2011-11-23 13:22:12 +00:00
|
|
|
dec->r128_gain_volume = gst_opus_dec_get_r128_volume (dec->r128_gain);
|
|
|
|
GST_INFO_OBJECT (dec,
|
|
|
|
"Found pre-skip of %u samples, R128 gain %d (volume %f)",
|
|
|
|
dec->pre_skip, dec->r128_gain, dec->r128_gain_volume);
|
2011-11-21 12:50:22 +00:00
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
dec->channel_mapping_family = data[18];
|
|
|
|
if (dec->channel_mapping_family == 0) {
|
|
|
|
/* implicit mapping */
|
|
|
|
GST_INFO_OBJECT (dec, "Channel mapping family 0, implicit mapping");
|
|
|
|
dec->n_streams = dec->n_stereo_streams = 1;
|
|
|
|
dec->channel_mapping[0] = 0;
|
|
|
|
dec->channel_mapping[1] = 1;
|
|
|
|
} else {
|
|
|
|
dec->n_streams = data[19];
|
|
|
|
dec->n_stereo_streams = data[20];
|
|
|
|
memcpy (dec->channel_mapping, data + 21, dec->n_channels);
|
|
|
|
|
|
|
|
if (dec->channel_mapping_family == 1) {
|
|
|
|
GST_INFO_OBJECT (dec, "Channel mapping family 1, Vorbis mapping");
|
|
|
|
switch (dec->n_channels) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
/* nothing */
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
2012-03-07 16:14:29 +00:00
|
|
|
posn = gst_opus_channel_positions[dec->n_channels - 1];
|
2011-11-24 13:29:56 +00:00
|
|
|
break;
|
|
|
|
default:{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
GST_ELEMENT_WARNING (GST_ELEMENT (dec), STREAM, DECODE,
|
|
|
|
(NULL), ("Using NONE channel layout for more than 8 channels"));
|
|
|
|
|
|
|
|
for (i = 0; i < dec->n_channels; i++)
|
2012-03-07 16:14:29 +00:00
|
|
|
pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
|
2011-11-24 13:29:56 +00:00
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
posn = pos;
|
2011-11-24 13:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GST_INFO_OBJECT (dec, "Channel mapping family %d",
|
|
|
|
dec->channel_mapping_family);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_opus_dec_negotiate (dec, posn);
|
2011-11-23 17:49:58 +00:00
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
2011-11-21 12:50:22 +00:00
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-21 12:50:22 +00:00
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
static GstFlowReturn
|
2011-11-14 13:41:58 +00:00
|
|
|
gst_opus_dec_parse_comments (GstOpusDec * dec, GstBuffer * buf)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-11-23 11:58:54 +00:00
|
|
|
opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buffer)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
2012-03-07 16:14:29 +00:00
|
|
|
gsize size;
|
2011-06-05 07:54:19 +00:00
|
|
|
guint8 *data;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
gint16 *out_data;
|
2011-09-28 12:24:21 +00:00
|
|
|
int n, err;
|
2011-11-16 01:14:32 +00:00
|
|
|
int samples;
|
2011-11-15 23:00:32 +00:00
|
|
|
unsigned int packet_size;
|
2011-11-23 11:58:54 +00:00
|
|
|
GstBuffer *buf;
|
2012-03-07 16:14:29 +00:00
|
|
|
GstMapInfo map, omap;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
if (dec->state == NULL) {
|
2011-11-28 19:47:34 +00:00
|
|
|
/* If we did not get any headers, default to 2 channels */
|
|
|
|
if (dec->n_channels == 0) {
|
|
|
|
GST_INFO_OBJECT (dec, "No header, assuming single stream");
|
|
|
|
dec->n_channels = 2;
|
|
|
|
dec->sample_rate = 48000;
|
|
|
|
/* default stereo mapping */
|
|
|
|
dec->channel_mapping_family = 0;
|
|
|
|
dec->channel_mapping[0] = 0;
|
|
|
|
dec->channel_mapping[1] = 1;
|
|
|
|
dec->n_streams = 1;
|
|
|
|
dec->n_stereo_streams = 1;
|
2012-03-07 16:14:29 +00:00
|
|
|
|
|
|
|
gst_opus_dec_negotiate (dec, NULL);
|
2011-11-28 19:47:34 +00:00
|
|
|
}
|
|
|
|
|
2011-11-16 17:24:20 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "Creating decoder with %d channels, %d Hz",
|
|
|
|
dec->n_channels, dec->sample_rate);
|
2011-12-08 18:45:27 +00:00
|
|
|
#ifndef GST_DISABLE_DEBUG
|
|
|
|
gst_opus_common_log_channel_mapping_table (GST_ELEMENT (dec), opusdec_debug,
|
|
|
|
"Mapping table", dec->n_channels, dec->channel_mapping);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (dec, "%d streams, %d stereo", dec->n_streams,
|
|
|
|
dec->n_stereo_streams);
|
|
|
|
dec->state =
|
|
|
|
opus_multistream_decoder_create (dec->sample_rate, dec->n_channels,
|
|
|
|
dec->n_streams, dec->n_stereo_streams, dec->channel_mapping, &err);
|
2011-09-28 12:24:21 +00:00
|
|
|
if (!dec->state || err != OPUS_OK)
|
|
|
|
goto creation_failed;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 11:58:54 +00:00
|
|
|
if (buffer) {
|
|
|
|
GST_DEBUG_OBJECT (dec, "Received buffer of size %u",
|
2011-11-25 11:48:58 +00:00
|
|
|
gst_buffer_get_size (buffer));
|
2011-11-23 11:58:54 +00:00
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (dec, "Received missing buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if using in-band FEC, we introdude one extra frame's delay as we need
|
|
|
|
to potentially wait for next buffer to decode a missing buffer */
|
|
|
|
if (dec->use_inband_fec && !dec->primed) {
|
|
|
|
GST_DEBUG_OBJECT (dec, "First buffer received in FEC mode, early out");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* That's the buffer we'll be sending to the opus decoder. */
|
|
|
|
buf = dec->use_inband_fec && dec->last_buffer ? dec->last_buffer : buffer;
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
if (buf) {
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
|
|
|
data = map.data;
|
|
|
|
size = map.size;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-23 11:58:54 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "Using buffer of size %u", size);
|
2011-06-05 07:54:19 +00:00
|
|
|
} else {
|
|
|
|
/* concealment data, pass NULL as the bits parameters */
|
2011-11-23 11:58:54 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "Using NULL buffer");
|
2011-06-05 07:54:19 +00:00
|
|
|
data = NULL;
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
2011-11-23 11:58:54 +00:00
|
|
|
/* use maximum size (120 ms) as the number of returned samples is
|
|
|
|
not constant over the stream. */
|
|
|
|
samples = 120 * dec->sample_rate / 1000;
|
2011-11-16 01:14:32 +00:00
|
|
|
packet_size = samples * dec->n_channels * 2;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-15 23:00:32 +00:00
|
|
|
outbuf = gst_buffer_new_and_alloc (packet_size);
|
|
|
|
if (!outbuf) {
|
|
|
|
goto buffer_failed;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
|
|
|
|
out_data = (gint16 *) omap.data;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-23 11:58:54 +00:00
|
|
|
if (dec->use_inband_fec) {
|
|
|
|
if (dec->last_buffer) {
|
|
|
|
/* normal delayed decode */
|
2011-11-23 17:49:58 +00:00
|
|
|
n = opus_multistream_decode (dec->state, data, size, out_data, samples,
|
|
|
|
0);
|
2011-11-23 11:58:54 +00:00
|
|
|
} else {
|
|
|
|
/* FEC reconstruction decode */
|
2011-11-23 17:49:58 +00:00
|
|
|
n = opus_multistream_decode (dec->state, data, size, out_data, samples,
|
|
|
|
1);
|
2011-11-23 11:58:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* normal decode */
|
2011-11-23 17:49:58 +00:00
|
|
|
n = opus_multistream_decode (dec->state, data, size, out_data, samples, 0);
|
2011-11-23 11:58:54 +00:00
|
|
|
}
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_unmap (outbuf, &omap);
|
|
|
|
if (buf)
|
|
|
|
gst_buffer_unmap (buf, &map);
|
2011-11-23 11:58:54 +00:00
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
if (n < 0) {
|
|
|
|
GST_ELEMENT_ERROR (dec, STREAM, DECODE, ("Decoding error: %d", n), (NULL));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2011-11-16 01:14:32 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "decoded %d samples", n);
|
2011-11-25 11:48:58 +00:00
|
|
|
gst_buffer_set_size (outbuf, n * 2 * dec->n_channels);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-21 17:01:49 +00:00
|
|
|
/* Skip any samples that need skipping */
|
|
|
|
if (dec->pre_skip > 0) {
|
|
|
|
guint scaled_pre_skip = dec->pre_skip * dec->sample_rate / 48000;
|
|
|
|
guint skip = scaled_pre_skip > n ? n : scaled_pre_skip;
|
|
|
|
guint scaled_skip = skip * 48000 / dec->sample_rate;
|
2011-11-25 11:48:58 +00:00
|
|
|
|
|
|
|
gst_buffer_resize (outbuf, skip * 2 * dec->n_channels, -1);
|
2011-11-21 17:01:49 +00:00
|
|
|
dec->pre_skip -= scaled_skip;
|
|
|
|
GST_INFO_OBJECT (dec,
|
|
|
|
"Skipping %u samples (%u at 48000 Hz, %u left to skip)", skip,
|
|
|
|
scaled_skip, dec->pre_skip);
|
2011-11-28 19:38:34 +00:00
|
|
|
}
|
2011-11-21 17:01:49 +00:00
|
|
|
|
2011-12-30 10:41:17 +00:00
|
|
|
if (gst_buffer_get_size (outbuf) == 0) {
|
2011-11-28 19:38:34 +00:00
|
|
|
gst_buffer_unref (outbuf);
|
|
|
|
outbuf = NULL;
|
2012-03-07 16:14:29 +00:00
|
|
|
} else if (dec->opus_pos[0] != GST_AUDIO_CHANNEL_POSITION_INVALID) {
|
|
|
|
gst_audio_buffer_reorder_channels (outbuf, GST_AUDIO_FORMAT_S16,
|
|
|
|
dec->n_channels, dec->opus_pos, dec->info.position);
|
2011-11-21 17:01:49 +00:00
|
|
|
}
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-23 13:22:12 +00:00
|
|
|
/* Apply gain */
|
|
|
|
/* Would be better off leaving this to a volume element, as this is
|
|
|
|
a naive conversion that does too many int/float conversions.
|
|
|
|
However, we don't have control over the pipeline...
|
|
|
|
So make it optional if the user program wants to use a volume,
|
|
|
|
but do it by default so the correct volume goes out by default */
|
|
|
|
if (dec->apply_gain && outbuf && dec->r128_gain) {
|
2011-11-25 11:48:58 +00:00
|
|
|
gsize rsize;
|
|
|
|
unsigned int i, nsamples;
|
2011-11-23 13:22:12 +00:00
|
|
|
double volume = dec->r128_gain_volume;
|
2012-03-07 16:14:29 +00:00
|
|
|
gint16 *samples;
|
2011-11-25 11:48:58 +00:00
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_map (outbuf, &omap, GST_MAP_READWRITE);
|
|
|
|
samples = (gint16 *) omap.data;
|
|
|
|
rsize = omap.size;
|
2011-11-23 13:22:12 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "Applying gain: volume %f", volume);
|
2011-11-25 11:48:58 +00:00
|
|
|
nsamples = rsize / 2;
|
2011-11-23 13:22:12 +00:00
|
|
|
for (i = 0; i < nsamples; ++i) {
|
|
|
|
int sample = (int) (samples[i] * volume + 0.5);
|
|
|
|
samples[i] = sample < -32768 ? -32768 : sample > 32767 ? 32767 : sample;
|
|
|
|
}
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_unmap (outbuf, &omap);
|
2011-11-23 13:22:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
if (res != GST_FLOW_OK)
|
|
|
|
GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
|
|
|
|
|
2011-11-23 11:58:54 +00:00
|
|
|
done:
|
|
|
|
if (dec->use_inband_fec) {
|
|
|
|
gst_buffer_replace (&dec->last_buffer, buffer);
|
|
|
|
dec->primed = TRUE;
|
|
|
|
}
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
return res;
|
2011-09-28 12:24:21 +00:00
|
|
|
|
|
|
|
creation_failed:
|
|
|
|
GST_ERROR_OBJECT (dec, "Failed to create Opus decoder: %d", err);
|
|
|
|
return GST_FLOW_ERROR;
|
2011-11-11 17:46:41 +00:00
|
|
|
|
2011-11-15 23:00:32 +00:00
|
|
|
buffer_failed:
|
|
|
|
GST_ERROR_OBJECT (dec, "Failed to create %u byte buffer", packet_size);
|
2011-11-11 17:46:41 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
static gboolean
|
|
|
|
gst_opus_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstOpusDec *dec = GST_OPUS_DEC (bdec);
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
GstStructure *s;
|
|
|
|
const GValue *streamheader;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (dec, "set_format: %" GST_PTR_FORMAT, caps);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
|
|
|
|
G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
|
|
|
|
gst_value_array_get_size (streamheader) >= 2) {
|
|
|
|
const GValue *header, *vorbiscomment;
|
|
|
|
GstBuffer *buf;
|
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
|
|
|
|
|
|
|
header = gst_value_array_get_value (streamheader, 0);
|
|
|
|
if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
|
|
|
|
buf = gst_value_get_buffer (header);
|
|
|
|
res = gst_opus_dec_parse_header (dec, buf);
|
|
|
|
if (res != GST_FLOW_OK)
|
|
|
|
goto done;
|
|
|
|
gst_buffer_replace (&dec->streamheader, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
vorbiscomment = gst_value_array_get_value (streamheader, 1);
|
|
|
|
if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
|
|
|
|
buf = gst_value_get_buffer (vorbiscomment);
|
|
|
|
res = gst_opus_dec_parse_comments (dec, buf);
|
|
|
|
if (res != GST_FLOW_OK)
|
|
|
|
goto done;
|
|
|
|
gst_buffer_replace (&dec->vorbiscomment, buf);
|
|
|
|
}
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2)
|
|
|
|
{
|
|
|
|
gsize size1, size2;
|
|
|
|
gboolean res;
|
2012-03-07 16:14:29 +00:00
|
|
|
GstMapInfo map;
|
2011-11-14 13:41:58 +00:00
|
|
|
|
|
|
|
size1 = gst_buffer_get_size (buf1);
|
|
|
|
size2 = gst_buffer_get_size (buf2);
|
|
|
|
|
|
|
|
if (size1 != size2)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_map (buf1, &map, GST_MAP_READ);
|
|
|
|
res = gst_buffer_memcmp (buf2, 0, map.data, map.size) == 0;
|
|
|
|
gst_buffer_unmap (buf1, &map);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_opus_dec_handle_frame (GstAudioDecoder * adec, GstBuffer * buf)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
2011-11-14 13:41:58 +00:00
|
|
|
GstFlowReturn res;
|
|
|
|
GstOpusDec *dec;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
/* no fancy draining */
|
|
|
|
if (G_UNLIKELY (!buf))
|
|
|
|
return GST_FLOW_OK;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
dec = GST_OPUS_DEC (adec);
|
|
|
|
GST_LOG_OBJECT (dec,
|
|
|
|
"Got buffer ts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
/* If we have the streamheader and vorbiscomment from the caps already
|
|
|
|
* ignore them here */
|
|
|
|
if (dec->streamheader && dec->vorbiscomment) {
|
|
|
|
if (memcmp_buffers (dec->streamheader, buf)) {
|
|
|
|
GST_DEBUG_OBJECT (dec, "found streamheader");
|
|
|
|
gst_audio_decoder_finish_frame (adec, NULL, 1);
|
|
|
|
res = GST_FLOW_OK;
|
|
|
|
} else if (memcmp_buffers (dec->vorbiscomment, buf)) {
|
|
|
|
GST_DEBUG_OBJECT (dec, "found vorbiscomments");
|
|
|
|
gst_audio_decoder_finish_frame (adec, NULL, 1);
|
|
|
|
res = GST_FLOW_OK;
|
|
|
|
} else {
|
2011-11-16 18:35:29 +00:00
|
|
|
res = opus_dec_chain_parse_data (dec, buf);
|
2011-11-14 13:41:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Otherwise fall back to packet counting and assume that the
|
2011-11-21 11:28:10 +00:00
|
|
|
* first two packets might be the headers, checking magic. */
|
2011-11-14 13:41:58 +00:00
|
|
|
switch (dec->packetno) {
|
|
|
|
case 0:
|
2011-11-21 17:48:54 +00:00
|
|
|
if (gst_opus_header_is_header (buf, "OpusHead", 8)) {
|
2011-11-16 01:14:32 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "found streamheader");
|
|
|
|
res = gst_opus_dec_parse_header (dec, buf);
|
|
|
|
gst_audio_decoder_finish_frame (adec, NULL, 1);
|
|
|
|
} else {
|
2011-11-16 18:35:29 +00:00
|
|
|
res = opus_dec_chain_parse_data (dec, buf);
|
2011-11-16 01:14:32 +00:00
|
|
|
}
|
2011-11-14 13:41:58 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2011-11-21 17:48:54 +00:00
|
|
|
if (gst_opus_header_is_header (buf, "OpusTags", 8)) {
|
2011-11-16 01:14:32 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
|
|
|
|
res = gst_opus_dec_parse_comments (dec, buf);
|
|
|
|
gst_audio_decoder_finish_frame (adec, NULL, 1);
|
|
|
|
} else {
|
2011-11-16 18:35:29 +00:00
|
|
|
res = opus_dec_chain_parse_data (dec, buf);
|
2011-11-16 01:14:32 +00:00
|
|
|
}
|
2011-11-14 13:41:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
2011-11-16 18:35:29 +00:00
|
|
|
res = opus_dec_chain_parse_data (dec, buf);
|
2011-11-14 13:41:58 +00:00
|
|
|
break;
|
2011-11-11 17:46:41 +00:00
|
|
|
}
|
2011-11-14 13:41:58 +00:00
|
|
|
}
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
dec->packetno++;
|
|
|
|
|
|
|
|
return res;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
2011-11-23 11:58:54 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opus_dec_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstOpusDec *dec = GST_OPUS_DEC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_USE_INBAND_FEC:
|
|
|
|
g_value_set_boolean (value, dec->use_inband_fec);
|
|
|
|
break;
|
2011-11-23 13:22:12 +00:00
|
|
|
case PROP_APPLY_GAIN:
|
|
|
|
g_value_set_boolean (value, dec->apply_gain);
|
|
|
|
break;
|
2011-11-23 11:58:54 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opus_dec_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstOpusDec *dec = GST_OPUS_DEC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_USE_INBAND_FEC:
|
|
|
|
dec->use_inband_fec = g_value_get_boolean (value);
|
|
|
|
break;
|
2011-11-23 13:22:12 +00:00
|
|
|
case PROP_APPLY_GAIN:
|
|
|
|
dec->apply_gain = g_value_get_boolean (value);
|
|
|
|
break;
|
2011-11-23 11:58:54 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|