2011-06-05 07:54:19 +00:00
|
|
|
/* GStreamer Opus Encoder
|
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* Copyright (C) <2008> Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
2011-11-20 09:52:46 +00:00
|
|
|
* Copyright (C) <2011> Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
|
2011-06-05 07:54:19 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-06-05 07:54:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Based on the speexenc element
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-opusenc
|
|
|
|
* @see_also: opusdec, oggmux
|
|
|
|
*
|
|
|
|
* This element encodes raw audio to OPUS.
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example pipelines</title>
|
|
|
|
* |[
|
|
|
|
* gst-launch -v audiotestsrc wave=sine num-buffers=100 ! audioconvert ! opusenc ! oggmux ! filesink location=sine.ogg
|
|
|
|
* ]| Encode a test sine signal to Ogg/OPUS.
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <math.h>
|
2015-04-28 16:24:04 +00:00
|
|
|
#include <opus.h>
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
#include <gst/gsttagsetter.h>
|
|
|
|
#include <gst/audio/audio.h>
|
2015-11-03 12:50:53 +00:00
|
|
|
#include <gst/pbutils/pbutils.h>
|
|
|
|
#include <gst/tag/tag.h>
|
2012-01-27 14:49:58 +00:00
|
|
|
#include <gst/glib-compat-private.h>
|
2011-11-20 09:52:46 +00:00
|
|
|
#include "gstopusheader.h"
|
2011-11-24 13:29:56 +00:00
|
|
|
#include "gstopuscommon.h"
|
2011-06-05 07:54:19 +00:00
|
|
|
#include "gstopusenc.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (opusenc_debug);
|
|
|
|
#define GST_CAT_DEFAULT opusenc_debug
|
|
|
|
|
2011-11-22 16:14:06 +00:00
|
|
|
/* Some arbitrary bounds beyond which it really doesn't make sense.
|
|
|
|
The spec mentions 6 kb/s to 510 kb/s, so 4000 and 650000 ought to be
|
|
|
|
safe as property bounds. */
|
|
|
|
#define LOWEST_BITRATE 4000
|
|
|
|
#define HIGHEST_BITRATE 650000
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
#define GST_OPUS_ENC_TYPE_BANDWIDTH (gst_opus_enc_bandwidth_get_type())
|
|
|
|
static GType
|
|
|
|
gst_opus_enc_bandwidth_get_type (void)
|
|
|
|
{
|
|
|
|
static const GEnumValue values[] = {
|
|
|
|
{OPUS_BANDWIDTH_NARROWBAND, "Narrow band", "narrowband"},
|
|
|
|
{OPUS_BANDWIDTH_MEDIUMBAND, "Medium band", "mediumband"},
|
|
|
|
{OPUS_BANDWIDTH_WIDEBAND, "Wide band", "wideband"},
|
|
|
|
{OPUS_BANDWIDTH_SUPERWIDEBAND, "Super wide band", "superwideband"},
|
|
|
|
{OPUS_BANDWIDTH_FULLBAND, "Full band", "fullband"},
|
2011-09-28 12:24:21 +00:00
|
|
|
{OPUS_AUTO, "Auto", "auto"},
|
2011-06-05 07:54:19 +00:00
|
|
|
{0, NULL, NULL}
|
|
|
|
};
|
|
|
|
static volatile GType id = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter ((gsize *) & id)) {
|
|
|
|
GType _id;
|
|
|
|
|
|
|
|
_id = g_enum_register_static ("GstOpusEncBandwidth", values);
|
|
|
|
|
|
|
|
g_once_init_leave ((gsize *) & id, _id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2011-11-16 19:40:20 +00:00
|
|
|
#define GST_OPUS_ENC_TYPE_FRAME_SIZE (gst_opus_enc_frame_size_get_type())
|
|
|
|
static GType
|
|
|
|
gst_opus_enc_frame_size_get_type (void)
|
|
|
|
{
|
|
|
|
static const GEnumValue values[] = {
|
|
|
|
{2, "2.5", "2.5"},
|
|
|
|
{5, "5", "5"},
|
|
|
|
{10, "10", "10"},
|
|
|
|
{20, "20", "20"},
|
|
|
|
{40, "40", "40"},
|
|
|
|
{60, "60", "60"},
|
|
|
|
{0, NULL, NULL}
|
|
|
|
};
|
|
|
|
static volatile GType id = 0;
|
2011-11-14 13:41:58 +00:00
|
|
|
|
2011-11-16 19:40:20 +00:00
|
|
|
if (g_once_init_enter ((gsize *) & id)) {
|
|
|
|
GType _id;
|
|
|
|
|
|
|
|
_id = g_enum_register_static ("GstOpusEncFrameSize", values);
|
|
|
|
|
|
|
|
g_once_init_leave ((gsize *) & id, _id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:43:59 +00:00
|
|
|
#define GST_OPUS_ENC_TYPE_AUDIO_TYPE (gst_opus_enc_audio_type_get_type())
|
|
|
|
static GType
|
|
|
|
gst_opus_enc_audio_type_get_type (void)
|
|
|
|
{
|
|
|
|
static const GEnumValue values[] = {
|
|
|
|
{OPUS_APPLICATION_AUDIO, "Generic audio", "generic"},
|
|
|
|
{OPUS_APPLICATION_VOIP, "Voice", "voice"},
|
|
|
|
{0, NULL, NULL}
|
|
|
|
};
|
|
|
|
static volatile GType id = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter ((gsize *) & id)) {
|
|
|
|
GType _id;
|
|
|
|
|
|
|
|
_id = g_enum_register_static ("GstOpusEncAudioType", values);
|
|
|
|
|
|
|
|
g_once_init_leave ((gsize *) & id, _id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2015-03-12 12:49:40 +00:00
|
|
|
#define GST_OPUS_ENC_TYPE_BITRATE_TYPE (gst_opus_enc_bitrate_type_get_type())
|
|
|
|
static GType
|
|
|
|
gst_opus_enc_bitrate_type_get_type (void)
|
|
|
|
{
|
|
|
|
static const GEnumValue values[] = {
|
|
|
|
{BITRATE_TYPE_CBR, "CBR", "cbr"},
|
|
|
|
{BITRATE_TYPE_VBR, "VBR", "vbr"},
|
|
|
|
{BITRATE_TYPE_CONSTRAINED_VBR, "Constrained VBR", "constrained-vbr"},
|
|
|
|
{0, NULL, NULL}
|
|
|
|
};
|
|
|
|
static volatile GType id = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter ((gsize *) & id)) {
|
|
|
|
GType _id;
|
|
|
|
|
|
|
|
_id = g_enum_register_static ("GstOpusEncBitrateType", values);
|
|
|
|
|
|
|
|
g_once_init_leave ((gsize *) & id, _id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2011-11-23 10:08:39 +00:00
|
|
|
#define FORMAT_STR GST_AUDIO_NE(S16)
|
2011-06-05 07:54:19 +00:00
|
|
|
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-11-11 17:46:41 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw, "
|
2011-11-14 13:41:58 +00:00
|
|
|
"format = (string) " FORMAT_STR ", "
|
2012-03-07 16:14:29 +00:00
|
|
|
"layout = (string) interleaved, "
|
2015-10-30 18:59:41 +00:00
|
|
|
"rate = (int) 48000, "
|
2015-11-03 12:50:53 +00:00
|
|
|
"channels = (int) [ 1, 8 ]; "
|
2015-10-30 18:59:41 +00:00
|
|
|
"audio/x-raw, "
|
|
|
|
"format = (string) " FORMAT_STR ", "
|
|
|
|
"layout = (string) interleaved, "
|
|
|
|
"rate = (int) { 8000, 12000, 16000, 24000 }, "
|
2015-11-03 12:50:53 +00:00
|
|
|
"channels = (int) [ 1, 8 ] ")
|
2011-06-05 07:54:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-11-16 18:49:03 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-opus")
|
2011-06-05 07:54:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
#define DEFAULT_AUDIO TRUE
|
2015-01-28 16:43:59 +00:00
|
|
|
#define DEFAULT_AUDIO_TYPE OPUS_APPLICATION_AUDIO
|
2011-06-05 07:54:19 +00:00
|
|
|
#define DEFAULT_BITRATE 64000
|
|
|
|
#define DEFAULT_BANDWIDTH OPUS_BANDWIDTH_FULLBAND
|
|
|
|
#define DEFAULT_FRAMESIZE 20
|
|
|
|
#define DEFAULT_CBR TRUE
|
|
|
|
#define DEFAULT_CONSTRAINED_VBR TRUE
|
2015-03-12 12:49:40 +00:00
|
|
|
#define DEFAULT_BITRATE_TYPE BITRATE_TYPE_CBR
|
2011-06-05 07:54:19 +00:00
|
|
|
#define DEFAULT_COMPLEXITY 10
|
|
|
|
#define DEFAULT_INBAND_FEC FALSE
|
|
|
|
#define DEFAULT_DTX FALSE
|
|
|
|
#define DEFAULT_PACKET_LOSS_PERCENT 0
|
2013-12-27 14:29:46 +00:00
|
|
|
#define DEFAULT_MAX_PAYLOAD_SIZE 4000
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_AUDIO,
|
2015-01-28 16:43:59 +00:00
|
|
|
PROP_AUDIO_TYPE,
|
2011-06-05 07:54:19 +00:00
|
|
|
PROP_BITRATE,
|
|
|
|
PROP_BANDWIDTH,
|
|
|
|
PROP_FRAME_SIZE,
|
|
|
|
PROP_CBR,
|
|
|
|
PROP_CONSTRAINED_VBR,
|
2015-03-12 12:49:40 +00:00
|
|
|
PROP_BITRATE_TYPE,
|
2011-06-05 07:54:19 +00:00
|
|
|
PROP_COMPLEXITY,
|
|
|
|
PROP_INBAND_FEC,
|
|
|
|
PROP_DTX,
|
2011-11-22 15:33:20 +00:00
|
|
|
PROP_PACKET_LOSS_PERCENT,
|
|
|
|
PROP_MAX_PAYLOAD_SIZE
|
2011-06-05 07:54:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void gst_opus_enc_finalize (GObject * object);
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
static gboolean gst_opus_enc_sink_event (GstAudioEncoder * benc,
|
|
|
|
GstEvent * event);
|
2011-12-30 10:41:17 +00:00
|
|
|
static GstCaps *gst_opus_enc_sink_getcaps (GstAudioEncoder * benc,
|
|
|
|
GstCaps * filter);
|
2011-06-05 07:54:19 +00:00
|
|
|
static gboolean gst_opus_enc_setup (GstOpusEnc * enc);
|
|
|
|
|
|
|
|
static void gst_opus_enc_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_opus_enc_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
|
2015-06-15 11:43:53 +00:00
|
|
|
static void gst_opus_enc_set_tags (GstOpusEnc * enc);
|
2011-11-14 13:41:58 +00:00
|
|
|
static gboolean gst_opus_enc_start (GstAudioEncoder * benc);
|
|
|
|
static gboolean gst_opus_enc_stop (GstAudioEncoder * benc);
|
|
|
|
static gboolean gst_opus_enc_set_format (GstAudioEncoder * benc,
|
|
|
|
GstAudioInfo * info);
|
|
|
|
static GstFlowReturn gst_opus_enc_handle_frame (GstAudioEncoder * benc,
|
|
|
|
GstBuffer * buf);
|
2011-11-16 16:56:43 +00:00
|
|
|
static gint64 gst_opus_enc_get_latency (GstOpusEnc * enc);
|
|
|
|
|
|
|
|
static GstFlowReturn gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buffer);
|
2011-11-14 13:41:58 +00:00
|
|
|
|
2011-11-11 17:46:41 +00:00
|
|
|
#define gst_opus_enc_parent_class parent_class
|
2011-11-14 13:41:58 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstOpusEnc, gst_opus_enc, GST_TYPE_AUDIO_ENCODER,
|
2011-11-11 17:46:41 +00:00
|
|
|
G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL);
|
|
|
|
G_IMPLEMENT_INTERFACE (GST_TYPE_PRESET, NULL));
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2015-06-15 11:43:53 +00:00
|
|
|
static void
|
|
|
|
gst_opus_enc_set_tags (GstOpusEnc * enc)
|
|
|
|
{
|
|
|
|
GstTagList *taglist;
|
|
|
|
|
|
|
|
/* create a taglist and add a bitrate tag to it */
|
|
|
|
taglist = gst_tag_list_new_empty ();
|
|
|
|
gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
|
|
|
|
GST_TAG_BITRATE, enc->bitrate, NULL);
|
|
|
|
|
|
|
|
gst_audio_encoder_merge_tags (GST_AUDIO_ENCODER (enc), taglist,
|
|
|
|
GST_TAG_MERGE_REPLACE);
|
|
|
|
|
|
|
|
gst_tag_list_unref (taglist);
|
|
|
|
}
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
static void
|
2011-11-11 17:46:41 +00:00
|
|
|
gst_opus_enc_class_init (GstOpusEncClass * klass)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
2011-11-11 17:46:41 +00:00
|
|
|
GObjectClass *gobject_class;
|
2011-11-14 13:41:58 +00:00
|
|
|
GstAudioEncoderClass *base_class;
|
2011-12-15 16:42:20 +00:00
|
|
|
GstElementClass *gstelement_class;
|
2011-11-11 17:46:41 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-11-14 13:41:58 +00:00
|
|
|
base_class = (GstAudioEncoderClass *) klass;
|
2011-12-15 16:42:20 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
gobject_class->set_property = gst_opus_enc_set_property;
|
|
|
|
gobject_class->get_property = gst_opus_enc_get_property;
|
|
|
|
|
2011-11-25 11:48:58 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
2011-06-05 07:54:19 +00:00
|
|
|
gst_static_pad_template_get (&src_factory));
|
2011-11-25 11:48:58 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
2011-06-05 07:54:19 +00:00
|
|
|
gst_static_pad_template_get (&sink_factory));
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class, "Opus audio encoder",
|
2011-06-05 07:54:19 +00:00
|
|
|
"Codec/Encoder/Audio",
|
|
|
|
"Encodes audio in Opus format",
|
2012-06-15 09:24:24 +00:00
|
|
|
"Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>");
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
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);
|
2012-03-30 10:22:48 +00:00
|
|
|
base_class->sink_event = GST_DEBUG_FUNCPTR (gst_opus_enc_sink_event);
|
2011-12-09 17:25:41 +00:00
|
|
|
base_class->getcaps = GST_DEBUG_FUNCPTR (gst_opus_enc_sink_getcaps);
|
2011-11-14 13:41:58 +00:00
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_AUDIO,
|
2015-01-28 16:43:59 +00:00
|
|
|
g_param_spec_boolean ("audio",
|
2015-09-11 09:22:35 +00:00
|
|
|
"Audio or voice",
|
|
|
|
"Audio or voice (DEPRECATED: use audio-type)", DEFAULT_AUDIO,
|
2015-01-28 16:43:59 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
|
|
|
|
g_object_class_install_property (gobject_class, PROP_AUDIO_TYPE,
|
|
|
|
g_param_spec_enum ("audio-type", "What type of audio to optimize for",
|
|
|
|
"What type of audio to optimize for", GST_OPUS_ENC_TYPE_AUDIO_TYPE,
|
|
|
|
DEFAULT_AUDIO_TYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BITRATE,
|
|
|
|
g_param_spec_int ("bitrate", "Encoding Bit-rate",
|
2015-01-28 16:43:59 +00:00
|
|
|
"Specify an encoding bit-rate (in bps).", LOWEST_BITRATE,
|
|
|
|
HIGHEST_BITRATE, DEFAULT_BITRATE,
|
2011-11-22 20:27:50 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_BANDWIDTH,
|
2011-11-22 20:27:50 +00:00
|
|
|
g_param_spec_enum ("bandwidth", "Band Width", "Audio Band Width",
|
|
|
|
GST_OPUS_ENC_TYPE_BANDWIDTH, DEFAULT_BANDWIDTH,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_FRAME_SIZE,
|
2011-11-16 19:40:20 +00:00
|
|
|
g_param_spec_enum ("frame-size", "Frame Size",
|
2011-11-22 20:27:50 +00:00
|
|
|
"The duration of an audio frame, in ms", GST_OPUS_ENC_TYPE_FRAME_SIZE,
|
|
|
|
DEFAULT_FRAMESIZE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_CBR,
|
2015-09-11 09:22:35 +00:00
|
|
|
g_param_spec_boolean ("cbr", "Constant bit rate",
|
|
|
|
"Constant bit rate (DEPRECATED: use bitrate-type)", DEFAULT_CBR,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_PLAYING
|
|
|
|
| G_PARAM_DEPRECATED));
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_CONSTRAINED_VBR,
|
2011-11-16 18:43:53 +00:00
|
|
|
g_param_spec_boolean ("constrained-vbr", "Constrained VBR",
|
2015-09-11 09:22:35 +00:00
|
|
|
"Constrained VBR (DEPRECATED: use bitrate-type)",
|
|
|
|
DEFAULT_CONSTRAINED_VBR,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_PLAYING
|
|
|
|
| G_PARAM_DEPRECATED));
|
2015-03-12 12:49:40 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_BITRATE_TYPE,
|
2015-09-11 09:22:35 +00:00
|
|
|
g_param_spec_enum ("bitrate-type", "Bitrate type", "Bitrate type",
|
|
|
|
GST_OPUS_ENC_TYPE_BITRATE_TYPE, DEFAULT_BITRATE_TYPE,
|
2015-03-12 12:49:40 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
2011-11-22 20:27:50 +00:00
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_COMPLEXITY,
|
2011-11-22 20:27:50 +00:00
|
|
|
g_param_spec_int ("complexity", "Complexity", "Complexity", 0, 10,
|
|
|
|
DEFAULT_COMPLEXITY,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_INBAND_FEC,
|
|
|
|
g_param_spec_boolean ("inband-fec", "In-band FEC",
|
|
|
|
"Enable forward error correction", DEFAULT_INBAND_FEC,
|
2011-11-22 20:27:50 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_DTX,
|
2011-11-22 20:27:50 +00:00
|
|
|
g_param_spec_boolean ("dtx", "DTX", "DTX", DEFAULT_DTX,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
2011-06-05 07:54:19 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass),
|
|
|
|
PROP_PACKET_LOSS_PERCENT, g_param_spec_int ("packet-loss-percentage",
|
|
|
|
"Loss percentage", "Packet loss percentage", 0, 100,
|
|
|
|
DEFAULT_PACKET_LOSS_PERCENT,
|
2011-11-22 20:27:50 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
2011-11-22 15:33:20 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass),
|
|
|
|
PROP_MAX_PAYLOAD_SIZE, g_param_spec_uint ("max-payload-size",
|
2013-12-27 14:29:46 +00:00
|
|
|
"Max payload size", "Maximum payload size in bytes", 2, 4000,
|
2011-11-22 15:33:20 +00:00
|
|
|
DEFAULT_MAX_PAYLOAD_SIZE,
|
2011-11-22 20:27:50 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_opus_enc_finalize);
|
|
|
|
|
2011-11-11 17:46:41 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (opusenc_debug, "opusenc", 0, "Opus encoder");
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opus_enc_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstOpusEnc *enc;
|
|
|
|
|
|
|
|
enc = GST_OPUS_ENC (object);
|
|
|
|
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_clear (&enc->property_lock);
|
2011-11-22 17:04:09 +00:00
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
static void
|
2011-11-25 11:48:58 +00:00
|
|
|
gst_opus_enc_init (GstOpusEnc * enc)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
2011-11-14 13:41:58 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "init");
|
|
|
|
|
2015-08-16 10:18:34 +00:00
|
|
|
GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (enc));
|
|
|
|
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_init (&enc->property_lock);
|
2011-11-22 17:04:09 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
enc->n_channels = -1;
|
|
|
|
enc->sample_rate = -1;
|
|
|
|
enc->frame_samples = 0;
|
|
|
|
|
|
|
|
enc->bitrate = DEFAULT_BITRATE;
|
|
|
|
enc->bandwidth = DEFAULT_BANDWIDTH;
|
2011-06-05 07:54:19 +00:00
|
|
|
enc->frame_size = DEFAULT_FRAMESIZE;
|
2015-03-12 12:49:40 +00:00
|
|
|
enc->bitrate_type = DEFAULT_BITRATE_TYPE;
|
2011-11-14 13:41:58 +00:00
|
|
|
enc->complexity = DEFAULT_COMPLEXITY;
|
|
|
|
enc->inband_fec = DEFAULT_INBAND_FEC;
|
|
|
|
enc->dtx = DEFAULT_DTX;
|
|
|
|
enc->packet_loss_percentage = DEFAULT_PACKET_LOSS_PERCENT;
|
2011-11-22 15:33:20 +00:00
|
|
|
enc->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
|
2015-01-28 16:43:59 +00:00
|
|
|
enc->audio_type = DEFAULT_AUDIO_TYPE;
|
2011-11-14 13:41:58 +00:00
|
|
|
}
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
static gboolean
|
|
|
|
gst_opus_enc_start (GstAudioEncoder * benc)
|
|
|
|
{
|
|
|
|
GstOpusEnc *enc = GST_OPUS_ENC (benc);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (enc, "start");
|
2014-06-10 08:33:40 +00:00
|
|
|
enc->encoded_samples = 0;
|
2015-10-30 18:57:37 +00:00
|
|
|
enc->consumed_samples = 0;
|
2011-11-22 17:04:09 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_opus_enc_stop (GstAudioEncoder * benc)
|
|
|
|
{
|
|
|
|
GstOpusEnc *enc = GST_OPUS_ENC (benc);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (enc, "stop");
|
|
|
|
if (enc->state) {
|
2011-11-23 17:49:58 +00:00
|
|
|
opus_multistream_encoder_destroy (enc->state);
|
2011-11-14 13:41:58 +00:00
|
|
|
enc->state = NULL;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
2011-11-21 12:02:28 +00:00
|
|
|
gst_tag_setter_reset_tags (GST_TAG_SETTER (enc));
|
2011-11-14 13:41:58 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
static gint64
|
|
|
|
gst_opus_enc_get_latency (GstOpusEnc * enc)
|
|
|
|
{
|
2011-11-16 16:56:43 +00:00
|
|
|
gint64 latency = gst_util_uint64_scale (enc->frame_samples, GST_SECOND,
|
2011-06-05 07:54:19 +00:00
|
|
|
enc->sample_rate);
|
2011-11-16 16:56:43 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "Latency: %" GST_TIME_FORMAT, GST_TIME_ARGS (latency));
|
|
|
|
return latency;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-22 17:04:09 +00:00
|
|
|
static void
|
|
|
|
gst_opus_enc_setup_base_class (GstOpusEnc * enc, GstAudioEncoder * benc)
|
|
|
|
{
|
|
|
|
gst_audio_encoder_set_latency (benc,
|
|
|
|
gst_opus_enc_get_latency (enc), gst_opus_enc_get_latency (enc));
|
2012-03-07 11:55:43 +00:00
|
|
|
gst_audio_encoder_set_frame_samples_min (benc, enc->frame_samples);
|
|
|
|
gst_audio_encoder_set_frame_samples_max (benc, enc->frame_samples);
|
2012-03-07 11:59:28 +00:00
|
|
|
gst_audio_encoder_set_frame_max (benc, 1);
|
2011-11-22 17:04:09 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
static gint
|
|
|
|
gst_opus_enc_get_frame_samples (GstOpusEnc * enc)
|
|
|
|
{
|
|
|
|
gint frame_samples = 0;
|
2011-06-05 07:54:19 +00:00
|
|
|
switch (enc->frame_size) {
|
|
|
|
case 2:
|
2011-11-14 13:41:58 +00:00
|
|
|
frame_samples = enc->sample_rate / 400;
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2011-11-14 13:41:58 +00:00
|
|
|
frame_samples = enc->sample_rate / 200;
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case 10:
|
2011-11-14 13:41:58 +00:00
|
|
|
frame_samples = enc->sample_rate / 100;
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case 20:
|
2011-11-14 13:41:58 +00:00
|
|
|
frame_samples = enc->sample_rate / 50;
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case 40:
|
2011-11-14 13:41:58 +00:00
|
|
|
frame_samples = enc->sample_rate / 25;
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case 60:
|
2011-11-14 13:41:58 +00:00
|
|
|
frame_samples = 3 * enc->sample_rate / 50;
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-09-28 12:25:21 +00:00
|
|
|
GST_WARNING_OBJECT (enc, "Unsupported frame size: %d", enc->frame_size);
|
2011-11-14 13:41:58 +00:00
|
|
|
frame_samples = 0;
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-11-14 13:41:58 +00:00
|
|
|
return frame_samples;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
static void
|
2011-12-08 18:45:27 +00:00
|
|
|
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)
|
2011-11-24 13:29:56 +00:00
|
|
|
{
|
|
|
|
#define MAPS(idx,pos) (GST_AUDIO_INFO_POSITION (info, (idx)) == GST_AUDIO_CHANNEL_POSITION_##pos)
|
|
|
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (enc, "Setting up channel mapping for %d channels",
|
|
|
|
enc->n_channels);
|
|
|
|
|
|
|
|
/* Start by setting up a default trivial mapping */
|
2011-12-08 18:45:27 +00:00
|
|
|
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);
|
2011-11-24 13:29:56 +00:00
|
|
|
|
|
|
|
/* 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;
|
2011-12-08 18:45:27 +00:00
|
|
|
/* implicit mapping for family 0 */
|
2011-11-24 13:29:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For two channels, use the basic RTP mapping if the channels are
|
|
|
|
mapped as left/right. */
|
|
|
|
if (enc->n_channels == 2) {
|
2015-11-03 12:50:53 +00:00
|
|
|
GST_INFO_OBJECT (enc, "Stereo, trivial RTP mapping");
|
|
|
|
enc->channel_mapping_family = 0;
|
|
|
|
enc->n_stereo_streams = 1;
|
|
|
|
/* implicit mapping for family 0 */
|
|
|
|
return;
|
2011-11-24 13:29:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:50:53 +00:00
|
|
|
/* For channels between 3 and 8, we use the Vorbis mapping if we can
|
|
|
|
find a permutation that matches it. Mono and stereo will have been taken
|
|
|
|
care of earlier, but this code also handles it. 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 >= 3 && enc->n_channels <= 8) {
|
2011-12-08 18:45:27 +00:00
|
|
|
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},
|
2015-11-03 12:50:53 +00:00
|
|
|
{GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
|
|
|
|
GST_AUDIO_CHANNEL_POSITION_REAR_CENTER},
|
2011-12-08 18:45:27 +00:00
|
|
|
};
|
|
|
|
size_t pair;
|
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
GST_DEBUG_OBJECT (enc,
|
2011-12-08 18:45:27 +00:00
|
|
|
"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;
|
2011-11-24 13:29:56 +00:00
|
|
|
}
|
2011-12-08 18:45:27 +00:00
|
|
|
|
|
|
|
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;
|
2011-11-24 13:29:56 +00:00
|
|
|
}
|
2011-12-08 18:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2015-11-03 12:50:53 +00:00
|
|
|
if (cv < 0)
|
|
|
|
g_assert_not_reached ();
|
2011-12-08 18:45:27 +00:00
|
|
|
enc->encoding_channel_mapping[mapped] = n;
|
|
|
|
enc->decoding_channel_mapping[cv] = mapped;
|
|
|
|
mapped++;
|
2011-11-24 13:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-08 18:45:27 +00:00
|
|
|
|
2012-09-12 07:10:35 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2011-12-08 18:45:27 +00:00
|
|
|
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
|
|
|
|
|
2011-11-24 13:29:56 +00:00
|
|
|
enc->channel_mapping_family = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-08 18:45:27 +00:00
|
|
|
/* 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 */
|
2011-11-24 13:29:56 +00:00
|
|
|
GST_WARNING_OBJECT (enc, "Unknown mapping");
|
|
|
|
enc->channel_mapping_family = 255;
|
2011-12-08 18:45:27 +00:00
|
|
|
enc->n_stereo_streams = 0;
|
2011-11-24 13:29:56 +00:00
|
|
|
|
|
|
|
#undef MAPS
|
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
static gboolean
|
|
|
|
gst_opus_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
2011-11-14 13:41:58 +00:00
|
|
|
GstOpusEnc *enc;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
enc = GST_OPUS_ENC (benc);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_lock (&enc->property_lock);
|
2011-11-22 17:04:09 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
enc->n_channels = GST_AUDIO_INFO_CHANNELS (info);
|
|
|
|
enc->sample_rate = GST_AUDIO_INFO_RATE (info);
|
2011-12-08 18:45:27 +00:00
|
|
|
gst_opus_enc_setup_channel_mappings (enc, info);
|
2011-11-14 13:41:58 +00:00
|
|
|
GST_DEBUG_OBJECT (benc, "Setup with %d channels, %d Hz", enc->n_channels,
|
|
|
|
enc->sample_rate);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
/* handle reconfigure */
|
|
|
|
if (enc->state) {
|
2011-11-23 17:49:58 +00:00
|
|
|
opus_multistream_encoder_destroy (enc->state);
|
2011-11-14 13:41:58 +00:00
|
|
|
enc->state = NULL;
|
2011-11-11 17:46:41 +00:00
|
|
|
}
|
2012-06-15 09:32:39 +00:00
|
|
|
if (!gst_opus_enc_setup (enc)) {
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_unlock (&enc->property_lock);
|
2011-06-05 07:54:19 +00:00
|
|
|
return FALSE;
|
2012-06-15 09:32:39 +00:00
|
|
|
}
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2015-06-15 11:43:53 +00:00
|
|
|
/* update the tags */
|
|
|
|
gst_opus_enc_set_tags (enc);
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
enc->frame_samples = gst_opus_enc_get_frame_samples (enc);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
/* feedback to base class */
|
2011-11-22 17:04:09 +00:00
|
|
|
gst_opus_enc_setup_base_class (enc, benc);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_unlock (&enc->property_lock);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-16 16:56:43 +00:00
|
|
|
return TRUE;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_opus_enc_setup (GstOpusEnc * enc)
|
|
|
|
{
|
2011-12-08 18:45:27 +00:00
|
|
|
int error = OPUS_OK;
|
2015-03-23 12:15:30 +00:00
|
|
|
GstCaps *caps;
|
|
|
|
gboolean ret;
|
2015-10-30 18:47:20 +00:00
|
|
|
gint32 lookahead;
|
2015-11-03 12:50:53 +00:00
|
|
|
const GstTagList *tags;
|
2015-11-03 22:12:22 +00:00
|
|
|
GstTagList *empty_tags = NULL;
|
2015-11-03 12:50:53 +00:00
|
|
|
GstBuffer *header, *comments;
|
2011-12-08 18:45:27 +00:00
|
|
|
|
2012-09-12 07:10:35 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2011-12-08 18:45:27 +00:00
|
|
|
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
|
2011-11-25 12:40:31 +00:00
|
|
|
|
2011-12-08 18:45:27 +00:00
|
|
|
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,
|
2015-01-28 16:43:59 +00:00
|
|
|
enc->audio_type, &error);
|
2011-09-28 12:24:21 +00:00
|
|
|
if (!enc->state || error != OPUS_OK)
|
2011-06-05 07:54:19 +00:00
|
|
|
goto encoder_creation_failed;
|
|
|
|
|
2011-11-23 17:49:58 +00:00
|
|
|
opus_multistream_encoder_ctl (enc->state, OPUS_SET_BITRATE (enc->bitrate), 0);
|
|
|
|
opus_multistream_encoder_ctl (enc->state, OPUS_SET_BANDWIDTH (enc->bandwidth),
|
2011-06-05 07:54:19 +00:00
|
|
|
0);
|
2011-11-23 17:49:58 +00:00
|
|
|
opus_multistream_encoder_ctl (enc->state,
|
2015-03-12 12:49:40 +00:00
|
|
|
OPUS_SET_VBR (enc->bitrate_type != BITRATE_TYPE_CBR), 0);
|
|
|
|
opus_multistream_encoder_ctl (enc->state,
|
|
|
|
OPUS_SET_VBR_CONSTRAINT (enc->bitrate_type ==
|
|
|
|
BITRATE_TYPE_CONSTRAINED_VBR), 0);
|
2011-11-23 17:49:58 +00:00
|
|
|
opus_multistream_encoder_ctl (enc->state,
|
|
|
|
OPUS_SET_COMPLEXITY (enc->complexity), 0);
|
|
|
|
opus_multistream_encoder_ctl (enc->state,
|
|
|
|
OPUS_SET_INBAND_FEC (enc->inband_fec), 0);
|
|
|
|
opus_multistream_encoder_ctl (enc->state, OPUS_SET_DTX (enc->dtx), 0);
|
|
|
|
opus_multistream_encoder_ctl (enc->state,
|
2011-06-05 07:54:19 +00:00
|
|
|
OPUS_SET_PACKET_LOSS_PERC (enc->packet_loss_percentage), 0);
|
|
|
|
|
2015-10-30 18:47:20 +00:00
|
|
|
opus_multistream_encoder_ctl (enc->state, OPUS_GET_LOOKAHEAD (&lookahead), 0);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2015-10-30 18:47:20 +00:00
|
|
|
GST_LOG_OBJECT (enc, "we have frame size %d, lookahead %d", enc->frame_size,
|
|
|
|
lookahead);
|
|
|
|
|
|
|
|
/* lookahead is samples, the Opus header wants it in 48kHz samples */
|
|
|
|
lookahead = lookahead * 48000 / enc->sample_rate;
|
2015-11-02 14:52:28 +00:00
|
|
|
enc->lookahead = enc->pending_lookahead = lookahead;
|
2015-10-30 18:47:20 +00:00
|
|
|
|
2015-11-03 12:50:53 +00:00
|
|
|
header = gst_codec_utils_opus_create_header (enc->sample_rate,
|
|
|
|
enc->n_channels, enc->channel_mapping_family,
|
|
|
|
enc->n_channels - enc->n_stereo_streams, enc->n_stereo_streams,
|
|
|
|
enc->decoding_channel_mapping, lookahead, 0);
|
|
|
|
tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (enc));
|
2015-11-03 22:12:22 +00:00
|
|
|
if (!tags)
|
|
|
|
tags = empty_tags = gst_tag_list_new_empty ();
|
2015-11-03 12:50:53 +00:00
|
|
|
comments =
|
|
|
|
gst_tag_list_to_vorbiscomment_buffer (tags, (const guint8 *) "OpusTags",
|
|
|
|
8, "Encoded with GStreamer opusenc");
|
|
|
|
caps = gst_codec_utils_opus_create_caps_from_header (header, comments);
|
2015-11-03 22:12:22 +00:00
|
|
|
if (empty_tags)
|
|
|
|
gst_tag_list_unref (empty_tags);
|
2015-11-03 12:50:53 +00:00
|
|
|
gst_buffer_unref (header);
|
|
|
|
gst_buffer_unref (comments);
|
2015-03-23 12:15:30 +00:00
|
|
|
|
|
|
|
/* negotiate with these caps */
|
|
|
|
GST_DEBUG_OBJECT (enc, "here are the caps: %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
ret = gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (enc), caps);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
|
|
|
return ret;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
encoder_creation_failed:
|
|
|
|
GST_ERROR_OBJECT (enc, "Encoder creation failed");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-14 13:41:58 +00:00
|
|
|
gst_opus_enc_sink_event (GstAudioEncoder * benc, GstEvent * event)
|
2011-06-05 07:54:19 +00:00
|
|
|
{
|
|
|
|
GstOpusEnc *enc;
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
enc = GST_OPUS_ENC (benc);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "sink event: %s", GST_EVENT_TYPE_NAME (event));
|
2011-06-05 07:54:19 +00:00
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_TAG:
|
|
|
|
{
|
|
|
|
GstTagList *list;
|
|
|
|
GstTagSetter *setter = GST_TAG_SETTER (enc);
|
|
|
|
const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
|
|
|
|
|
|
|
|
gst_event_parse_tag (event, &list);
|
|
|
|
gst_tag_setter_merge_tags (setter, list, mode);
|
2011-11-11 17:46:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-06-10 08:33:40 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
|
|
|
enc->encoded_samples = 0;
|
2015-10-30 18:57:37 +00:00
|
|
|
enc->consumed_samples = 0;
|
2014-06-10 08:33:40 +00:00
|
|
|
break;
|
2011-11-11 17:46:41 +00:00
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-03-30 10:22:48 +00:00
|
|
|
return GST_AUDIO_ENCODER_CLASS (parent_class)->sink_event (benc, event);
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:50:53 +00:00
|
|
|
static GstCaps *
|
|
|
|
gst_opus_enc_get_sink_template_caps (void)
|
|
|
|
{
|
|
|
|
static volatile gsize init = 0;
|
|
|
|
static GstCaps *caps = NULL;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&init)) {
|
|
|
|
GValue rate_array = G_VALUE_INIT;
|
|
|
|
GValue v = G_VALUE_INIT;
|
|
|
|
GstStructure *s1, *s2, *s;
|
|
|
|
gint i, c;
|
|
|
|
|
|
|
|
caps = gst_caps_new_empty ();
|
|
|
|
|
|
|
|
/* Generate our two template structures */
|
|
|
|
g_value_init (&rate_array, GST_TYPE_LIST);
|
|
|
|
g_value_init (&v, G_TYPE_INT);
|
|
|
|
g_value_set_int (&v, 8000);
|
|
|
|
gst_value_list_append_value (&rate_array, &v);
|
|
|
|
g_value_set_int (&v, 12000);
|
|
|
|
gst_value_list_append_value (&rate_array, &v);
|
|
|
|
g_value_set_int (&v, 16000);
|
|
|
|
gst_value_list_append_value (&rate_array, &v);
|
|
|
|
g_value_set_int (&v, 24000);
|
|
|
|
gst_value_list_append_value (&rate_array, &v);
|
|
|
|
|
|
|
|
s1 = gst_structure_new ("audio/x-raw",
|
|
|
|
"format", G_TYPE_STRING, GST_AUDIO_NE (S16),
|
|
|
|
"layout", G_TYPE_STRING, "interleaved",
|
|
|
|
"rate", G_TYPE_INT, 48000, NULL);
|
|
|
|
s2 = gst_structure_new ("audio/x-raw",
|
|
|
|
"format", G_TYPE_STRING, GST_AUDIO_NE (S16),
|
|
|
|
"layout", G_TYPE_STRING, "interleaved", NULL);
|
|
|
|
gst_structure_set_value (s2, "rate", &rate_array);
|
|
|
|
g_value_unset (&rate_array);
|
|
|
|
g_value_unset (&v);
|
|
|
|
|
|
|
|
/* Mono */
|
|
|
|
s = gst_structure_copy (s1);
|
|
|
|
gst_structure_set (s, "channels", G_TYPE_INT, 1, NULL);
|
|
|
|
gst_caps_append_structure (caps, s);
|
|
|
|
|
|
|
|
s = gst_structure_copy (s2);
|
|
|
|
gst_structure_set (s, "channels", G_TYPE_INT, 1, NULL);
|
|
|
|
gst_caps_append_structure (caps, s);
|
|
|
|
|
|
|
|
/* Stereo and further */
|
|
|
|
for (i = 2; i <= 8; i++) {
|
|
|
|
guint64 channel_mask = 0;
|
|
|
|
const GstAudioChannelPosition *pos = gst_opus_channel_positions[i - 1];
|
|
|
|
|
|
|
|
for (c = 0; c < i; c++) {
|
|
|
|
channel_mask |= G_GUINT64_CONSTANT (1) << pos[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
s = gst_structure_copy (s1);
|
|
|
|
gst_structure_set (s, "channels", G_TYPE_INT, i, "channel-mask",
|
|
|
|
GST_TYPE_BITMASK, channel_mask, NULL);
|
|
|
|
gst_caps_append_structure (caps, s);
|
|
|
|
|
|
|
|
s = gst_structure_copy (s2);
|
|
|
|
gst_structure_set (s, "channels", G_TYPE_INT, i, "channel-mask",
|
|
|
|
GST_TYPE_BITMASK, channel_mask, NULL);
|
|
|
|
gst_caps_append_structure (caps, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_structure_free (s1);
|
|
|
|
gst_structure_free (s2);
|
|
|
|
|
|
|
|
g_once_init_leave (&init, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2011-12-09 17:25:41 +00:00
|
|
|
static GstCaps *
|
2011-12-30 10:41:17 +00:00
|
|
|
gst_opus_enc_sink_getcaps (GstAudioEncoder * benc, GstCaps * filter)
|
2011-12-09 17:25:41 +00:00
|
|
|
{
|
|
|
|
GstOpusEnc *enc;
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
enc = GST_OPUS_ENC (benc);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (enc, "sink getcaps");
|
|
|
|
|
2015-11-03 12:50:53 +00:00
|
|
|
caps = gst_opus_enc_get_sink_template_caps ();
|
|
|
|
caps = gst_audio_encoder_proxy_getcaps (benc, caps, filter);
|
2011-12-30 10:41:17 +00:00
|
|
|
|
2011-12-09 17:25:41 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "Returning caps: %" GST_PTR_FORMAT, caps);
|
2015-11-03 12:50:53 +00:00
|
|
|
|
2011-12-09 17:25:41 +00:00
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2011-11-16 16:56:43 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buf)
|
|
|
|
{
|
2011-11-25 11:48:58 +00:00
|
|
|
guint8 *bdata = NULL, *data, *mdata = NULL;
|
2011-11-16 16:56:43 +00:00
|
|
|
gsize bsize, size;
|
2014-05-10 16:32:28 +00:00
|
|
|
gsize bytes;
|
2011-11-16 16:56:43 +00:00
|
|
|
gint ret = GST_FLOW_OK;
|
2012-03-07 16:14:29 +00:00
|
|
|
GstMapInfo map;
|
2012-03-29 15:41:53 +00:00
|
|
|
GstMapInfo omap;
|
2012-03-07 11:59:28 +00:00
|
|
|
gint outsize;
|
|
|
|
GstBuffer *outbuf;
|
2014-06-10 08:33:40 +00:00
|
|
|
GstSegment *segment;
|
|
|
|
GstClockTime duration;
|
2015-11-02 14:52:28 +00:00
|
|
|
guint64 trim_start = 0, trim_end = 0;
|
2011-11-16 16:56:43 +00:00
|
|
|
|
2014-05-10 16:32:28 +00:00
|
|
|
guint max_payload_size;
|
2015-10-30 18:57:37 +00:00
|
|
|
gint frame_samples, input_samples, output_samples;
|
2014-05-10 16:32:28 +00:00
|
|
|
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_lock (&enc->property_lock);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2014-05-10 16:32:28 +00:00
|
|
|
bytes = enc->frame_samples * enc->n_channels * 2;
|
|
|
|
max_payload_size = enc->max_payload_size;
|
2015-10-30 18:57:37 +00:00
|
|
|
frame_samples = input_samples = enc->frame_samples;
|
2014-05-10 16:32:28 +00:00
|
|
|
|
|
|
|
g_mutex_unlock (&enc->property_lock);
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
if (G_LIKELY (buf)) {
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_READ);
|
|
|
|
bdata = map.data;
|
|
|
|
bsize = map.size;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
if (G_UNLIKELY (bsize % bytes)) {
|
2015-10-30 18:57:37 +00:00
|
|
|
gint64 diff;
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "draining; adding silence samples");
|
2015-10-30 18:57:37 +00:00
|
|
|
g_assert (bsize < bytes);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2014-06-10 08:33:40 +00:00
|
|
|
/* If encoding part of a frame, and we have no set stop time on
|
|
|
|
* the output segment, we update the segment stop time to reflect
|
|
|
|
* the last sample. This will let oggmux set the last page's
|
|
|
|
* granpos to tell a decoder the dummy samples should be clipped.
|
|
|
|
*/
|
2015-10-30 18:57:37 +00:00
|
|
|
input_samples = bsize / (enc->n_channels * 2);
|
2014-06-10 08:33:40 +00:00
|
|
|
segment = &GST_AUDIO_ENCODER_OUTPUT_SEGMENT (enc);
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (segment->stop)) {
|
|
|
|
GST_DEBUG_OBJECT (enc,
|
|
|
|
"No stop time and partial frame, updating segment");
|
|
|
|
duration =
|
2015-10-30 18:57:37 +00:00
|
|
|
gst_util_uint64_scale_ceil (enc->consumed_samples + input_samples,
|
2014-06-10 08:33:40 +00:00
|
|
|
GST_SECOND, enc->sample_rate);
|
|
|
|
segment->stop = segment->start + duration;
|
|
|
|
GST_DEBUG_OBJECT (enc, "new output segment %" GST_SEGMENT_FORMAT,
|
|
|
|
segment);
|
|
|
|
gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (enc),
|
|
|
|
gst_event_new_segment (segment));
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:57:37 +00:00
|
|
|
diff =
|
|
|
|
(enc->encoded_samples + frame_samples) - (enc->consumed_samples +
|
|
|
|
input_samples);
|
|
|
|
if (diff >= 0) {
|
|
|
|
GST_DEBUG_OBJECT (enc,
|
|
|
|
"%" G_GINT64_FORMAT " extra samples of padding in this frame",
|
|
|
|
diff);
|
|
|
|
output_samples = frame_samples - diff;
|
2015-11-02 14:52:28 +00:00
|
|
|
trim_end = diff * 48000 / enc->sample_rate;
|
2015-10-30 18:57:37 +00:00
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (enc,
|
|
|
|
"Need to add %" G_GINT64_FORMAT " extra samples in the next frame",
|
|
|
|
-diff);
|
|
|
|
output_samples = frame_samples;
|
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
size = ((bsize / bytes) + 1) * bytes;
|
|
|
|
mdata = g_malloc0 (size);
|
2015-10-31 13:02:50 +00:00
|
|
|
/* FIXME: Instead of silence, use LPC with the last real samples.
|
|
|
|
* Otherwise we will create a discontinuity here, which will distort the
|
|
|
|
* last few encoded samples
|
|
|
|
*/
|
2011-11-15 17:49:48 +00:00
|
|
|
memcpy (mdata, bdata, bsize);
|
2011-11-14 13:41:58 +00:00
|
|
|
data = mdata;
|
|
|
|
} else {
|
|
|
|
data = bdata;
|
|
|
|
size = bsize;
|
2015-10-30 18:57:37 +00:00
|
|
|
|
|
|
|
/* Adjust for lookahead here */
|
|
|
|
if (enc->pending_lookahead) {
|
2015-11-02 14:52:28 +00:00
|
|
|
guint scaled_lookahead =
|
|
|
|
enc->pending_lookahead * enc->sample_rate / 48000;
|
|
|
|
|
|
|
|
if (input_samples > scaled_lookahead) {
|
|
|
|
output_samples = input_samples - scaled_lookahead;
|
|
|
|
trim_start = enc->pending_lookahead;
|
2015-10-30 18:57:37 +00:00
|
|
|
enc->pending_lookahead = 0;
|
|
|
|
} else {
|
2015-11-02 14:52:28 +00:00
|
|
|
trim_start = input_samples * 48000 / enc->sample_rate;
|
|
|
|
enc->pending_lookahead -= trim_start;
|
2015-10-30 18:57:37 +00:00
|
|
|
output_samples = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output_samples = input_samples;
|
|
|
|
}
|
2011-11-14 13:41:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-30 18:57:37 +00:00
|
|
|
if (enc->encoded_samples < enc->consumed_samples) {
|
2015-10-31 13:02:50 +00:00
|
|
|
/* FIXME: Instead of silence, use LPC with the last real samples.
|
|
|
|
* Otherwise we will create a discontinuity here, which will distort the
|
|
|
|
* last few encoded samples
|
|
|
|
*/
|
2015-10-30 18:57:37 +00:00
|
|
|
data = mdata = g_malloc0 (bytes);
|
|
|
|
size = bytes;
|
|
|
|
output_samples = enc->consumed_samples - enc->encoded_samples;
|
|
|
|
input_samples = 0;
|
|
|
|
GST_DEBUG_OBJECT (enc, "draining %d samples", output_samples);
|
2015-11-02 14:52:28 +00:00
|
|
|
trim_end = (frame_samples - output_samples) * 48000 / enc->sample_rate;
|
2015-10-30 18:57:37 +00:00
|
|
|
} else if (enc->encoded_samples == enc->consumed_samples) {
|
|
|
|
GST_DEBUG_OBJECT (enc, "nothing to drain");
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
g_assert_not_reached ();
|
|
|
|
goto done;
|
|
|
|
}
|
2011-11-14 13:41:58 +00:00
|
|
|
}
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2012-03-07 11:59:28 +00:00
|
|
|
g_assert (size == bytes);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2015-04-28 14:58:21 +00:00
|
|
|
outbuf =
|
|
|
|
gst_audio_encoder_allocate_output_buffer (GST_AUDIO_ENCODER (enc),
|
|
|
|
max_payload_size * enc->n_channels);
|
2012-03-29 15:41:53 +00:00
|
|
|
if (!outbuf)
|
2012-03-07 11:59:28 +00:00
|
|
|
goto done;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2012-03-29 15:41:53 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)",
|
2014-05-10 16:32:28 +00:00
|
|
|
frame_samples, (int) bytes);
|
2012-03-29 15:41:53 +00:00
|
|
|
|
2015-11-02 14:52:28 +00:00
|
|
|
if (trim_start || trim_end) {
|
|
|
|
GST_DEBUG_OBJECT (enc,
|
|
|
|
"Adding trim-start %" G_GUINT64_FORMAT " trim-end %" G_GUINT64_FORMAT,
|
|
|
|
trim_start, trim_end);
|
|
|
|
gst_buffer_add_audio_clipping_meta (outbuf, GST_FORMAT_DEFAULT, trim_start,
|
|
|
|
trim_end);
|
|
|
|
}
|
|
|
|
|
2012-03-29 15:41:53 +00:00
|
|
|
gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2012-03-07 11:59:28 +00:00
|
|
|
outsize =
|
|
|
|
opus_multistream_encode (enc->state, (const gint16 *) data,
|
2014-05-10 16:32:28 +00:00
|
|
|
frame_samples, omap.data, max_payload_size * enc->n_channels);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2012-03-29 15:41:53 +00:00
|
|
|
gst_buffer_unmap (outbuf, &omap);
|
2011-11-14 13:41:58 +00:00
|
|
|
|
2012-03-07 11:59:28 +00:00
|
|
|
if (outsize < 0) {
|
|
|
|
GST_ERROR_OBJECT (enc, "Encoding failed: %d", outsize);
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto done;
|
2014-05-10 16:32:28 +00:00
|
|
|
} else if (outsize > max_payload_size) {
|
2012-03-07 11:59:28 +00:00
|
|
|
GST_WARNING_OBJECT (enc,
|
|
|
|
"Encoded size %d is higher than max payload size (%d bytes)",
|
2014-05-10 16:32:28 +00:00
|
|
|
outsize, max_payload_size);
|
2012-03-07 11:59:28 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto done;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 11:59:28 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "Output packet is %u bytes", outsize);
|
2012-03-30 15:09:34 +00:00
|
|
|
gst_buffer_set_size (outbuf, outsize);
|
2011-11-16 16:56:43 +00:00
|
|
|
|
2015-10-30 18:57:37 +00:00
|
|
|
|
2012-03-07 11:59:28 +00:00
|
|
|
ret =
|
|
|
|
gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), outbuf,
|
2015-10-30 18:57:37 +00:00
|
|
|
output_samples);
|
|
|
|
enc->encoded_samples += output_samples;
|
|
|
|
enc->consumed_samples += input_samples;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
if (bdata)
|
2012-03-07 16:14:29 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
2011-11-22 17:04:09 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
if (mdata)
|
|
|
|
g_free (mdata);
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_opus_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstOpusEnc *enc;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
enc = GST_OPUS_ENC (benc);
|
|
|
|
GST_DEBUG_OBJECT (enc, "handle_frame");
|
2012-12-18 15:56:28 +00:00
|
|
|
GST_DEBUG_OBJECT (enc, "received buffer %p of %" G_GSIZE_FORMAT " bytes", buf,
|
2011-11-25 11:48:58 +00:00
|
|
|
buf ? gst_buffer_get_size (buf) : 0);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
ret = gst_opus_enc_encode (enc, buf);
|
2011-06-05 07:54:19 +00:00
|
|
|
|
2011-11-14 13:41:58 +00:00
|
|
|
return ret;
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opus_enc_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstOpusEnc *enc;
|
|
|
|
|
|
|
|
enc = GST_OPUS_ENC (object);
|
|
|
|
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_lock (&enc->property_lock);
|
2011-11-22 17:04:09 +00:00
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_AUDIO:
|
2015-01-28 16:43:59 +00:00
|
|
|
g_value_set_boolean (value,
|
|
|
|
enc->audio_type == OPUS_APPLICATION_AUDIO ? TRUE : FALSE);
|
|
|
|
break;
|
|
|
|
case PROP_AUDIO_TYPE:
|
|
|
|
g_value_set_enum (value, enc->audio_type);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_BITRATE:
|
|
|
|
g_value_set_int (value, enc->bitrate);
|
|
|
|
break;
|
|
|
|
case PROP_BANDWIDTH:
|
2011-11-10 17:13:40 +00:00
|
|
|
g_value_set_enum (value, enc->bandwidth);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_FRAME_SIZE:
|
2011-11-16 19:40:20 +00:00
|
|
|
g_value_set_enum (value, enc->frame_size);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_CBR:
|
2015-09-11 09:11:09 +00:00
|
|
|
GST_WARNING_OBJECT (enc,
|
|
|
|
"cbr property is deprecated; use bitrate-type instead");
|
2015-03-12 12:49:40 +00:00
|
|
|
g_value_set_boolean (value, enc->bitrate_type == BITRATE_TYPE_CBR);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_CONSTRAINED_VBR:
|
2015-09-11 09:11:09 +00:00
|
|
|
GST_WARNING_OBJECT (enc,
|
|
|
|
"constrained-vbr property is deprecated; use bitrate-type instead");
|
2015-03-12 12:49:40 +00:00
|
|
|
g_value_set_boolean (value,
|
|
|
|
enc->bitrate_type == BITRATE_TYPE_CONSTRAINED_VBR);
|
2015-04-03 06:16:12 +00:00
|
|
|
break;
|
2015-03-12 12:49:40 +00:00
|
|
|
case PROP_BITRATE_TYPE:
|
|
|
|
g_value_set_enum (value, enc->bitrate_type);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_COMPLEXITY:
|
|
|
|
g_value_set_int (value, enc->complexity);
|
|
|
|
break;
|
|
|
|
case PROP_INBAND_FEC:
|
|
|
|
g_value_set_boolean (value, enc->inband_fec);
|
|
|
|
break;
|
|
|
|
case PROP_DTX:
|
|
|
|
g_value_set_boolean (value, enc->dtx);
|
|
|
|
break;
|
|
|
|
case PROP_PACKET_LOSS_PERCENT:
|
|
|
|
g_value_set_int (value, enc->packet_loss_percentage);
|
|
|
|
break;
|
2011-11-22 15:33:20 +00:00
|
|
|
case PROP_MAX_PAYLOAD_SIZE:
|
|
|
|
g_value_set_uint (value, enc->max_payload_size);
|
|
|
|
break;
|
2011-06-05 07:54:19 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2011-11-22 17:04:09 +00:00
|
|
|
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_unlock (&enc->property_lock);
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_opus_enc_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstOpusEnc *enc;
|
|
|
|
|
|
|
|
enc = GST_OPUS_ENC (object);
|
|
|
|
|
2011-11-22 17:04:09 +00:00
|
|
|
#define GST_OPUS_UPDATE_PROPERTY(prop,type,ctl) do { \
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_lock (&enc->property_lock); \
|
2011-11-22 17:04:09 +00:00
|
|
|
enc->prop = g_value_get_##type (value); \
|
|
|
|
if (enc->state) { \
|
2011-11-23 17:49:58 +00:00
|
|
|
opus_multistream_encoder_ctl (enc->state, OPUS_SET_##ctl (enc->prop)); \
|
2011-11-22 17:04:09 +00:00
|
|
|
} \
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_unlock (&enc->property_lock); \
|
2011-11-22 17:04:09 +00:00
|
|
|
} while(0)
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_AUDIO:
|
2015-01-28 16:43:59 +00:00
|
|
|
enc->audio_type =
|
|
|
|
g_value_get_boolean (value) ? OPUS_APPLICATION_AUDIO :
|
|
|
|
OPUS_APPLICATION_VOIP;
|
|
|
|
break;
|
|
|
|
case PROP_AUDIO_TYPE:
|
|
|
|
enc->audio_type = g_value_get_enum (value);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_BITRATE:
|
2011-11-22 17:04:09 +00:00
|
|
|
GST_OPUS_UPDATE_PROPERTY (bitrate, int, BITRATE);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_BANDWIDTH:
|
2011-11-22 17:04:09 +00:00
|
|
|
GST_OPUS_UPDATE_PROPERTY (bandwidth, enum, BANDWIDTH);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_FRAME_SIZE:
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_lock (&enc->property_lock);
|
2011-11-16 19:40:20 +00:00
|
|
|
enc->frame_size = g_value_get_enum (value);
|
2011-11-22 17:04:09 +00:00
|
|
|
enc->frame_samples = gst_opus_enc_get_frame_samples (enc);
|
|
|
|
gst_opus_enc_setup_base_class (enc, GST_AUDIO_ENCODER (enc));
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_unlock (&enc->property_lock);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_CBR:
|
2015-09-11 09:11:09 +00:00
|
|
|
GST_WARNING_OBJECT (enc,
|
|
|
|
"cbr property is deprecated; use bitrate-type instead");
|
2015-03-12 12:49:40 +00:00
|
|
|
g_warning ("cbr property is deprecated; use bitrate-type instead");
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_lock (&enc->property_lock);
|
2015-03-12 12:49:40 +00:00
|
|
|
enc->bitrate_type = BITRATE_TYPE_CBR;
|
|
|
|
if (enc->state) {
|
|
|
|
opus_multistream_encoder_ctl (enc->state, OPUS_SET_VBR (FALSE));
|
|
|
|
opus_multistream_encoder_ctl (enc->state,
|
|
|
|
OPUS_SET_VBR_CONSTRAINT (FALSE), 0);
|
|
|
|
}
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_unlock (&enc->property_lock);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_CONSTRAINED_VBR:
|
2015-09-11 09:11:09 +00:00
|
|
|
GST_WARNING_OBJECT (enc,
|
|
|
|
"constrained-vbr property is deprecated; use bitrate-type instead");
|
2015-03-12 12:49:40 +00:00
|
|
|
g_warning
|
|
|
|
("constrained-vbr property is deprecated; use bitrate-type instead");
|
|
|
|
g_mutex_lock (&enc->property_lock);
|
|
|
|
enc->bitrate_type = BITRATE_TYPE_CONSTRAINED_VBR;
|
|
|
|
if (enc->state) {
|
|
|
|
opus_multistream_encoder_ctl (enc->state, OPUS_SET_VBR (TRUE));
|
|
|
|
opus_multistream_encoder_ctl (enc->state,
|
|
|
|
OPUS_SET_VBR_CONSTRAINT (TRUE), 0);
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&enc->property_lock);
|
|
|
|
break;
|
|
|
|
case PROP_BITRATE_TYPE:
|
|
|
|
/* this one has an opposite meaning to the opus ctl... */
|
|
|
|
g_mutex_lock (&enc->property_lock);
|
|
|
|
enc->bitrate_type = g_value_get_enum (value);
|
|
|
|
if (enc->state) {
|
|
|
|
opus_multistream_encoder_ctl (enc->state,
|
|
|
|
OPUS_SET_VBR (enc->bitrate_type != BITRATE_TYPE_CBR));
|
|
|
|
opus_multistream_encoder_ctl (enc->state,
|
|
|
|
OPUS_SET_VBR_CONSTRAINT (enc->bitrate_type ==
|
|
|
|
BITRATE_TYPE_CONSTRAINED_VBR), 0);
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&enc->property_lock);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_COMPLEXITY:
|
2011-11-22 17:04:09 +00:00
|
|
|
GST_OPUS_UPDATE_PROPERTY (complexity, int, COMPLEXITY);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_INBAND_FEC:
|
2011-11-22 17:04:09 +00:00
|
|
|
GST_OPUS_UPDATE_PROPERTY (inband_fec, boolean, INBAND_FEC);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_DTX:
|
2011-11-22 17:04:09 +00:00
|
|
|
GST_OPUS_UPDATE_PROPERTY (dtx, boolean, DTX);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
case PROP_PACKET_LOSS_PERCENT:
|
2011-11-22 17:04:09 +00:00
|
|
|
GST_OPUS_UPDATE_PROPERTY (packet_loss_percentage, int, PACKET_LOSS_PERC);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
2011-11-22 15:33:20 +00:00
|
|
|
case PROP_MAX_PAYLOAD_SIZE:
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_lock (&enc->property_lock);
|
2011-11-22 15:33:20 +00:00
|
|
|
enc->max_payload_size = g_value_get_uint (value);
|
2012-09-11 12:31:49 +00:00
|
|
|
g_mutex_unlock (&enc->property_lock);
|
2011-06-05 07:54:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2011-11-22 17:04:09 +00:00
|
|
|
|
|
|
|
#undef GST_OPUS_UPDATE_PROPERTY
|
|
|
|
|
2011-06-05 07:54:19 +00:00
|
|
|
}
|