#include <gst/gsttagsetter.h>
#include <gst/audio/audio.h>
+#include <gst/glib-compat-private.h>
#include "gstopusheader.h"
#include "gstopuscommon.h"
#include "gstopusenc.h"
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-raw, "
"format = (string) " FORMAT_STR ", "
+ "layout = (string) interleaved, "
"rate = (int) { 8000, 12000, 16000, 24000, 48000 }, "
"channels = (int) [ 1, 2 ] ")
);
static gboolean gst_opus_enc_sink_event (GstAudioEncoder * benc,
GstEvent * event);
+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,
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>");
+ "Vincent Penquerc'h <vincent.penquerch@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);
base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_opus_enc_handle_frame);
- base_class->event = GST_DEBUG_FUNCPTR (gst_opus_enc_sink_event);
+ base_class->sink_event = GST_DEBUG_FUNCPTR (gst_opus_enc_sink_event);
+ base_class->getcaps = GST_DEBUG_FUNCPTR (gst_opus_enc_sink_getcaps);
g_object_class_install_property (gobject_class, PROP_AUDIO,
g_param_spec_boolean ("audio", "Audio or voice",
gst_tag_list_free (enc->tags);
enc->tags = NULL;
g_slist_foreach (enc->headers, (GFunc) gst_buffer_unref, NULL);
+ g_slist_free (enc->headers);
enc->headers = NULL;
gst_tag_setter_reset_tags (GST_TAG_SETTER (enc));
{
gst_audio_encoder_set_latency (benc,
gst_opus_enc_get_latency (enc), gst_opus_enc_get_latency (enc));
- gst_audio_encoder_set_frame_samples_min (benc,
- enc->frame_samples * enc->n_channels * 2);
- gst_audio_encoder_set_frame_samples_max (benc,
- enc->frame_samples * enc->n_channels * 2);
- gst_audio_encoder_set_frame_max (benc, 0);
+ gst_audio_encoder_set_frame_samples_min (benc, enc->frame_samples);
+ gst_audio_encoder_set_frame_samples_max (benc, enc->frame_samples);
+ gst_audio_encoder_set_frame_max (benc, 1);
}
static gint
}
static void
-gst_opus_enc_setup_channel_mapping (GstOpusEnc * enc, const GstAudioInfo * info)
+gst_opus_enc_setup_trivial_mapping (GstOpusEnc * enc, guint8 mapping[256])
+{
+ int n;
+
+ for (n = 0; n < 255; ++n)
+ mapping[n] = n;
+}
+
+static int
+gst_opus_enc_find_channel_position (GstOpusEnc * enc, const GstAudioInfo * info,
+ GstAudioChannelPosition position)
+{
+ int n;
+ for (n = 0; n < enc->n_channels; ++n) {
+ if (GST_AUDIO_INFO_POSITION (info, n) == position) {
+ return n;
+ }
+ }
+ return -1;
+}
+
+static int
+gst_opus_enc_find_channel_position_in_vorbis_order (GstOpusEnc * enc,
+ GstAudioChannelPosition position)
+{
+ int c;
+
+ for (c = 0; c < enc->n_channels; ++c) {
+ if (gst_opus_channel_positions[enc->n_channels - 1][c] == position) {
+ GST_INFO_OBJECT (enc,
+ "Channel position %s maps to index %d in Vorbis order",
+ gst_opus_channel_names[position], c);
+ return c;
+ }
+ }
+ GST_WARNING_OBJECT (enc,
+ "Channel position %s is not representable in Vorbis order",
+ gst_opus_channel_names[position]);
+ return -1;
+}
+
+static void
+gst_opus_enc_setup_channel_mappings (GstOpusEnc * enc,
+ const GstAudioInfo * info)
{
#define MAPS(idx,pos) (GST_AUDIO_INFO_POSITION (info, (idx)) == GST_AUDIO_CHANNEL_POSITION_##pos)
enc->n_channels);
/* Start by setting up a default trivial mapping */
- for (n = 0; n < 255; ++n)
- enc->channel_mapping[n] = n;
+ enc->n_stereo_streams = 0;
+ gst_opus_enc_setup_trivial_mapping (enc, enc->encoding_channel_mapping);
+ gst_opus_enc_setup_trivial_mapping (enc, enc->decoding_channel_mapping);
/* For one channel, use the basic RTP mapping */
if (enc->n_channels == 1) {
GST_INFO_OBJECT (enc, "Mono, trivial RTP mapping");
enc->channel_mapping_family = 0;
- enc->channel_mapping[0] = 0;
+ /* implicit mapping for family 0 */
return;
}
if (MAPS (0, FRONT_LEFT) && MAPS (1, FRONT_RIGHT)) {
GST_INFO_OBJECT (enc, "Stereo, canonical mapping");
enc->channel_mapping_family = 0;
+ enc->n_stereo_streams = 1;
/* The channel mapping is implicit for family 0, that's why we do not
attempt to create one for right/left - this will be mapped to the
Vorbis mapping below. */
+ return;
} else {
GST_DEBUG_OBJECT (enc, "Stereo, but not canonical mapping, continuing");
}
/* For channels between 1 and 8, we use the Vorbis mapping if we can
find a permutation that matches it. Mono will have been taken care
- of earlier, but this code also handles it. */
+ of earlier, but this code also handles it. Same for left/right stereo.
+ There are two mappings. One maps the input channels to an ordering
+ which has the natural pairs first so they can benefit from the Opus
+ stereo channel coupling, and the other maps this ordering to the
+ Vorbis ordering. */
if (enc->n_channels >= 1 && enc->n_channels <= 8) {
+ int c0, c1, c0v, c1v;
+ int mapped;
+ gboolean positions_done[256];
+ static const GstAudioChannelPosition pairs[][2] = {
+ {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
+ GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT},
+ {GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
+ GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT},
+ {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
+ GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER},
+ {GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
+ GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER},
+ {GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
+ GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT},
+ };
+ size_t pair;
+
GST_DEBUG_OBJECT (enc,
- "In range for the Vorbis mapping, checking channel positions");
- for (n = 0; n < enc->n_channels; ++n) {
- GstAudioChannelPosition pos = GST_AUDIO_INFO_POSITION (info, n);
- int c;
-
- GST_DEBUG_OBJECT (enc, "Channel %d has position %d (%s)", n, pos,
- gst_opus_channel_names[pos]);
- for (c = 0; c < enc->n_channels; ++c) {
- if (gst_opus_channel_positions[enc->n_channels - 1][c] == pos) {
- GST_DEBUG_OBJECT (enc, "Found in Vorbis mapping as channel %d", c);
- break;
+ "In range for the Vorbis mapping, building channel mapping tables");
+
+ enc->n_stereo_streams = 0;
+ mapped = 0;
+ for (n = 0; n < 256; ++n)
+ positions_done[n] = FALSE;
+
+ /* First, find any natural pairs, and move them to the front */
+ for (pair = 0; pair < G_N_ELEMENTS (pairs); ++pair) {
+ GstAudioChannelPosition p0 = pairs[pair][0];
+ GstAudioChannelPosition p1 = pairs[pair][1];
+ c0 = gst_opus_enc_find_channel_position (enc, info, p0);
+ c1 = gst_opus_enc_find_channel_position (enc, info, p1);
+ if (c0 >= 0 && c1 >= 0) {
+ /* We found a natural pair */
+ GST_DEBUG_OBJECT (enc, "Natural pair '%s/%s' found at %d %d",
+ gst_opus_channel_names[p0], gst_opus_channel_names[p1], c0, c1);
+ /* Find where they map in Vorbis order */
+ c0v = gst_opus_enc_find_channel_position_in_vorbis_order (enc, p0);
+ c1v = gst_opus_enc_find_channel_position_in_vorbis_order (enc, p1);
+ if (c0v < 0 || c1v < 0) {
+ GST_WARNING_OBJECT (enc,
+ "Cannot map channel positions to Vorbis order, using unknown mapping");
+ enc->channel_mapping_family = 255;
+ enc->n_stereo_streams = 0;
+ return;
}
+
+ enc->encoding_channel_mapping[mapped] = c0;
+ enc->encoding_channel_mapping[mapped + 1] = c1;
+ enc->decoding_channel_mapping[c0v] = mapped;
+ enc->decoding_channel_mapping[c1v] = mapped + 1;
+ enc->n_stereo_streams++;
+ mapped += 2;
+ positions_done[p0] = positions_done[p1] = TRUE;
}
- if (c == enc->n_channels) {
- /* We did not find that position, so use undefined */
- GST_WARNING_OBJECT (enc,
- "Position %d (%s) not found in Vorbis mapping, using unknown mapping",
- pos, gst_opus_channel_positions[pos]);
- enc->channel_mapping_family = 255;
- return;
+ }
+
+ /* Now add all other input channels as mono streams */
+ for (n = 0; n < enc->n_channels; ++n) {
+ GstAudioChannelPosition position = GST_AUDIO_INFO_POSITION (info, n);
+
+ /* if we already mapped it while searching for pairs, nothing else
+ needs to be done */
+ if (!positions_done[position]) {
+ int cv;
+ GST_DEBUG_OBJECT (enc, "Channel position %s is not mapped yet, adding",
+ gst_opus_channel_names[position]);
+ cv = gst_opus_enc_find_channel_position_in_vorbis_order (enc, position);
+ if (cv < 0) {
+ GST_WARNING_OBJECT (enc,
+ "Cannot map channel positions to Vorbis order, using unknown mapping");
+ enc->channel_mapping_family = 255;
+ enc->n_stereo_streams = 0;
+ return;
+ }
+ enc->encoding_channel_mapping[mapped] = n;
+ enc->decoding_channel_mapping[cv] = mapped;
+ mapped++;
}
- GST_DEBUG_OBJECT (enc, "Mapping output channel %d to %d (%s)", c, n,
- gst_opus_channel_names[pos]);
- enc->channel_mapping[c] = n;
}
- GST_INFO_OBJECT (enc, "Permutation found, using Vorbis mapping");
+
+#ifndef GST_DISABLE_DEBUG
+ GST_INFO_OBJECT (enc,
+ "Mapping tables built: %d channels, %d stereo streams", enc->n_channels,
+ enc->n_stereo_streams);
+ gst_opus_common_log_channel_mapping_table (GST_ELEMENT (enc), opusenc_debug,
+ "Encoding mapping table", enc->n_channels,
+ enc->encoding_channel_mapping);
+ gst_opus_common_log_channel_mapping_table (GST_ELEMENT (enc), opusenc_debug,
+ "Decoding mapping table", enc->n_channels,
+ enc->decoding_channel_mapping);
+#endif
+
enc->channel_mapping_family = 1;
return;
}
- /* For other cases, we use undefined, with the default trivial mapping */
+ /* More than 8 channels, if future mappings are added for those */
+
+ /* For other cases, we use undefined, with the default trivial mapping
+ and all mono streams */
GST_WARNING_OBJECT (enc, "Unknown mapping");
enc->channel_mapping_family = 255;
+ enc->n_stereo_streams = 0;
#undef MAPS
}
enc->n_channels = GST_AUDIO_INFO_CHANNELS (info);
enc->sample_rate = GST_AUDIO_INFO_RATE (info);
- gst_opus_enc_setup_channel_mapping (enc, info);
+ gst_opus_enc_setup_channel_mappings (enc, info);
GST_DEBUG_OBJECT (benc, "Setup with %d channels, %d Hz", enc->n_channels,
enc->sample_rate);
static gboolean
gst_opus_enc_setup (GstOpusEnc * enc)
{
- int error = OPUS_OK, n;
- guint8 trivial_mapping[256];
-
- GST_DEBUG_OBJECT (enc, "setup");
-
- for (n = 0; n < 256; ++n)
- trivial_mapping[n] = n;
+ int error = OPUS_OK;
+
+#ifndef GST_DISABLE_DEBUG
+ GST_DEBUG_OBJECT (enc,
+ "setup: %d Hz, %d channels, %d stereo streams, family %d",
+ enc->sample_rate, enc->n_channels, enc->n_stereo_streams,
+ enc->channel_mapping_family);
+ GST_INFO_OBJECT (enc, "Mapping tables built: %d channels, %d stereo streams",
+ enc->n_channels, enc->n_stereo_streams);
+ gst_opus_common_log_channel_mapping_table (GST_ELEMENT (enc), opusenc_debug,
+ "Encoding mapping table", enc->n_channels, enc->encoding_channel_mapping);
+ gst_opus_common_log_channel_mapping_table (GST_ELEMENT (enc), opusenc_debug,
+ "Decoding mapping table", enc->n_channels, enc->decoding_channel_mapping);
+#endif
- enc->state =
- opus_multistream_encoder_create (enc->sample_rate, enc->n_channels,
- enc->n_channels, 0, trivial_mapping,
+ enc->state = opus_multistream_encoder_create (enc->sample_rate,
+ enc->n_channels, enc->n_channels - enc->n_stereo_streams,
+ enc->n_stereo_streams, enc->encoding_channel_mapping,
enc->audio_or_voip ? OPUS_APPLICATION_AUDIO : OPUS_APPLICATION_VOIP,
&error);
if (!enc->state || error != OPUS_OK)
break;
}
- return FALSE;
+ return GST_AUDIO_ENCODER_CLASS (parent_class)->sink_event (benc, event);
+}
+
+static GstCaps *
+gst_opus_enc_sink_getcaps (GstAudioEncoder * benc, GstCaps * filter)
+{
+ GstOpusEnc *enc;
+ GstCaps *caps;
+ GstCaps *peercaps = NULL;
+ GstCaps *intersect = NULL;
+ guint i;
+ gboolean allow_multistream;
+
+ enc = GST_OPUS_ENC (benc);
+
+ GST_DEBUG_OBJECT (enc, "sink getcaps");
+
+ peercaps = gst_pad_peer_query_caps (GST_AUDIO_ENCODER_SRC_PAD (benc), NULL);
+ if (!peercaps) {
+ GST_DEBUG_OBJECT (benc, "No peercaps, returning template sink caps");
+ return
+ gst_caps_copy (gst_pad_get_pad_template_caps
+ (GST_AUDIO_ENCODER_SINK_PAD (benc)));
+ }
+
+ intersect = gst_caps_intersect (peercaps,
+ gst_pad_get_pad_template_caps (GST_AUDIO_ENCODER_SRC_PAD (benc)));
+ gst_caps_unref (peercaps);
+
+ if (gst_caps_is_empty (intersect))
+ return intersect;
+
+ allow_multistream = FALSE;
+ for (i = 0; i < gst_caps_get_size (intersect); i++) {
+ GstStructure *s = gst_caps_get_structure (intersect, i);
+ gboolean multistream;
+ if (gst_structure_get_boolean (s, "multistream", &multistream)) {
+ if (multistream) {
+ allow_multistream = TRUE;
+ }
+ } else {
+ allow_multistream = TRUE;
+ }
+ }
+
+ gst_caps_unref (intersect);
+
+ caps =
+ gst_caps_copy (gst_pad_get_pad_template_caps (GST_AUDIO_ENCODER_SINK_PAD
+ (benc)));
+ if (!allow_multistream) {
+ GValue range = { 0 };
+ g_value_init (&range, GST_TYPE_INT_RANGE);
+ gst_value_set_int_range (&range, 1, 2);
+ for (i = 0; i < gst_caps_get_size (caps); i++) {
+ GstStructure *s = gst_caps_get_structure (caps, i);
+ gst_structure_set_value (s, "channels", &range);
+ }
+ 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
gsize bsize, size;
gsize bytes = enc->frame_samples * enc->n_channels * 2;
gint ret = GST_FLOW_OK;
+ GstMapInfo map;
+ GstMapInfo omap;
+ gint outsize;
+ GstBuffer *outbuf;
g_mutex_lock (enc->property_lock);
if (G_LIKELY (buf)) {
- bdata = gst_buffer_map (buf, &bsize, NULL, GST_MAP_READ);
+ gst_buffer_map (buf, &map, GST_MAP_READ);
+ bdata = map.data;
+ bsize = map.size;
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 {
data = bdata;
goto done;
}
- while (size) {
- gint encoded_size;
- unsigned char *out_data;
- gsize out_size;
- GstBuffer *outbuf;
-
- 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);
-
- 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, out_data, enc->max_payload_size * enc->n_channels);
- gst_buffer_unmap (outbuf, out_data, out_size);
-
- if (encoded_size < 0) {
- GST_ERROR_OBJECT (enc, "Encoding failed: %d", encoded_size);
- ret = GST_FLOW_ERROR;
- goto done;
- } else if (encoded_size > enc->max_payload_size) {
- GST_WARNING_OBJECT (enc,
- "Encoded size %d is higher than max payload size (%d bytes)",
- out_size, enc->max_payload_size);
- ret = GST_FLOW_ERROR;
- goto done;
- }
+ g_assert (size == bytes);
- GST_DEBUG_OBJECT (enc, "Output packet is %u bytes", encoded_size);
- gst_buffer_set_size (outbuf, encoded_size);
+ 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);
+
+ gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
+
+ GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)",
+ enc->frame_samples, (int) bytes);
- ret =
- gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), outbuf,
- enc->frame_samples);
+ outsize =
+ opus_multistream_encode (enc->state, (const gint16 *) data,
+ enc->frame_samples, omap.data, enc->max_payload_size * enc->n_channels);
- if ((GST_FLOW_OK != ret) && (GST_FLOW_NOT_LINKED != ret))
- goto done;
+ gst_buffer_unmap (outbuf, &omap);
- data += bytes;
- size -= bytes;
+ if (outsize < 0) {
+ GST_ERROR_OBJECT (enc, "Encoding failed: %d", outsize);
+ ret = GST_FLOW_ERROR;
+ goto done;
+ } else if (outsize > enc->max_payload_size) {
+ GST_WARNING_OBJECT (enc,
+ "Encoded size %d is higher than max payload size (%d bytes)",
+ outsize, enc->max_payload_size);
+ ret = GST_FLOW_ERROR;
+ goto done;
}
+ GST_DEBUG_OBJECT (enc, "Output packet is %u bytes", outsize);
+ gst_buffer_set_size (outbuf, outsize);
+
+ ret =
+ gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), outbuf,
+ enc->frame_samples);
+
done:
if (bdata)
- gst_buffer_unmap (buf, bdata, bsize);
+ gst_buffer_unmap (buf, &map);
g_mutex_unlock (enc->property_lock);
if (mdata)
GstCaps *caps;
g_slist_foreach (enc->headers, (GFunc) gst_buffer_unref, NULL);
+ g_slist_free (enc->headers);
enc->headers = NULL;
gst_opus_header_create_caps (&caps, &enc->headers, enc->n_channels,
- enc->sample_rate, enc->channel_mapping_family, enc->channel_mapping,
+ enc->n_stereo_streams, enc->sample_rate, enc->channel_mapping_family,
+ enc->decoding_channel_mapping,
gst_tag_setter_get_tag_list (GST_TAG_SETTER (enc)));
/* negotiate with these caps */
GST_DEBUG_OBJECT (enc, "here are the caps: %" GST_PTR_FORMAT, caps);
- gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc), caps);
+ gst_audio_encoder_set_output_format (benc, caps);
+ gst_caps_unref (caps);
enc->header_sent = TRUE;
}