mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-02-17 03:35:21 +00:00
faac: port to audioencoder
This commit is contained in:
parent
70be630427
commit
c8a3567923
3 changed files with 174 additions and 416 deletions
|
@ -1,7 +1,8 @@
|
||||||
plugin_LTLIBRARIES = libgstfaac.la
|
plugin_LTLIBRARIES = libgstfaac.la
|
||||||
|
|
||||||
libgstfaac_la_SOURCES = gstfaac.c
|
libgstfaac_la_SOURCES = gstfaac.c
|
||||||
libgstfaac_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) \
|
libgstfaac_la_CFLAGS = -DGST_USE_UNSTABLE_API \
|
||||||
|
$(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) \
|
||||||
$(GST_CFLAGS) $(FAAC_CFLAGS)
|
$(GST_CFLAGS) $(FAAC_CFLAGS)
|
||||||
libgstfaac_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) \
|
libgstfaac_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) \
|
||||||
-lgstaudio-@GST_MAJORMINOR@ -lgstpbutils-@GST_MAJORMINOR@ \
|
-lgstaudio-@GST_MAJORMINOR@ -lgstpbutils-@GST_MAJORMINOR@ \
|
||||||
|
|
|
@ -91,12 +91,6 @@
|
||||||
"rate = (int) {" SAMPLE_RATES "}, " \
|
"rate = (int) {" SAMPLE_RATES "}, " \
|
||||||
"stream-format = (string) { adts, raw }, " \
|
"stream-format = (string) { adts, raw }, " \
|
||||||
"profile = (string) { main, lc }"
|
"profile = (string) { main, lc }"
|
||||||
enum
|
|
||||||
{
|
|
||||||
VBR = 1,
|
|
||||||
ABR
|
|
||||||
};
|
|
||||||
|
|
||||||
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
|
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
|
||||||
GST_PAD_SRC,
|
GST_PAD_SRC,
|
||||||
GST_PAD_ALWAYS,
|
GST_PAD_ALWAYS,
|
||||||
|
@ -119,35 +113,39 @@ enum
|
||||||
PROP_SHORTCTL
|
PROP_SHORTCTL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
VBR = 1,
|
||||||
|
ABR
|
||||||
|
};
|
||||||
|
|
||||||
static void gst_faac_base_init (GstFaacClass * klass);
|
static void gst_faac_base_init (GstFaacClass * klass);
|
||||||
static void gst_faac_class_init (GstFaacClass * klass);
|
static void gst_faac_class_init (GstFaacClass * klass);
|
||||||
static void gst_faac_init (GstFaac * faac);
|
static void gst_faac_init (GstFaac * faac);
|
||||||
static void gst_faac_finalize (GObject * object);
|
|
||||||
static void gst_faac_reset (GstFaac * faac);
|
|
||||||
|
|
||||||
static void gst_faac_set_property (GObject * object,
|
static void gst_faac_set_property (GObject * object,
|
||||||
guint prop_id, const GValue * value, GParamSpec * pspec);
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
||||||
static void gst_faac_get_property (GObject * object,
|
static void gst_faac_get_property (GObject * object,
|
||||||
guint prop_id, GValue * value, GParamSpec * pspec);
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
||||||
|
|
||||||
static gboolean gst_faac_sink_event (GstPad * pad, GstEvent * event);
|
|
||||||
static gboolean gst_faac_configure_source_pad (GstFaac * faac);
|
static gboolean gst_faac_configure_source_pad (GstFaac * faac);
|
||||||
static gboolean gst_faac_sink_setcaps (GstPad * pad, GstCaps * caps);
|
static GstCaps *gst_faac_getcaps (GstAudioEncoder * enc);
|
||||||
static GstCaps *gst_faac_sink_getcaps (GstPad * pad);
|
|
||||||
static GstFlowReturn gst_faac_push_buffers (GstFaac * faac, gboolean force);
|
static gboolean gst_faac_start (GstAudioEncoder * enc);
|
||||||
static GstFlowReturn gst_faac_chain (GstPad * pad, GstBuffer * data);
|
static gboolean gst_faac_stop (GstAudioEncoder * enc);
|
||||||
static GstStateChangeReturn gst_faac_change_state (GstElement * element,
|
static gboolean gst_faac_set_format (GstAudioEncoder * enc,
|
||||||
GstStateChange transition);
|
GstAudioInfo * info);
|
||||||
|
static GstFlowReturn gst_faac_handle_frame (GstAudioEncoder * enc,
|
||||||
|
GstBuffer * in_buf);
|
||||||
|
|
||||||
static GstElementClass *parent_class = NULL;
|
static GstElementClass *parent_class = NULL;
|
||||||
|
|
||||||
GST_DEBUG_CATEGORY_STATIC (faac_debug);
|
GST_DEBUG_CATEGORY_STATIC (faac_debug);
|
||||||
#define GST_CAT_DEFAULT faac_debug
|
#define GST_CAT_DEFAULT faac_debug
|
||||||
|
|
||||||
#define FAAC_DEFAULT_OUTPUTFORMAT 0 /* RAW */
|
|
||||||
#define FAAC_DEFAULT_QUALITY 100
|
#define FAAC_DEFAULT_QUALITY 100
|
||||||
#define FAAC_DEFAULT_BITRATE 128 * 1000
|
#define FAAC_DEFAULT_BITRATE 128 * 1000
|
||||||
#define FAAC_DEFAULT_RATE_CONTROL VBR
|
#define FAAC_DEFAULT_RATE_CONTROL VBR
|
||||||
#define FAAC_DEFAULT_TNS FALSE
|
#define FAAC_DEFAULT_TNS FALSE
|
||||||
#define FAAC_DEFAULT_MIDSIDE TRUE
|
#define FAAC_DEFAULT_MIDSIDE TRUE
|
||||||
#define FAAC_DEFAULT_SHORTCTL SHORTCTL_NORMAL
|
#define FAAC_DEFAULT_SHORTCTL SHORTCTL_NORMAL
|
||||||
|
@ -169,17 +167,9 @@ gst_faac_get_type (void)
|
||||||
0,
|
0,
|
||||||
(GInstanceInitFunc) gst_faac_init,
|
(GInstanceInitFunc) gst_faac_init,
|
||||||
};
|
};
|
||||||
const GInterfaceInfo preset_interface_info = {
|
|
||||||
NULL, /* interface_init */
|
|
||||||
NULL, /* interface_finalize */
|
|
||||||
NULL /* interface_data */
|
|
||||||
};
|
|
||||||
|
|
||||||
gst_faac_type = g_type_register_static (GST_TYPE_ELEMENT,
|
gst_faac_type = g_type_register_static (GST_TYPE_AUDIO_ENCODER,
|
||||||
"GstFaac", &gst_faac_info, 0);
|
"GstFaac", &gst_faac_info, 0);
|
||||||
|
|
||||||
g_type_add_interface_static (gst_faac_type, GST_TYPE_PRESET,
|
|
||||||
&preset_interface_info);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return gst_faac_type;
|
return gst_faac_type;
|
||||||
|
@ -248,96 +238,53 @@ static void
|
||||||
gst_faac_class_init (GstFaacClass * klass)
|
gst_faac_class_init (GstFaacClass * klass)
|
||||||
{
|
{
|
||||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||||
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
|
GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
|
||||||
|
|
||||||
parent_class = g_type_class_peek_parent (klass);
|
parent_class = g_type_class_peek_parent (klass);
|
||||||
|
|
||||||
gobject_class->set_property = gst_faac_set_property;
|
gobject_class->set_property = gst_faac_set_property;
|
||||||
gobject_class->get_property = gst_faac_get_property;
|
gobject_class->get_property = gst_faac_get_property;
|
||||||
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_faac_finalize);
|
|
||||||
|
base_class->start = GST_DEBUG_FUNCPTR (gst_faac_start);
|
||||||
|
base_class->stop = GST_DEBUG_FUNCPTR (gst_faac_stop);
|
||||||
|
base_class->set_format = GST_DEBUG_FUNCPTR (gst_faac_set_format);
|
||||||
|
base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_faac_handle_frame);
|
||||||
|
base_class->getcaps = GST_DEBUG_FUNCPTR (gst_faac_getcaps);
|
||||||
|
|
||||||
/* properties */
|
/* properties */
|
||||||
g_object_class_install_property (gobject_class, PROP_QUALITY,
|
g_object_class_install_property (gobject_class, PROP_QUALITY,
|
||||||
g_param_spec_int ("quality", "Quality (%)",
|
g_param_spec_int ("quality", "Quality (%)",
|
||||||
"Variable bitrate (VBR) quantizer quality in %", 1, 1000,
|
"Variable bitrate (VBR) quantizer quality in %", 1, 1000,
|
||||||
FAAC_DEFAULT_QUALITY, G_PARAM_READWRITE));
|
FAAC_DEFAULT_QUALITY,
|
||||||
|
G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
||||||
g_object_class_install_property (gobject_class, PROP_BITRATE,
|
g_object_class_install_property (gobject_class, PROP_BITRATE,
|
||||||
g_param_spec_int ("bitrate", "Bitrate (bps)",
|
g_param_spec_int ("bitrate", "Bitrate (bps)",
|
||||||
"Average bitrate (ABR) in bits/sec", 8 * 1000, 320 * 1000,
|
"Average Bitrate (ABR) in bits/sec", 8 * 1000, 320 * 1000,
|
||||||
FAAC_DEFAULT_BITRATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
FAAC_DEFAULT_BITRATE,
|
||||||
|
G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
||||||
g_object_class_install_property (gobject_class, PROP_RATE_CONTROL,
|
g_object_class_install_property (gobject_class, PROP_RATE_CONTROL,
|
||||||
g_param_spec_enum ("rate-control", "Rate Control (ABR/VBR)",
|
g_param_spec_enum ("rate-control", "Rate Control (ABR/VBR)",
|
||||||
"Encoding bitrate type (VBR/ABR)", GST_TYPE_FAAC_RATE_CONTROL,
|
"Encoding bitrate type (VBR/ABR)", GST_TYPE_FAAC_RATE_CONTROL,
|
||||||
FAAC_DEFAULT_RATE_CONTROL, G_PARAM_READWRITE));
|
FAAC_DEFAULT_RATE_CONTROL,
|
||||||
|
G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
||||||
g_object_class_install_property (gobject_class, PROP_TNS,
|
g_object_class_install_property (gobject_class, PROP_TNS,
|
||||||
g_param_spec_boolean ("tns", "TNS", "Use temporal noise shaping",
|
g_param_spec_boolean ("tns", "TNS", "Use temporal noise shaping",
|
||||||
FAAC_DEFAULT_TNS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
FAAC_DEFAULT_TNS,
|
||||||
|
G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
||||||
g_object_class_install_property (gobject_class, PROP_MIDSIDE,
|
g_object_class_install_property (gobject_class, PROP_MIDSIDE,
|
||||||
g_param_spec_boolean ("midside", "Midside", "Allow mid/side encoding",
|
g_param_spec_boolean ("midside", "Midside", "Allow mid/side encoding",
|
||||||
FAAC_DEFAULT_MIDSIDE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
FAAC_DEFAULT_MIDSIDE,
|
||||||
|
G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
||||||
g_object_class_install_property (gobject_class, PROP_SHORTCTL,
|
g_object_class_install_property (gobject_class, PROP_SHORTCTL,
|
||||||
g_param_spec_enum ("shortctl", "Block type",
|
g_param_spec_enum ("shortctl", "Block type",
|
||||||
"Block type encorcing",
|
"Block type encorcing",
|
||||||
GST_TYPE_FAAC_SHORTCTL, FAAC_DEFAULT_SHORTCTL,
|
GST_TYPE_FAAC_SHORTCTL, FAAC_DEFAULT_SHORTCTL,
|
||||||
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
||||||
|
|
||||||
/* virtual functions */
|
|
||||||
gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_faac_change_state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_faac_init (GstFaac * faac)
|
gst_faac_init (GstFaac * faac)
|
||||||
{
|
{
|
||||||
faac->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
|
|
||||||
gst_pad_set_chain_function (faac->sinkpad,
|
|
||||||
GST_DEBUG_FUNCPTR (gst_faac_chain));
|
|
||||||
gst_pad_set_setcaps_function (faac->sinkpad,
|
|
||||||
GST_DEBUG_FUNCPTR (gst_faac_sink_setcaps));
|
|
||||||
gst_pad_set_getcaps_function (faac->sinkpad,
|
|
||||||
GST_DEBUG_FUNCPTR (gst_faac_sink_getcaps));
|
|
||||||
gst_pad_set_event_function (faac->sinkpad,
|
|
||||||
GST_DEBUG_FUNCPTR (gst_faac_sink_event));
|
|
||||||
gst_element_add_pad (GST_ELEMENT (faac), faac->sinkpad);
|
|
||||||
|
|
||||||
faac->srcpad = gst_pad_new_from_static_template (&src_template, "src");
|
|
||||||
gst_pad_use_fixed_caps (faac->srcpad);
|
|
||||||
gst_element_add_pad (GST_ELEMENT (faac), faac->srcpad);
|
|
||||||
|
|
||||||
faac->adapter = gst_adapter_new ();
|
|
||||||
|
|
||||||
faac->profile = LOW;
|
|
||||||
faac->mpegversion = 4;
|
|
||||||
|
|
||||||
/* default properties */
|
|
||||||
faac->quality = FAAC_DEFAULT_QUALITY;
|
|
||||||
faac->bitrate = FAAC_DEFAULT_BITRATE;
|
|
||||||
faac->brtype = FAAC_DEFAULT_RATE_CONTROL;
|
|
||||||
faac->shortctl = FAAC_DEFAULT_SHORTCTL;
|
|
||||||
faac->outputformat = FAAC_DEFAULT_OUTPUTFORMAT;
|
|
||||||
faac->tns = FAAC_DEFAULT_TNS;
|
|
||||||
faac->midside = FAAC_DEFAULT_MIDSIDE;
|
|
||||||
|
|
||||||
gst_faac_reset (faac);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
gst_faac_reset (GstFaac * faac)
|
|
||||||
{
|
|
||||||
faac->handle = NULL;
|
|
||||||
faac->samplerate = -1;
|
|
||||||
faac->channels = -1;
|
|
||||||
faac->offset = 0;
|
|
||||||
gst_adapter_clear (faac->adapter);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
gst_faac_finalize (GObject * object)
|
|
||||||
{
|
|
||||||
GstFaac *faac = (GstFaac *) object;
|
|
||||||
|
|
||||||
g_object_unref (faac->adapter);
|
|
||||||
|
|
||||||
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -346,8 +293,25 @@ gst_faac_close_encoder (GstFaac * faac)
|
||||||
if (faac->handle)
|
if (faac->handle)
|
||||||
faacEncClose (faac->handle);
|
faacEncClose (faac->handle);
|
||||||
faac->handle = NULL;
|
faac->handle = NULL;
|
||||||
gst_adapter_clear (faac->adapter);
|
}
|
||||||
faac->offset = 0;
|
|
||||||
|
static gboolean
|
||||||
|
gst_faac_start (GstAudioEncoder * enc)
|
||||||
|
{
|
||||||
|
GstFaac *faac = GST_FAAC (enc);
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (faac, "start");
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_faac_stop (GstAudioEncoder * enc)
|
||||||
|
{
|
||||||
|
GstFaac *faac = GST_FAAC (enc);
|
||||||
|
|
||||||
|
GST_DEBUG_OBJECT (faac, "stop");
|
||||||
|
gst_faac_close_encoder (faac);
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const GstAudioChannelPosition aac_channel_positions[][8] = {
|
static const GstAudioChannelPosition aac_channel_positions[][8] = {
|
||||||
|
@ -380,7 +344,7 @@ static const GstAudioChannelPosition aac_channel_positions[][8] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static GstCaps *
|
static GstCaps *
|
||||||
gst_faac_sink_getcaps (GstPad * pad)
|
gst_faac_getcaps (GstAudioEncoder * enc)
|
||||||
{
|
{
|
||||||
static volatile gsize sinkcaps = 0;
|
static volatile gsize sinkcaps = 0;
|
||||||
|
|
||||||
|
@ -433,12 +397,78 @@ gst_faac_sink_getcaps (GstPad * pad)
|
||||||
gst_structure_free (s);
|
gst_structure_free (s);
|
||||||
g_value_unset (&rates_arr);
|
g_value_unset (&rates_arr);
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (pad, "Generated sinkcaps: %" GST_PTR_FORMAT, tmp);
|
GST_DEBUG_OBJECT (enc, "Generated sinkcaps: %" GST_PTR_FORMAT, tmp);
|
||||||
|
|
||||||
g_once_init_leave (&sinkcaps, (gsize) tmp);
|
g_once_init_leave (&sinkcaps, (gsize) tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
return gst_caps_ref ((GstCaps *) sinkcaps);
|
return gst_audio_encoder_proxy_getcaps (enc, (GstCaps *) sinkcaps);
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
gst_faac_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
|
||||||
|
{
|
||||||
|
GstFaac *faac = GST_FAAC (enc);
|
||||||
|
faacEncHandle *handle;
|
||||||
|
gint channels, samplerate, width;
|
||||||
|
gulong samples, bytes, fmt = 0, bps = 0;
|
||||||
|
gboolean result = FALSE;
|
||||||
|
|
||||||
|
/* base class takes care */
|
||||||
|
channels = GST_AUDIO_INFO_CHANNELS (info);
|
||||||
|
samplerate = GST_AUDIO_INFO_RATE (info);
|
||||||
|
width = GST_AUDIO_INFO_WIDTH (info);
|
||||||
|
|
||||||
|
if (GST_AUDIO_INFO_IS_INTEGER (info)) {
|
||||||
|
switch (width) {
|
||||||
|
case 16:
|
||||||
|
fmt = FAAC_INPUT_16BIT;
|
||||||
|
bps = 2;
|
||||||
|
break;
|
||||||
|
case 24:
|
||||||
|
case 32:
|
||||||
|
fmt = FAAC_INPUT_32BIT;
|
||||||
|
bps = 4;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
g_return_val_if_reached (FALSE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt = FAAC_INPUT_FLOAT;
|
||||||
|
bps = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* clean up in case of re-configure */
|
||||||
|
gst_faac_close_encoder (faac);
|
||||||
|
|
||||||
|
if (!(handle = faacEncOpen (samplerate, channels, &samples, &bytes)))
|
||||||
|
goto setup_failed;
|
||||||
|
|
||||||
|
/* ok, record and set up */
|
||||||
|
faac->format = fmt;
|
||||||
|
faac->bps = bps;
|
||||||
|
faac->handle = handle;
|
||||||
|
faac->bytes = bytes;
|
||||||
|
faac->samples = samples;
|
||||||
|
faac->channels = channels;
|
||||||
|
faac->samplerate = samplerate;
|
||||||
|
|
||||||
|
/* finish up */
|
||||||
|
result = gst_faac_configure_source_pad (faac);
|
||||||
|
|
||||||
|
/* report needs to base class */
|
||||||
|
gst_audio_encoder_set_frame_samples (enc, samples);
|
||||||
|
gst_audio_encoder_set_frame_max (enc, 1);
|
||||||
|
|
||||||
|
done:
|
||||||
|
return result;
|
||||||
|
|
||||||
|
/* ERRORS */
|
||||||
|
setup_failed:
|
||||||
|
{
|
||||||
|
GST_ELEMENT_ERROR (faac, LIBRARY, SETTINGS, (NULL), (NULL));
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check downstream caps to configure format */
|
/* check downstream caps to configure format */
|
||||||
|
@ -447,7 +477,12 @@ gst_faac_negotiate (GstFaac * faac)
|
||||||
{
|
{
|
||||||
GstCaps *caps;
|
GstCaps *caps;
|
||||||
|
|
||||||
caps = gst_pad_get_allowed_caps (faac->srcpad);
|
/* default setup */
|
||||||
|
faac->profile = LOW;
|
||||||
|
faac->mpegversion = 4;
|
||||||
|
faac->outputformat = 0;
|
||||||
|
|
||||||
|
caps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (faac));
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (faac, "allowed caps: %" GST_PTR_FORMAT, caps);
|
GST_DEBUG_OBJECT (faac, "allowed caps: %" GST_PTR_FORMAT, caps);
|
||||||
|
|
||||||
|
@ -494,94 +529,6 @@ gst_faac_negotiate (GstFaac * faac)
|
||||||
gst_caps_unref (caps);
|
gst_caps_unref (caps);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
|
||||||
gst_faac_sink_setcaps (GstPad * pad, GstCaps * caps)
|
|
||||||
{
|
|
||||||
GstFaac *faac = GST_FAAC (gst_pad_get_parent (pad));
|
|
||||||
GstStructure *structure = gst_caps_get_structure (caps, 0);
|
|
||||||
faacEncHandle *handle;
|
|
||||||
gint channels, samplerate, width;
|
|
||||||
gulong samples, bytes, fmt = 0, bps = 0;
|
|
||||||
gboolean result = FALSE;
|
|
||||||
|
|
||||||
if (!gst_caps_is_fixed (caps))
|
|
||||||
goto refuse_caps;
|
|
||||||
|
|
||||||
if (!gst_structure_get_int (structure, "channels", &channels) ||
|
|
||||||
!gst_structure_get_int (structure, "rate", &samplerate)) {
|
|
||||||
goto refuse_caps;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gst_structure_has_name (structure, "audio/x-raw-int")) {
|
|
||||||
gst_structure_get_int (structure, "width", &width);
|
|
||||||
switch (width) {
|
|
||||||
case 16:
|
|
||||||
fmt = FAAC_INPUT_16BIT;
|
|
||||||
bps = 2;
|
|
||||||
break;
|
|
||||||
case 24:
|
|
||||||
case 32:
|
|
||||||
fmt = FAAC_INPUT_32BIT;
|
|
||||||
bps = 4;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
g_return_val_if_reached (FALSE);
|
|
||||||
}
|
|
||||||
} else if (gst_structure_has_name (structure, "audio/x-raw-float")) {
|
|
||||||
fmt = FAAC_INPUT_FLOAT;
|
|
||||||
bps = 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fmt)
|
|
||||||
goto refuse_caps;
|
|
||||||
|
|
||||||
/* If the encoder is initialized, do not
|
|
||||||
reinitialize it again if not necessary */
|
|
||||||
if (faac->handle) {
|
|
||||||
if (samplerate == faac->samplerate && channels == faac->channels &&
|
|
||||||
fmt == faac->format)
|
|
||||||
return TRUE;
|
|
||||||
|
|
||||||
/* clear out pending frames */
|
|
||||||
gst_faac_push_buffers (faac, TRUE);
|
|
||||||
|
|
||||||
gst_faac_close_encoder (faac);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(handle = faacEncOpen (samplerate, channels, &samples, &bytes)))
|
|
||||||
goto setup_failed;
|
|
||||||
|
|
||||||
/* ok, record and set up */
|
|
||||||
faac->format = fmt;
|
|
||||||
faac->bps = bps;
|
|
||||||
faac->handle = handle;
|
|
||||||
faac->bytes = bytes;
|
|
||||||
faac->samples = samples;
|
|
||||||
faac->channels = channels;
|
|
||||||
faac->samplerate = samplerate;
|
|
||||||
|
|
||||||
gst_faac_negotiate (faac);
|
|
||||||
|
|
||||||
/* finish up */
|
|
||||||
result = gst_faac_configure_source_pad (faac);
|
|
||||||
|
|
||||||
done:
|
|
||||||
gst_object_unref (faac);
|
|
||||||
return result;
|
|
||||||
|
|
||||||
/* ERRORS */
|
|
||||||
setup_failed:
|
|
||||||
{
|
|
||||||
GST_ELEMENT_ERROR (faac, LIBRARY, SETTINGS, (NULL), (NULL));
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
refuse_caps:
|
|
||||||
{
|
|
||||||
GST_WARNING_OBJECT (faac, "refused caps %" GST_PTR_FORMAT, caps);
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
gst_faac_configure_source_pad (GstFaac * faac)
|
gst_faac_configure_source_pad (GstFaac * faac)
|
||||||
{
|
{
|
||||||
|
@ -590,6 +537,9 @@ gst_faac_configure_source_pad (GstFaac * faac)
|
||||||
faacEncConfiguration *conf;
|
faacEncConfiguration *conf;
|
||||||
guint maxbitrate;
|
guint maxbitrate;
|
||||||
|
|
||||||
|
/* negotiate stream format */
|
||||||
|
gst_faac_negotiate (faac);
|
||||||
|
|
||||||
/* we negotiated caps update current configuration */
|
/* we negotiated caps update current configuration */
|
||||||
conf = faacEncGetCurrentConfiguration (faac->handle);
|
conf = faacEncGetCurrentConfiguration (faac->handle);
|
||||||
conf->mpegVersion = (faac->mpegversion == 4) ? MPEG4 : MPEG2;
|
conf->mpegVersion = (faac->mpegversion == 4) ? MPEG4 : MPEG2;
|
||||||
|
@ -698,7 +648,7 @@ gst_faac_configure_source_pad (GstFaac * faac)
|
||||||
|
|
||||||
GST_DEBUG_OBJECT (faac, "src pad caps: %" GST_PTR_FORMAT, srccaps);
|
GST_DEBUG_OBJECT (faac, "src pad caps: %" GST_PTR_FORMAT, srccaps);
|
||||||
|
|
||||||
ret = gst_pad_set_caps (faac->srcpad, srccaps);
|
ret = gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (faac), srccaps);
|
||||||
gst_caps_unref (srccaps);
|
gst_caps_unref (srccaps);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -717,127 +667,33 @@ invalid_codec_data:
|
||||||
}
|
}
|
||||||
|
|
||||||
static GstFlowReturn
|
static GstFlowReturn
|
||||||
gst_faac_push_buffers (GstFaac * faac, gboolean force)
|
gst_faac_handle_frame (GstAudioEncoder * enc, GstBuffer * in_buf)
|
||||||
{
|
{
|
||||||
|
GstFaac *faac = GST_FAAC (enc);
|
||||||
GstFlowReturn ret = GST_FLOW_OK;
|
GstFlowReturn ret = GST_FLOW_OK;
|
||||||
gint av, frame_size, size, ret_size;
|
GstBuffer *out_buf;
|
||||||
GstBuffer *outbuf;
|
gint size, ret_size;
|
||||||
guint64 timestamp, distance;
|
|
||||||
const guint8 *data;
|
const guint8 *data;
|
||||||
|
|
||||||
/* samples already considers channel count */
|
out_buf = gst_buffer_new_and_alloc (faac->bytes);
|
||||||
frame_size = faac->samples * faac->bps;
|
|
||||||
|
|
||||||
while (G_LIKELY (ret == GST_FLOW_OK)) {
|
if (G_LIKELY (in_buf)) {
|
||||||
|
data = GST_BUFFER_DATA (in_buf);
|
||||||
av = gst_adapter_available (faac->adapter);
|
size = GST_BUFFER_SIZE (in_buf);
|
||||||
|
} else {
|
||||||
GST_LOG_OBJECT (faac, "pushing: force: %d, frame_size: %d, av: %d, "
|
data = NULL;
|
||||||
"offset: %d", force, frame_size, av, faac->offset);
|
size = 0;
|
||||||
|
|
||||||
/* idea:
|
|
||||||
* - start of adapter corresponds with what has already been encoded
|
|
||||||
* (i.e. really returned by faac)
|
|
||||||
* - start + offset is what needs to be fed to faac next
|
|
||||||
* That way we can timestamp the output based
|
|
||||||
* on adapter provided timestamp (and duration is a fixed frame duration) */
|
|
||||||
|
|
||||||
/* not enough data for one frame and no flush forcing */
|
|
||||||
if (!force && (av < frame_size + faac->offset))
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (G_LIKELY (av - faac->offset >= frame_size)) {
|
|
||||||
GST_LOG_OBJECT (faac, "encoding a frame");
|
|
||||||
data = gst_adapter_peek (faac->adapter, faac->offset + frame_size);
|
|
||||||
data += faac->offset;
|
|
||||||
size = frame_size;
|
|
||||||
} else if (av - faac->offset > 0) {
|
|
||||||
GST_LOG_OBJECT (faac, "encoding leftover");
|
|
||||||
data = gst_adapter_peek (faac->adapter, av);
|
|
||||||
data += faac->offset;
|
|
||||||
size = av - faac->offset;
|
|
||||||
} else {
|
|
||||||
GST_LOG_OBJECT (faac, "emptying encoder");
|
|
||||||
data = NULL;
|
|
||||||
size = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
outbuf = gst_buffer_new_and_alloc (faac->bytes);
|
|
||||||
|
|
||||||
if (G_UNLIKELY ((ret_size = faacEncEncode (faac->handle, (gint32 *) data,
|
|
||||||
size / faac->bps, GST_BUFFER_DATA (outbuf),
|
|
||||||
faac->bytes)) < 0)) {
|
|
||||||
gst_buffer_unref (outbuf);
|
|
||||||
goto encode_failed;
|
|
||||||
}
|
|
||||||
|
|
||||||
GST_LOG_OBJECT (faac, "encoder return: %d", ret_size);
|
|
||||||
|
|
||||||
/* consumed, advanced view */
|
|
||||||
faac->offset += size;
|
|
||||||
g_assert (faac->offset <= av);
|
|
||||||
|
|
||||||
if (G_UNLIKELY (!ret_size)) {
|
|
||||||
gst_buffer_unref (outbuf);
|
|
||||||
if (size)
|
|
||||||
continue;
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* deal with encoder lead-out */
|
|
||||||
if (G_UNLIKELY (av == 0 && faac->offset == 0)) {
|
|
||||||
GST_DEBUG_OBJECT (faac, "encoder returned additional data");
|
|
||||||
/* continuous with previous output, ok to have 0 duration */
|
|
||||||
timestamp = faac->next_ts;
|
|
||||||
} else {
|
|
||||||
/* after some caching, finally some data */
|
|
||||||
/* adapter gives time */
|
|
||||||
timestamp = gst_adapter_prev_timestamp (faac->adapter, &distance);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (G_LIKELY ((av = gst_adapter_available (faac->adapter)) >= frame_size)) {
|
|
||||||
/* must have then come from a complete frame */
|
|
||||||
gst_adapter_flush (faac->adapter, frame_size);
|
|
||||||
faac->offset -= frame_size;
|
|
||||||
size = frame_size;
|
|
||||||
} else {
|
|
||||||
/* otherwise leftover */
|
|
||||||
gst_adapter_clear (faac->adapter);
|
|
||||||
faac->offset = 0;
|
|
||||||
size = av;
|
|
||||||
}
|
|
||||||
|
|
||||||
GST_BUFFER_SIZE (outbuf) = ret_size;
|
|
||||||
if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (timestamp)))
|
|
||||||
GST_BUFFER_TIMESTAMP (outbuf) = timestamp +
|
|
||||||
GST_FRAMES_TO_CLOCK_TIME (distance / faac->channels / faac->bps,
|
|
||||||
faac->samplerate);
|
|
||||||
GST_BUFFER_DURATION (outbuf) =
|
|
||||||
GST_FRAMES_TO_CLOCK_TIME (size / faac->channels / faac->bps,
|
|
||||||
faac->samplerate);
|
|
||||||
faac->next_ts =
|
|
||||||
GST_BUFFER_TIMESTAMP (outbuf) + GST_BUFFER_DURATION (outbuf);
|
|
||||||
|
|
||||||
/* perhaps check/set DISCONT based on timestamps ? */
|
|
||||||
|
|
||||||
GST_LOG_OBJECT (faac, "Pushing out buffer time: %" GST_TIME_FORMAT
|
|
||||||
" duration: %" GST_TIME_FORMAT,
|
|
||||||
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
|
|
||||||
GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
|
|
||||||
|
|
||||||
gst_buffer_set_caps (outbuf, GST_PAD_CAPS (faac->srcpad));
|
|
||||||
ret = gst_pad_push (faac->srcpad, outbuf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* in case encoder returns less than expected, clear our view as well */
|
if (G_UNLIKELY ((ret_size = faacEncEncode (faac->handle, (gint32 *) data,
|
||||||
if (G_UNLIKELY (force)) {
|
size / faac->bps, GST_BUFFER_DATA (out_buf),
|
||||||
#ifndef GST_DISABLE_GST_DEBUG
|
GST_BUFFER_SIZE (out_buf))) < 0))
|
||||||
if ((av = gst_adapter_available (faac->adapter)))
|
goto encode_failed;
|
||||||
GST_WARNING_OBJECT (faac, "encoder left %d bytes; discarding", av);
|
|
||||||
#endif
|
GST_LOG_OBJECT (faac, "encoder return: %d", ret_size);
|
||||||
gst_adapter_clear (faac->adapter);
|
if (ret_size > 0) {
|
||||||
faac->offset = 0;
|
GST_BUFFER_SIZE (out_buf) = ret_size;
|
||||||
|
ret = gst_audio_encoder_finish_frame (enc, out_buf, faac->samples);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -850,72 +706,6 @@ encode_failed:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
|
||||||
gst_faac_sink_event (GstPad * pad, GstEvent * event)
|
|
||||||
{
|
|
||||||
GstFaac *faac;
|
|
||||||
gboolean ret;
|
|
||||||
|
|
||||||
faac = GST_FAAC (gst_pad_get_parent (pad));
|
|
||||||
|
|
||||||
GST_LOG_OBJECT (faac, "received %s", GST_EVENT_TYPE_NAME (event));
|
|
||||||
|
|
||||||
switch (GST_EVENT_TYPE (event)) {
|
|
||||||
case GST_EVENT_EOS:
|
|
||||||
{
|
|
||||||
if (faac->handle) {
|
|
||||||
/* flush first */
|
|
||||||
GST_DEBUG_OBJECT (faac, "Pushing out remaining buffers because of EOS");
|
|
||||||
gst_faac_push_buffers (faac, TRUE);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = gst_pad_event_default (pad, event);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
ret = gst_pad_event_default (pad, event);
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
gst_object_unref (faac);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static GstFlowReturn
|
|
||||||
gst_faac_chain (GstPad * pad, GstBuffer * inbuf)
|
|
||||||
{
|
|
||||||
GstFlowReturn result = GST_FLOW_OK;
|
|
||||||
GstFaac *faac;
|
|
||||||
|
|
||||||
faac = GST_FAAC (gst_pad_get_parent (pad));
|
|
||||||
|
|
||||||
if (!faac->handle)
|
|
||||||
goto no_handle;
|
|
||||||
|
|
||||||
GST_LOG_OBJECT (faac, "Got buffer time: %" GST_TIME_FORMAT " duration: %"
|
|
||||||
GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)),
|
|
||||||
GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)));
|
|
||||||
|
|
||||||
gst_adapter_push (faac->adapter, inbuf);
|
|
||||||
|
|
||||||
result = gst_faac_push_buffers (faac, FALSE);
|
|
||||||
|
|
||||||
done:
|
|
||||||
gst_object_unref (faac);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
|
|
||||||
/* ERRORS */
|
|
||||||
no_handle:
|
|
||||||
{
|
|
||||||
GST_ELEMENT_ERROR (faac, CORE, NEGOTIATION, (NULL),
|
|
||||||
("format wasn't negotiated before chain function"));
|
|
||||||
gst_buffer_unref (inbuf);
|
|
||||||
result = GST_FLOW_ERROR;
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gst_faac_set_property (GObject * object,
|
gst_faac_set_property (GObject * object,
|
||||||
guint prop_id, const GValue * value, GParamSpec * pspec)
|
guint prop_id, const GValue * value, GParamSpec * pspec)
|
||||||
|
@ -986,35 +776,6 @@ gst_faac_get_property (GObject * object,
|
||||||
GST_OBJECT_UNLOCK (faac);
|
GST_OBJECT_UNLOCK (faac);
|
||||||
}
|
}
|
||||||
|
|
||||||
static GstStateChangeReturn
|
|
||||||
gst_faac_change_state (GstElement * element, GstStateChange transition)
|
|
||||||
{
|
|
||||||
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
|
|
||||||
GstFaac *faac = GST_FAAC (element);
|
|
||||||
|
|
||||||
/* upwards state changes */
|
|
||||||
switch (transition) {
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
||||||
|
|
||||||
/* downwards state changes */
|
|
||||||
switch (transition) {
|
|
||||||
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
||||||
{
|
|
||||||
gst_faac_close_encoder (faac);
|
|
||||||
gst_faac_reset (faac);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
plugin_init (GstPlugin * plugin)
|
plugin_init (GstPlugin * plugin)
|
||||||
{
|
{
|
||||||
|
|
|
@ -21,8 +21,8 @@
|
||||||
#define __GST_FAAC_H__
|
#define __GST_FAAC_H__
|
||||||
|
|
||||||
#include <gst/gst.h>
|
#include <gst/gst.h>
|
||||||
#include <gst/base/gstadapter.h>
|
#include <gst/audio/gstaudioencoder.h>
|
||||||
#include <gst/audio/audio.h>
|
|
||||||
#include <faac.h>
|
#include <faac.h>
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
@ -42,41 +42,37 @@ typedef struct _GstFaac GstFaac;
|
||||||
typedef struct _GstFaacClass GstFaacClass;
|
typedef struct _GstFaacClass GstFaacClass;
|
||||||
|
|
||||||
struct _GstFaac {
|
struct _GstFaac {
|
||||||
GstElement element;
|
GstAudioEncoder element;
|
||||||
|
|
||||||
/* pads */
|
|
||||||
GstPad *srcpad, *sinkpad;
|
|
||||||
|
|
||||||
/* stream properties */
|
/* stream properties */
|
||||||
gint samplerate,
|
gint samplerate,
|
||||||
channels,
|
channels,
|
||||||
format,
|
format,
|
||||||
bps,
|
bps;
|
||||||
quality,
|
|
||||||
bitrate,
|
/* input frame size */
|
||||||
brtype,
|
gulong samples;
|
||||||
|
/* required output buffer size */
|
||||||
|
gulong bytes;
|
||||||
|
|
||||||
|
/* negotiated */
|
||||||
|
gint mpegversion, outputformat;
|
||||||
|
|
||||||
|
/* properties */
|
||||||
|
gint bitrate,
|
||||||
profile,
|
profile,
|
||||||
mpegversion,
|
quality,
|
||||||
shortctl,
|
brtype,
|
||||||
outputformat;
|
shortctl;
|
||||||
gboolean tns,
|
gboolean tns,
|
||||||
midside;
|
midside;
|
||||||
gulong bytes,
|
|
||||||
samples;
|
|
||||||
|
|
||||||
/* FAAC object */
|
/* FAAC object */
|
||||||
faacEncHandle handle;
|
faacEncHandle handle;
|
||||||
|
|
||||||
/* cache of the input */
|
|
||||||
GstAdapter *adapter;
|
|
||||||
/* offset of data to be encoded next */
|
|
||||||
guint offset;
|
|
||||||
/* ts for last buffer */
|
|
||||||
GstClockTime next_ts;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _GstFaacClass {
|
struct _GstFaacClass {
|
||||||
GstElementClass parent_class;
|
GstAudioEncoderClass parent_class;
|
||||||
};
|
};
|
||||||
|
|
||||||
GType gst_faac_get_type (void);
|
GType gst_faac_get_type (void);
|
||||||
|
|
Loading…
Reference in a new issue