mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-26 19:51:11 +00:00
voamrwbenc: port to audioencoder
This commit is contained in:
parent
53723f81eb
commit
97279f1dfd
3 changed files with 88 additions and 162 deletions
|
@ -4,8 +4,10 @@ libgstvoamrwbenc_la_SOURCES = \
|
||||||
gstvoamrwb.c \
|
gstvoamrwb.c \
|
||||||
gstvoamrwbenc.c
|
gstvoamrwbenc.c
|
||||||
|
|
||||||
libgstvoamrwbenc_la_CFLAGS = $(GST_CFLAGS) $(VOAMRWBENC_CFLAGS)
|
libgstvoamrwbenc_la_CFLAGS = -DGST_USE_UNSTABLE_API \
|
||||||
libgstvoamrwbenc_la_LIBADD = $(GST_BASE_LIBS) $(VOAMRWBENC_LIBS)
|
$(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS) $(VOAMRWBENC_CFLAGS)
|
||||||
|
libgstvoamrwbenc_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) \
|
||||||
|
-lgstaudio-$(GST_MAJORMINOR) $(GST_BASE_LIBS) $(VOAMRWBENC_LIBS)
|
||||||
libgstvoamrwbenc_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
|
libgstvoamrwbenc_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
|
||||||
libgstvoamrwbenc_la_LIBTOOLFLAGS = --tag=disable-static
|
libgstvoamrwbenc_la_LIBTOOLFLAGS = --tag=disable-static
|
||||||
|
|
||||||
|
|
|
@ -109,31 +109,15 @@ static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
|
||||||
GST_DEBUG_CATEGORY_STATIC (gst_voamrwbenc_debug);
|
GST_DEBUG_CATEGORY_STATIC (gst_voamrwbenc_debug);
|
||||||
#define GST_CAT_DEFAULT gst_voamrwbenc_debug
|
#define GST_CAT_DEFAULT gst_voamrwbenc_debug
|
||||||
|
|
||||||
static void gst_voamrwbenc_finalize (GObject * object);
|
static gboolean gst_voamrwbenc_start (GstAudioEncoder * enc);
|
||||||
|
static gboolean gst_voamrwbenc_stop (GstAudioEncoder * enc);
|
||||||
|
static gboolean gst_voamrwbenc_set_format (GstAudioEncoder * enc,
|
||||||
|
GstAudioInfo * info);
|
||||||
|
static GstFlowReturn gst_voamrwbenc_handle_frame (GstAudioEncoder * enc,
|
||||||
|
GstBuffer * in_buf);
|
||||||
|
|
||||||
static GstFlowReturn gst_voamrwbenc_chain (GstPad * pad, GstBuffer * buffer);
|
GST_BOILERPLATE (GstVoAmrWbEnc, gst_voamrwbenc, GstAudioEncoder,
|
||||||
static gboolean gst_voamrwbenc_setcaps (GstPad * pad, GstCaps * caps);
|
GST_TYPE_AUDIO_ENCODER);
|
||||||
static GstStateChangeReturn gst_voamrwbenc_state_change (GstElement * element,
|
|
||||||
GstStateChange transition);
|
|
||||||
|
|
||||||
static void
|
|
||||||
_do_init (GType object_type)
|
|
||||||
{
|
|
||||||
const GInterfaceInfo preset_interface_info = {
|
|
||||||
NULL, /* interface init */
|
|
||||||
NULL, /* interface finalize */
|
|
||||||
NULL /* interface_data */
|
|
||||||
};
|
|
||||||
|
|
||||||
g_type_add_interface_static (object_type, GST_TYPE_PRESET,
|
|
||||||
&preset_interface_info);
|
|
||||||
|
|
||||||
GST_DEBUG_CATEGORY_INIT (gst_voamrwbenc_debug, "amrwbenc", 0,
|
|
||||||
"AMR-WB audio encoder");
|
|
||||||
}
|
|
||||||
|
|
||||||
GST_BOILERPLATE_FULL (GstVoAmrWbEnc, gst_voamrwbenc, GstElement,
|
|
||||||
GST_TYPE_ELEMENT, _do_init);
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_voamrwbenc_set_property (GObject * object, guint prop_id,
|
gst_voamrwbenc_set_property (GObject * object, guint prop_id,
|
||||||
|
@ -191,71 +175,74 @@ static void
|
||||||
gst_voamrwbenc_class_init (GstVoAmrWbEncClass * klass)
|
gst_voamrwbenc_class_init (GstVoAmrWbEncClass * klass)
|
||||||
{
|
{
|
||||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||||
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
|
||||||
|
|
||||||
object_class->finalize = gst_voamrwbenc_finalize;
|
|
||||||
object_class->set_property = gst_voamrwbenc_set_property;
|
object_class->set_property = gst_voamrwbenc_set_property;
|
||||||
object_class->get_property = gst_voamrwbenc_get_property;
|
object_class->get_property = gst_voamrwbenc_get_property;
|
||||||
|
|
||||||
|
base_class->start = GST_DEBUG_FUNCPTR (gst_voamrwbenc_start);
|
||||||
|
base_class->stop = GST_DEBUG_FUNCPTR (gst_voamrwbenc_stop);
|
||||||
|
base_class->set_format = GST_DEBUG_FUNCPTR (gst_voamrwbenc_set_format);
|
||||||
|
base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_voamrwbenc_handle_frame);
|
||||||
|
|
||||||
g_object_class_install_property (object_class, PROP_BANDMODE,
|
g_object_class_install_property (object_class, PROP_BANDMODE,
|
||||||
g_param_spec_enum ("band-mode", "Band Mode",
|
g_param_spec_enum ("band-mode", "Band Mode",
|
||||||
"Encoding Band Mode (Kbps)", GST_VOAMRWBENC_BANDMODE_TYPE,
|
"Encoding Band Mode (Kbps)", GST_VOAMRWBENC_BANDMODE_TYPE,
|
||||||
BANDMODE_DEFAULT,
|
BANDMODE_DEFAULT,
|
||||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
||||||
|
|
||||||
element_class->change_state = GST_DEBUG_FUNCPTR (gst_voamrwbenc_state_change);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_voamrwbenc_init (GstVoAmrWbEnc * amrwbenc, GstVoAmrWbEncClass * klass)
|
gst_voamrwbenc_init (GstVoAmrWbEnc * amrwbenc, GstVoAmrWbEncClass * klass)
|
||||||
{
|
{
|
||||||
/* create the sink pad */
|
|
||||||
amrwbenc->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
|
|
||||||
gst_pad_set_setcaps_function (amrwbenc->sinkpad, gst_voamrwbenc_setcaps);
|
|
||||||
gst_pad_set_chain_function (amrwbenc->sinkpad, gst_voamrwbenc_chain);
|
|
||||||
gst_element_add_pad (GST_ELEMENT (amrwbenc), amrwbenc->sinkpad);
|
|
||||||
|
|
||||||
/* create the src pad */
|
|
||||||
amrwbenc->srcpad = gst_pad_new_from_static_template (&src_template, "src");
|
|
||||||
gst_pad_use_fixed_caps (amrwbenc->srcpad);
|
|
||||||
gst_element_add_pad (GST_ELEMENT (amrwbenc), amrwbenc->srcpad);
|
|
||||||
|
|
||||||
amrwbenc->adapter = gst_adapter_new ();
|
|
||||||
|
|
||||||
/* init rest */
|
/* init rest */
|
||||||
amrwbenc->handle = NULL;
|
amrwbenc->handle = NULL;
|
||||||
amrwbenc->channels = 0;
|
amrwbenc->channels = 0;
|
||||||
amrwbenc->rate = 0;
|
amrwbenc->rate = 0;
|
||||||
amrwbenc->ts = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
gst_voamrwbenc_finalize (GObject * object)
|
|
||||||
{
|
|
||||||
GstVoAmrWbEnc *amrwbenc;
|
|
||||||
|
|
||||||
amrwbenc = GST_VOAMRWBENC (object);
|
|
||||||
|
|
||||||
g_object_unref (G_OBJECT (amrwbenc->adapter));
|
|
||||||
amrwbenc->adapter = NULL;
|
|
||||||
|
|
||||||
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
gst_voamrwbenc_setcaps (GstPad * pad, GstCaps * caps)
|
gst_voamrwbenc_start (GstAudioEncoder * enc)
|
||||||
|
{
|
||||||
|
GstVoAmrWbEnc *voamrwbenc = GST_VOAMRWBENC (enc);
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (enc, "start");
|
||||||
|
|
||||||
|
if (!(voamrwbenc->handle = E_IF_init ()))
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
voamrwbenc->rate = 0;
|
||||||
|
voamrwbenc->channels = 0;
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_voamrwbenc_stop (GstAudioEncoder * enc)
|
||||||
|
{
|
||||||
|
GstVoAmrWbEnc *voamrwbenc = GST_VOAMRWBENC (enc);
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (enc, "stop");
|
||||||
|
|
||||||
|
if (voamrwbenc->handle) {
|
||||||
|
E_IF_exit (voamrwbenc->handle);
|
||||||
|
voamrwbenc->handle = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_voamrwbenc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
|
||||||
{
|
{
|
||||||
GstStructure *structure;
|
|
||||||
GstVoAmrWbEnc *amrwbenc;
|
GstVoAmrWbEnc *amrwbenc;
|
||||||
GstCaps *copy;
|
GstCaps *copy;
|
||||||
|
|
||||||
amrwbenc = GST_VOAMRWBENC (GST_PAD_PARENT (pad));
|
amrwbenc = GST_VOAMRWBENC (benc);
|
||||||
|
|
||||||
structure = gst_caps_get_structure (caps, 0);
|
|
||||||
|
|
||||||
/* get channel count */
|
/* get channel count */
|
||||||
gst_structure_get_int (structure, "channels", &amrwbenc->channels);
|
amrwbenc->channels = GST_AUDIO_INFO_CHANNELS (info);
|
||||||
gst_structure_get_int (structure, "rate", &amrwbenc->rate);
|
amrwbenc->rate = GST_AUDIO_INFO_RATE (info);
|
||||||
|
|
||||||
/* this is not wrong but will sound bad */
|
/* this is not wrong but will sound bad */
|
||||||
if (amrwbenc->channels != 1) {
|
if (amrwbenc->channels != 1) {
|
||||||
|
@ -270,116 +257,59 @@ gst_voamrwbenc_setcaps (GstPad * pad, GstCaps * caps)
|
||||||
"channels", G_TYPE_INT, amrwbenc->channels,
|
"channels", G_TYPE_INT, amrwbenc->channels,
|
||||||
"rate", G_TYPE_INT, amrwbenc->rate, NULL);
|
"rate", G_TYPE_INT, amrwbenc->rate, NULL);
|
||||||
|
|
||||||
gst_pad_set_caps (amrwbenc->srcpad, copy);
|
gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (amrwbenc), copy);
|
||||||
gst_caps_unref (copy);
|
gst_caps_unref (copy);
|
||||||
|
|
||||||
|
/* report needs to base class: one frame at a time */
|
||||||
|
gst_audio_encoder_set_frame_samples_min (benc, L_FRAME16k);
|
||||||
|
gst_audio_encoder_set_frame_samples_max (benc, L_FRAME16k);
|
||||||
|
gst_audio_encoder_set_frame_max (benc, 1);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static GstFlowReturn
|
static GstFlowReturn
|
||||||
gst_voamrwbenc_chain (GstPad * pad, GstBuffer * buffer)
|
gst_voamrwbenc_handle_frame (GstAudioEncoder * benc, GstBuffer * buffer)
|
||||||
{
|
{
|
||||||
GstVoAmrWbEnc *amrwbenc;
|
GstVoAmrWbEnc *amrwbenc;
|
||||||
GstFlowReturn ret = GST_FLOW_OK;
|
GstFlowReturn ret = GST_FLOW_OK;
|
||||||
const int buffer_size = sizeof (short) * L_FRAME16k;
|
const int buffer_size = sizeof (short) * L_FRAME16k;
|
||||||
|
GstBuffer *out;
|
||||||
|
gint outsize;
|
||||||
|
|
||||||
amrwbenc = GST_VOAMRWBENC (gst_pad_get_parent (pad));
|
amrwbenc = GST_VOAMRWBENC (benc);
|
||||||
|
|
||||||
g_return_val_if_fail (amrwbenc->handle, GST_FLOW_WRONG_STATE);
|
g_return_val_if_fail (amrwbenc->handle, GST_FLOW_NOT_NEGOTIATED);
|
||||||
|
|
||||||
if (amrwbenc->rate == 0 || amrwbenc->channels == 0) {
|
if (amrwbenc->rate == 0 || amrwbenc->channels == 0) {
|
||||||
ret = GST_FLOW_NOT_NEGOTIATED;
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* discontinuity clears adapter, FIXME, maybe we can set some
|
/* we don't deal with squeezing remnants, so simply discard those */
|
||||||
* encoder flag to mask the discont. */
|
if (G_UNLIKELY (buffer == NULL)) {
|
||||||
if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
|
GST_DEBUG_OBJECT (amrwbenc, "no data");
|
||||||
gst_adapter_clear (amrwbenc->adapter);
|
goto done;
|
||||||
amrwbenc->ts = 0;
|
|
||||||
amrwbenc->discont = TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
|
if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < buffer_size)) {
|
||||||
amrwbenc->ts = GST_BUFFER_TIMESTAMP (buffer);
|
GST_DEBUG_OBJECT (amrwbenc, "discarding trailing data %d",
|
||||||
|
buffer ? GST_BUFFER_SIZE (buffer) : 0);
|
||||||
ret = GST_FLOW_OK;
|
ret = gst_audio_encoder_finish_frame (benc, NULL, -1);
|
||||||
gst_adapter_push (amrwbenc->adapter, buffer);
|
goto done;
|
||||||
|
|
||||||
/* Collect samples until we have enough for an output frame */
|
|
||||||
while (gst_adapter_available (amrwbenc->adapter) >= buffer_size) {
|
|
||||||
GstBuffer *out;
|
|
||||||
guint8 *data;
|
|
||||||
gint outsize;
|
|
||||||
|
|
||||||
out = gst_buffer_new_and_alloc (buffer_size);
|
|
||||||
GST_BUFFER_DURATION (out) = GST_SECOND * L_FRAME16k /
|
|
||||||
(amrwbenc->rate * amrwbenc->channels);
|
|
||||||
GST_BUFFER_TIMESTAMP (out) = amrwbenc->ts;
|
|
||||||
if (amrwbenc->ts != -1) {
|
|
||||||
amrwbenc->ts += GST_BUFFER_DURATION (out);
|
|
||||||
}
|
|
||||||
if (amrwbenc->discont) {
|
|
||||||
GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT);
|
|
||||||
amrwbenc->discont = FALSE;
|
|
||||||
}
|
|
||||||
gst_buffer_set_caps (out, gst_pad_get_caps (amrwbenc->srcpad));
|
|
||||||
|
|
||||||
data = (guint8 *) gst_adapter_peek (amrwbenc->adapter, buffer_size);
|
|
||||||
|
|
||||||
/* encode */
|
|
||||||
outsize =
|
|
||||||
E_IF_encode (amrwbenc->handle, amrwbenc->bandmode, (const short *) data,
|
|
||||||
(unsigned char *) GST_BUFFER_DATA (out), 0);
|
|
||||||
|
|
||||||
gst_adapter_flush (amrwbenc->adapter, buffer_size);
|
|
||||||
GST_BUFFER_SIZE (out) = outsize;
|
|
||||||
|
|
||||||
/* play */
|
|
||||||
if ((ret = gst_pad_push (amrwbenc->srcpad, out)) != GST_FLOW_OK)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out = gst_buffer_new_and_alloc (buffer_size);
|
||||||
|
/* encode */
|
||||||
|
outsize = E_IF_encode (amrwbenc->handle, amrwbenc->bandmode,
|
||||||
|
(const short *) GST_BUFFER_DATA (buffer),
|
||||||
|
(unsigned char *) GST_BUFFER_DATA (out), 0);
|
||||||
|
|
||||||
|
GST_LOG_OBJECT (amrwbenc, "encoded to %d bytes", outsize);
|
||||||
|
GST_BUFFER_SIZE (out) = outsize;
|
||||||
|
|
||||||
|
ret = gst_audio_encoder_finish_frame (benc, out, L_FRAME16k);
|
||||||
|
|
||||||
done:
|
done:
|
||||||
|
|
||||||
gst_object_unref (amrwbenc);
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static GstStateChangeReturn
|
|
||||||
gst_voamrwbenc_state_change (GstElement * element, GstStateChange transition)
|
|
||||||
{
|
|
||||||
GstVoAmrWbEnc *amrwbenc;
|
|
||||||
GstStateChangeReturn ret;
|
|
||||||
|
|
||||||
amrwbenc = GST_VOAMRWBENC (element);
|
|
||||||
|
|
||||||
switch (transition) {
|
|
||||||
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
||||||
if (!(amrwbenc->handle = E_IF_init ()))
|
|
||||||
return GST_STATE_CHANGE_FAILURE;
|
|
||||||
break;
|
|
||||||
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
||||||
amrwbenc->rate = 0;
|
|
||||||
amrwbenc->channels = 0;
|
|
||||||
amrwbenc->ts = 0;
|
|
||||||
amrwbenc->discont = FALSE;
|
|
||||||
gst_adapter_clear (amrwbenc->adapter);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
||||||
|
|
||||||
switch (transition) {
|
|
||||||
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
||||||
E_IF_exit (amrwbenc->handle);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,8 @@
|
||||||
#define __GST_VOAMRWBENC_H__
|
#define __GST_VOAMRWBENC_H__
|
||||||
|
|
||||||
#include <gst/gst.h>
|
#include <gst/gst.h>
|
||||||
#include <gst/base/gstadapter.h>
|
#include <gst/audio/gstaudioencoder.h>
|
||||||
|
|
||||||
#include <vo-amrwbenc/enc_if.h>
|
#include <vo-amrwbenc/enc_if.h>
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
@ -41,14 +42,7 @@ typedef struct _GstVoAmrWbEnc GstVoAmrWbEnc;
|
||||||
typedef struct _GstVoAmrWbEncClass GstVoAmrWbEncClass;
|
typedef struct _GstVoAmrWbEncClass GstVoAmrWbEncClass;
|
||||||
|
|
||||||
struct _GstVoAmrWbEnc {
|
struct _GstVoAmrWbEnc {
|
||||||
GstElement element;
|
GstAudioEncoder element;
|
||||||
|
|
||||||
/* pads */
|
|
||||||
GstPad *sinkpad, *srcpad;
|
|
||||||
guint64 ts;
|
|
||||||
gboolean discont;
|
|
||||||
|
|
||||||
GstAdapter *adapter;
|
|
||||||
|
|
||||||
/* library handle */
|
/* library handle */
|
||||||
void *handle;
|
void *handle;
|
||||||
|
@ -59,7 +53,7 @@ struct _GstVoAmrWbEnc {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _GstVoAmrWbEncClass {
|
struct _GstVoAmrWbEncClass {
|
||||||
GstElementClass parent_class;
|
GstAudioEncoderClass parent_class;
|
||||||
};
|
};
|
||||||
|
|
||||||
GType gst_voamrwbenc_get_type (void);
|
GType gst_voamrwbenc_get_type (void);
|
||||||
|
|
Loading…
Reference in a new issue