GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
- GST_STATIC_CAPS ("audio/x-raw-int, "
+ GST_STATIC_CAPS ("audio/x-raw, "
+ "format = (string) { " GST_AUDIO_NE (S16) " }, "
"rate = (int) { 48000, 24000, 16000, 12000, 8000 }, "
- "channels = (int) [ 1, 8 ], "
- "endianness = (int) BYTE_ORDER, "
- "signed = (boolean) true, " "width = (int) 16, " "depth = (int) 16")
+ "channels = (int) [ 1, 8 ] ")
);
static GstStaticPadTemplate opus_dec_sink_factory =
GST_STATIC_CAPS ("audio/x-opus")
);
+G_DEFINE_TYPE (GstOpusDec, gst_opus_dec, GST_TYPE_AUDIO_DECODER);
+
#define DB_TO_LINEAR(x) pow (10., (x) / 20.)
#define DEFAULT_USE_INBAND_FEC FALSE
PROP_APPLY_GAIN
};
-GST_BOILERPLATE (GstOpusDec, gst_opus_dec, GstAudioDecoder,
- GST_TYPE_AUDIO_DECODER);
static GstFlowReturn gst_opus_dec_parse_header (GstOpusDec * dec,
GstBuffer * buf);
static void
-gst_opus_dec_base_init (gpointer g_class)
-{
- GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
-
- gst_element_class_add_static_pad_template (element_class,
- &opus_dec_src_factory);
- gst_element_class_add_static_pad_template (element_class,
- &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>");
-}
-
-static void
gst_opus_dec_class_init (GstOpusDecClass * klass)
{
GObjectClass *gobject_class;
GstAudioDecoderClass *adclass;
+ GstElementClass *element_class;
gobject_class = (GObjectClass *) klass;
adclass = (GstAudioDecoderClass *) klass;
+ element_class = (GstElementClass *) klass;
gobject_class->set_property = gst_opus_dec_set_property;
gobject_class->get_property = gst_opus_dec_get_property;
adclass->handle_frame = GST_DEBUG_FUNCPTR (gst_opus_dec_handle_frame);
adclass->set_format = GST_DEBUG_FUNCPTR (gst_opus_dec_set_format);
+ 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>");
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,
}
static void
-gst_opus_dec_init (GstOpusDec * dec, GstOpusDecClass * g_class)
+gst_opus_dec_init (GstOpusDec * dec)
{
dec->sample_rate = 0;
dec->n_channels = 0;
static GstFlowReturn
gst_opus_dec_parse_header (GstOpusDec * dec, GstBuffer * buf)
{
- const guint8 *data = GST_BUFFER_DATA (buf);
+ const guint8 *data;
GstCaps *caps;
const GstAudioChannelPosition *pos = NULL;
g_return_val_if_fail (gst_opus_header_is_id_header (buf), GST_FLOW_ERROR);
+
+ data = gst_buffer_map (buf, NULL, NULL, GST_MAP_READ);
+
g_return_val_if_fail (dec->n_channels == 0
|| dec->n_channels == data[9], GST_FLOW_ERROR);
gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps);
gst_caps_unref (caps);
+ gst_buffer_unmap (buf, (guint8 *) data, -1);
+
return GST_FLOW_OK;
}
+
static GstFlowReturn
gst_opus_dec_parse_comments (GstOpusDec * dec, GstBuffer * buf)
{
opus_dec_chain_parse_data (GstOpusDec * dec, GstBuffer * buffer)
{
GstFlowReturn res = GST_FLOW_OK;
- gint size;
+ gsize size, out_size;
guint8 *data;
GstBuffer *outbuf;
gint16 *out_data;
if (buffer) {
GST_DEBUG_OBJECT (dec, "Received buffer of size %u",
- GST_BUFFER_SIZE (buffer));
+ gst_buffer_get_size (buffer));
} else {
GST_DEBUG_OBJECT (dec, "Received missing buffer");
}
buf = dec->use_inband_fec && dec->last_buffer ? dec->last_buffer : buffer;
if (buf) {
- data = GST_BUFFER_DATA (buf);
- size = GST_BUFFER_SIZE (buf);
+ data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
+
GST_DEBUG_OBJECT (dec, "Using buffer of size %u", size);
} else {
/* concealment data, pass NULL as the bits parameters */
samples = 120 * dec->sample_rate / 1000;
packet_size = samples * dec->n_channels * 2;
- res = gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
- GST_BUFFER_OFFSET_NONE, packet_size,
- GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
-
- if (res != GST_FLOW_OK) {
- GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
- return res;
+ outbuf = gst_buffer_new_and_alloc (packet_size);
+ if (!outbuf) {
+ goto buffer_failed;
}
- out_data = (gint16 *) GST_BUFFER_DATA (outbuf);
+ out_data = (gint16 *) gst_buffer_map (outbuf, &out_size, NULL, GST_MAP_WRITE);
if (dec->use_inband_fec) {
if (dec->last_buffer) {
/* normal decode */
n = opus_multistream_decode (dec->state, data, size, out_data, samples, 0);
}
+ gst_buffer_unmap (buf, data, size);
+ gst_buffer_unmap (outbuf, out_data, out_size);
if (n < 0) {
GST_ELEMENT_ERROR (dec, STREAM, DECODE, ("Decoding error: %d", n), (NULL));
return GST_FLOW_ERROR;
}
GST_DEBUG_OBJECT (dec, "decoded %d samples", n);
- GST_BUFFER_SIZE (outbuf) = n * 2 * dec->n_channels;
+ gst_buffer_set_size (outbuf, n * 2 * dec->n_channels);
/* 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;
- GST_BUFFER_SIZE (outbuf) -= skip * 2 * dec->n_channels;
- GST_BUFFER_DATA (outbuf) += skip * 2 * dec->n_channels;
+
+ gst_buffer_resize (outbuf, skip * 2 * dec->n_channels, -1);
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);
}
- if (GST_BUFFER_SIZE (outbuf) == 0) {
+ if (gst_buffer_get_size (outbuf) == 0) {
gst_buffer_unref (outbuf);
outbuf = NULL;
}
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) {
- unsigned int i, nsamples = GST_BUFFER_SIZE (outbuf) / 2;
+ gsize rsize;
+ unsigned int i, nsamples;
double volume = dec->r128_gain_volume;
- gint16 *samples = (gint16 *) GST_BUFFER_DATA (outbuf);
+ gint16 *samples =
+ (gint16 *) gst_buffer_map (outbuf, &rsize, NULL, GST_MAP_READWRITE);
+
GST_DEBUG_OBJECT (dec, "Applying gain: volume %f", volume);
+ nsamples = rsize / 2;
for (i = 0; i < nsamples; ++i) {
int sample = (int) (samples[i] * volume + 0.5);
samples[i] = sample < -32768 ? -32768 : sample > 32767 ? 32767 : sample;
}
+ gst_buffer_unmap (outbuf, samples, rsize);
}
res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
creation_failed:
GST_ERROR_OBJECT (dec, "Failed to create Opus decoder: %d", err);
return GST_FLOW_ERROR;
+
+buffer_failed:
+ GST_ERROR_OBJECT (dec, "Failed to create %u byte buffer", packet_size);
+ return GST_FLOW_ERROR;
}
static gboolean
memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2)
{
gsize size1, size2;
+ gpointer data1;
+ gboolean res;
- size1 = GST_BUFFER_SIZE (buf1);
- size2 = GST_BUFFER_SIZE (buf2);
+ size1 = gst_buffer_get_size (buf1);
+ size2 = gst_buffer_get_size (buf2);
if (size1 != size2)
return FALSE;
- return !memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2), size1);
+ data1 = gst_buffer_map (buf1, NULL, NULL, GST_MAP_READ);
+ res = gst_buffer_memcmp (buf2, 0, data1, size1) == 0;
+ gst_buffer_unmap (buf1, data1, size1);
+
+ return res;
}
static GstFlowReturn
return id;
}
+#define FORMAT_STR GST_AUDIO_NE(S16)
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
- GST_STATIC_CAPS ("audio/x-raw-int, "
- "rate = (int) { 48000, 24000, 16000, 12000, 8000 }, "
- "channels = (int) [ 1, 8 ], "
- "endianness = (int) BYTE_ORDER, "
- "signed = (boolean) TRUE, " "width = (int) 16, " "depth = (int) 16")
+ GST_STATIC_CAPS ("audio/x-raw, "
+ "format = (string) " FORMAT_STR ", "
+ "rate = (int) { 8000, 12000, 16000, 24000, 48000 }, "
+ "channels = (int) [ 1, 2 ] ")
);
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
static gboolean gst_opus_enc_sink_event (GstAudioEncoder * benc,
GstEvent * event);
-static GstCaps *gst_opus_enc_sink_getcaps (GstAudioEncoder * benc);
+static GstCaps *gst_opus_enc_sink_getcaps (GstAudioEncoder * benc,
+ GstCaps * filter);
static gboolean gst_opus_enc_setup (GstOpusEnc * enc);
static void gst_opus_enc_get_property (GObject * object, guint prop_id,
static GstFlowReturn gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buffer);
-static void
-gst_opus_enc_setup_interfaces (GType opusenc_type)
-{
- static const GInterfaceInfo tag_setter_info = { NULL, NULL, NULL };
- const GInterfaceInfo preset_interface_info = {
- NULL, /* interface_init */
- NULL, /* interface_finalize */
- NULL /* interface_data */
- };
-
- g_type_add_interface_static (opusenc_type, GST_TYPE_TAG_SETTER,
- &tag_setter_info);
- g_type_add_interface_static (opusenc_type, GST_TYPE_PRESET,
- &preset_interface_info);
-
- GST_DEBUG_CATEGORY_INIT (opusenc_debug, "opusenc", 0, "Opus encoder");
-}
-
-GST_BOILERPLATE_FULL (GstOpusEnc, gst_opus_enc, GstAudioEncoder,
- GST_TYPE_AUDIO_ENCODER, gst_opus_enc_setup_interfaces);
-
-static void
-gst_opus_enc_base_init (gpointer g_class)
-{
- GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
-
- gst_element_class_add_static_pad_template (element_class, &src_factory);
- gst_element_class_add_static_pad_template (element_class, &sink_factory);
- gst_element_class_set_details_simple (element_class, "Opus audio encoder",
- "Codec/Encoder/Audio",
- "Encodes audio in Opus format",
- "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
-}
+#define gst_opus_enc_parent_class parent_class
+G_DEFINE_TYPE_WITH_CODE (GstOpusEnc, gst_opus_enc, GST_TYPE_AUDIO_ENCODER,
+ G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL);
+ G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL));
static void
gst_opus_enc_class_init (GstOpusEncClass * klass)
{
GObjectClass *gobject_class;
GstAudioEncoderClass *base_class;
+ GstElementClass *gstelement_class;
gobject_class = (GObjectClass *) klass;
base_class = (GstAudioEncoderClass *) klass;
+ gstelement_class = (GstElementClass *) klass;
gobject_class->set_property = gst_opus_enc_set_property;
gobject_class->get_property = gst_opus_enc_get_property;
+ gst_element_class_add_pad_template (gstelement_class,
+ gst_static_pad_template_get (&src_factory));
+ gst_element_class_add_pad_template (gstelement_class,
+ gst_static_pad_template_get (&sink_factory));
+ gst_element_class_set_details_simple (gstelement_class, "Opus audio encoder",
+ "Codec/Encoder/Audio",
+ "Encodes audio in Opus format",
+ "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
+
base_class->start = GST_DEBUG_FUNCPTR (gst_opus_enc_start);
base_class->stop = GST_DEBUG_FUNCPTR (gst_opus_enc_stop);
base_class->set_format = GST_DEBUG_FUNCPTR (gst_opus_enc_set_format);
GST_PARAM_MUTABLE_PLAYING));
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_opus_enc_finalize);
+
+ GST_DEBUG_CATEGORY_INIT (opusenc_debug, "opusenc", 0, "Opus encoder");
}
static void
}
static void
-gst_opus_enc_init (GstOpusEnc * enc, GstOpusEncClass * klass)
+gst_opus_enc_init (GstOpusEnc * enc)
{
GstAudioEncoder *benc = GST_AUDIO_ENCODER (enc);
GstOpusEnc *enc = GST_OPUS_ENC (benc);
GST_DEBUG_OBJECT (enc, "start");
- enc->tags = gst_tag_list_new ();
+ enc->tags = gst_tag_list_new_empty ();
enc->header_sent = FALSE;
return TRUE;
gst_tag_setter_merge_tags (setter, list, mode);
break;
}
+
default:
break;
}
}
static GstCaps *
-gst_opus_enc_sink_getcaps (GstAudioEncoder * benc)
+gst_opus_enc_sink_getcaps (GstAudioEncoder * benc, GstCaps * filter)
{
GstOpusEnc *enc;
GstCaps *caps;
GST_DEBUG_OBJECT (enc, "sink getcaps");
- peercaps = gst_pad_peer_get_caps (GST_AUDIO_ENCODER_SRC_PAD (benc));
+ peercaps = gst_pad_peer_query_caps (GST_AUDIO_ENCODER_SRC_PAD (benc), filter);
if (!peercaps) {
GST_DEBUG_OBJECT (benc, "No peercaps, returning template sink caps");
return
g_value_unset (&range);
}
+ if (filter) {
+ GstCaps *tmp = gst_caps_intersect_full (caps, filter,
+ GST_CAPS_INTERSECT_FIRST);
+ gst_caps_unref (caps);
+ caps = tmp;
+ }
+
GST_DEBUG_OBJECT (enc, "Returning caps: %" GST_PTR_FORMAT, caps);
return caps;
}
static GstFlowReturn
gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buf)
{
- guint8 *bdata, *data, *mdata = NULL;
+ guint8 *bdata = NULL, *data, *mdata = NULL;
gsize bsize, size;
- gsize bytes;
+ gsize bytes = enc->frame_samples * enc->n_channels * 2;
gint ret = GST_FLOW_OK;
g_mutex_lock (enc->property_lock);
- bytes = enc->frame_samples * enc->n_channels * 2;
if (G_LIKELY (buf)) {
- bdata = GST_BUFFER_DATA (buf);
- bsize = GST_BUFFER_SIZE (buf);
+ bdata = gst_buffer_map (buf, &bsize, NULL, GST_MAP_READ);
+
if (G_UNLIKELY (bsize % bytes)) {
GST_DEBUG_OBJECT (enc, "draining; adding silence samples");
size = ((bsize / bytes) + 1) * bytes;
mdata = g_malloc0 (size);
memcpy (mdata, bdata, bsize);
+ gst_buffer_unmap (buf, bdata, bsize);
bdata = NULL;
data = mdata;
} else {
goto done;
}
-
while (size) {
- gint outsize;
+ gint encoded_size;
+ unsigned char *out_data;
+ gsize out_size;
GstBuffer *outbuf;
- ret = gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc),
- GST_BUFFER_OFFSET_NONE, enc->max_payload_size * enc->n_channels,
- GST_PAD_CAPS (GST_AUDIO_ENCODER_SRC_PAD (enc)), &outbuf);
-
- if (GST_FLOW_OK != ret)
+ outbuf = gst_buffer_new_and_alloc (enc->max_payload_size * enc->n_channels);
+ if (!outbuf)
goto done;
GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)",
enc->frame_samples, (int) bytes);
- outsize =
+ out_data = gst_buffer_map (outbuf, &out_size, NULL, GST_MAP_WRITE);
+ encoded_size =
opus_multistream_encode (enc->state, (const gint16 *) data,
- enc->frame_samples, GST_BUFFER_DATA (outbuf),
- enc->max_payload_size * enc->n_channels);
+ enc->frame_samples, out_data, enc->max_payload_size * enc->n_channels);
+ gst_buffer_unmap (outbuf, out_data, out_size);
- if (outsize < 0) {
- GST_ERROR_OBJECT (enc, "Encoding failed: %d", outsize);
+ if (encoded_size < 0) {
+ GST_ERROR_OBJECT (enc, "Encoding failed: %d", encoded_size);
ret = GST_FLOW_ERROR;
goto done;
- } else if (outsize > enc->max_payload_size) {
+ } else if (encoded_size > enc->max_payload_size) {
GST_WARNING_OBJECT (enc,
"Encoded size %d is higher than max payload size (%d bytes)",
- outsize, enc->max_payload_size);
+ out_size, enc->max_payload_size);
ret = GST_FLOW_ERROR;
goto done;
}
- GST_DEBUG_OBJECT (enc, "Output packet is %u bytes", outsize);
- GST_BUFFER_SIZE (outbuf) = outsize;
+ GST_DEBUG_OBJECT (enc, "Output packet is %u bytes", encoded_size);
+ gst_buffer_set_size (outbuf, encoded_size);
ret =
gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), outbuf,
done:
+ if (bdata)
+ gst_buffer_unmap (buf, bdata, bsize);
g_mutex_unlock (enc->property_lock);
if (mdata)
}
GST_DEBUG_OBJECT (enc, "received buffer %p of %u bytes", buf,
- buf ? GST_BUFFER_SIZE (buf) : 0);
+ buf ? gst_buffer_get_size (buf) : 0);
ret = gst_opus_enc_encode (enc, buf);
if (tags == NULL) {
/* FIXME: better fix chain of callers to not write metadata at all,
* if there is none */
- empty_tags = gst_tag_list_new ();
+ empty_tags = gst_tag_list_new_empty ();
tags = empty_tags;
}
comments =
{
int n_streams, family;
gboolean multistream;
+ guint8 *data;
+ gsize size;
g_return_if_fail (caps);
g_return_if_fail (headers && !*headers);
- g_return_if_fail (GST_BUFFER_SIZE (buf1) >= 19);
+ g_return_if_fail (gst_buffer_get_size (buf1) >= 19);
+
+ data = gst_buffer_map (buf1, &size, NULL, GST_MAP_READ);
/* work out the number of streams */
- family = GST_BUFFER_DATA (buf1)[18];
+ family = data[18];
if (family == 0) {
n_streams = 1;
} else {
/* only included in the header for family > 0 */
- g_return_if_fail (GST_BUFFER_SIZE (buf1) >= 20);
- n_streams = GST_BUFFER_DATA (buf1)[19];
+ if (size >= 20)
+ n_streams = data[19];
+ else {
+ g_warning ("family > 0 but header buffer size < 20");
+ gst_buffer_unmap (buf1, data, size);
+ return;
+ }
}
+ gst_buffer_unmap (buf1, data, size);
+
/* mark and put on caps */
multistream = n_streams > 1;
*caps = gst_caps_new_simple ("audio/x-opus",
gboolean
gst_opus_header_is_header (GstBuffer * buf, const char *magic, guint magic_size)
{
- return (GST_BUFFER_SIZE (buf) >= magic_size
- && !memcmp (magic, GST_BUFFER_DATA (buf), magic_size));
+ return (gst_buffer_get_size (buf) >= magic_size
+ && !gst_buffer_memcmp (buf, 0, magic, magic_size));
}
gboolean
gst_opus_header_is_id_header (GstBuffer * buf)
{
- gsize size = GST_BUFFER_SIZE (buf);
- const guint8 *data = GST_BUFFER_DATA (buf);
+ gsize size = gst_buffer_get_size (buf);
+ guint8 *data = NULL;
guint8 channels, channel_mapping_family, n_streams, n_stereo_streams;
+ gboolean ret = FALSE;
if (size < 19)
- return FALSE;
+ goto beach;
if (!gst_opus_header_is_header (buf, "OpusHead", 8))
- return FALSE;
+ goto beach;
+
+ data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
+
channels = data[9];
+
if (channels == 0)
- return FALSE;
+ goto beach;
+
channel_mapping_family = data[18];
+
if (channel_mapping_family == 0) {
if (channels > 2)
- return FALSE;
+ goto beach;
} else {
channels = data[9];
if (size < 21 + channels)
- return FALSE;
+ goto beach;
n_streams = data[19];
n_stereo_streams = data[20];
if (n_streams == 0)
- return FALSE;
+ goto beach;
if (n_stereo_streams > n_streams)
- return FALSE;
+ goto beach;
if (n_streams + n_stereo_streams > 255)
- return FALSE;
+ goto beach;
}
- return TRUE;
+ ret = TRUE;
+
+beach:
+ if (data)
+ gst_buffer_unmap (buf, data, size);
+ return ret;
}
gboolean