gstreamer/ext/voamrwbenc/gstvoamrwbenc.c

311 lines
8.7 KiB
C
Raw Normal View History

/* GStreamer Adaptive Multi-Rate Wide-Band (AMR-WB) plugin
* Copyright (C) 2006 Edgard Lima <edgard.lima@gmail.com>
*
* 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
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:element-voamrwbenc
* @title: voamrwbenc
* @see_also: #GstAmrWbDec, #GstAmrWbParse
*
* AMR wideband encoder based on the
* [reference codec implementation](http://www.penguin.cz/~utx/amr).
*
* ## Example launch line
docs/plugins/: docs/plugins/inspect/plugin-mythtv.xml Original commit message from CVS: * docs/plugins/Makefile.am: * docs/plugins/gst-plugins-bad-plugins-docs.sgml: * docs/plugins/gst-plugins-bad-plugins-sections.txt: * docs/plugins/gst-plugins-bad-plugins.args: * docs/plugins/gst-plugins-bad-plugins.hierarchy: * docs/plugins/gst-plugins-bad-plugins.interfaces: * docs/plugins/gst-plugins-bad-plugins.prerequisites: * docs/plugins/gst-plugins-bad-plugins.signals: * docs/plugins/inspect/plugin-alsaspdif.xml: * docs/plugins/inspect/plugin-amrwb.xml: * docs/plugins/inspect/plugin-app.xml: * docs/plugins/inspect/plugin-bayer.xml: * docs/plugins/inspect/plugin-bz2.xml: * docs/plugins/inspect/plugin-cdaudio.xml: * docs/plugins/inspect/plugin-cdxaparse.xml: * docs/plugins/inspect/plugin-dtsdec.xml: * docs/plugins/inspect/plugin-dvb.xml: * docs/plugins/inspect/plugin-dvdspu.xml: * docs/plugins/inspect/plugin-faac.xml: * docs/plugins/inspect/plugin-faad.xml: * docs/plugins/inspect/plugin-fbdevsink.xml: * docs/plugins/inspect/plugin-festival.xml: * docs/plugins/inspect/plugin-filter.xml: * docs/plugins/inspect/plugin-flvdemux.xml: * docs/plugins/inspect/plugin-freeze.xml: * docs/plugins/inspect/plugin-gsm.xml: * docs/plugins/inspect/plugin-gstinterlace.xml: * docs/plugins/inspect/plugin-gstrtpmanager.xml: * docs/plugins/inspect/plugin-h264parse.xml: * docs/plugins/inspect/plugin-interleave.xml: * docs/plugins/inspect/plugin-jack.xml: * docs/plugins/inspect/plugin-ladspa.xml: * docs/plugins/inspect/plugin-metadata.xml: * docs/plugins/inspect/plugin-mms.xml: * docs/plugins/inspect/plugin-modplug.xml: * docs/plugins/inspect/plugin-mpeg2enc.xml: * docs/plugins/inspect/plugin-mpeg4videoparse.xml: * docs/plugins/inspect/plugin-mpegtsparse.xml: * docs/plugins/inspect/plugin-mpegvideoparse.xml: * docs/plugins/inspect/plugin-musepack.xml: * docs/plugins/inspect/plugin-musicbrainz.xml: * docs/plugins/inspect/plugin-mve.xml: * docs/plugins/inspect/plugin-mythtv.xml * docs/plugins/inspect/plugin-nas.xml: * docs/plugins/inspect/plugin-neon.xml: * docs/plugins/inspect/plugin-nsfdec.xml: * docs/plugins/inspect/plugin-nuvdemux.xml: * docs/plugins/inspect/plugin-oss4.xml * docs/plugins/inspect/plugin-rawparse.xml: * docs/plugins/inspect/plugin-real.xml: * docs/plugins/inspect/plugin-replaygain.xml: * docs/plugins/inspect/plugin-rfbsrc.xml: * docs/plugins/inspect/plugin-sdl.xml: * docs/plugins/inspect/plugin-sdp.xml: * docs/plugins/inspect/plugin-selector.xml: * docs/plugins/inspect/plugin-sndfile.xml: * docs/plugins/inspect/plugin-soundtouch.xml: * docs/plugins/inspect/plugin-spcdec.xml: * docs/plugins/inspect/plugin-speed.xml: * docs/plugins/inspect/plugin-speexresample.xml: * docs/plugins/inspect/plugin-stereo.xml: * docs/plugins/inspect/plugin-subenc.xml * docs/plugins/inspect/plugin-timidity.xml: * docs/plugins/inspect/plugin-tta.xml: * docs/plugins/inspect/plugin-vcdsrc.xml: * docs/plugins/inspect/plugin-videosignal.xml: * docs/plugins/inspect/plugin-vmnc.xml: * docs/plugins/inspect/plugin-wildmidi.xml: * docs/plugins/inspect/plugin-x264.xml: * docs/plugins/inspect/plugin-xvid.xml: * docs/plugins/inspect/plugin-y4menc.xml: * ext/amrwb/gstamrwbdec.c: * ext/amrwb/gstamrwbenc.c: * ext/amrwb/gstamrwbparse.c: * ext/dc1394/gstdc1394.c: * ext/directfb/dfbvideosink.c: * ext/ivorbis/vorbisdec.c: * ext/jack/gstjackaudiosink.c: * ext/mpeg2enc/gstmpeg2enc.cc: * ext/mplex/gstmplex.cc: * ext/musicbrainz/gsttrm.c: * ext/mythtv/gstmythtvsrc.c: * ext/theora/theoradec.c: * ext/timidity/gsttimidity.c: * ext/timidity/gstwildmidi.c: * gst-libs/gst/app/gstappsink.c: * gst/deinterlace/gstdeinterlace.c: * gst/dvdspu/gstdvdspu.c: * gst/festival/gstfestival.c: * gst/freeze/gstfreeze.c: * gst/interleave/deinterleave.c: * gst/interleave/interleave.c: * gst/modplug/gstmodplug.cc: * gst/nuvdemux/gstnuvdemux.c: Add missing elements to docs. Fix doc-markup: use convinience syntax for examples (produces valid docbook), add several refsec2 when we have several titles. Fix some types.
2008-06-13 11:59:23 +00:00
* |[
* gst-launch filesrc location=abc.wav ! wavparse ! audioresample ! audioconvert ! voamrwbenc ! filesink location=abc.amr
docs/plugins/: docs/plugins/inspect/plugin-mythtv.xml Original commit message from CVS: * docs/plugins/Makefile.am: * docs/plugins/gst-plugins-bad-plugins-docs.sgml: * docs/plugins/gst-plugins-bad-plugins-sections.txt: * docs/plugins/gst-plugins-bad-plugins.args: * docs/plugins/gst-plugins-bad-plugins.hierarchy: * docs/plugins/gst-plugins-bad-plugins.interfaces: * docs/plugins/gst-plugins-bad-plugins.prerequisites: * docs/plugins/gst-plugins-bad-plugins.signals: * docs/plugins/inspect/plugin-alsaspdif.xml: * docs/plugins/inspect/plugin-amrwb.xml: * docs/plugins/inspect/plugin-app.xml: * docs/plugins/inspect/plugin-bayer.xml: * docs/plugins/inspect/plugin-bz2.xml: * docs/plugins/inspect/plugin-cdaudio.xml: * docs/plugins/inspect/plugin-cdxaparse.xml: * docs/plugins/inspect/plugin-dtsdec.xml: * docs/plugins/inspect/plugin-dvb.xml: * docs/plugins/inspect/plugin-dvdspu.xml: * docs/plugins/inspect/plugin-faac.xml: * docs/plugins/inspect/plugin-faad.xml: * docs/plugins/inspect/plugin-fbdevsink.xml: * docs/plugins/inspect/plugin-festival.xml: * docs/plugins/inspect/plugin-filter.xml: * docs/plugins/inspect/plugin-flvdemux.xml: * docs/plugins/inspect/plugin-freeze.xml: * docs/plugins/inspect/plugin-gsm.xml: * docs/plugins/inspect/plugin-gstinterlace.xml: * docs/plugins/inspect/plugin-gstrtpmanager.xml: * docs/plugins/inspect/plugin-h264parse.xml: * docs/plugins/inspect/plugin-interleave.xml: * docs/plugins/inspect/plugin-jack.xml: * docs/plugins/inspect/plugin-ladspa.xml: * docs/plugins/inspect/plugin-metadata.xml: * docs/plugins/inspect/plugin-mms.xml: * docs/plugins/inspect/plugin-modplug.xml: * docs/plugins/inspect/plugin-mpeg2enc.xml: * docs/plugins/inspect/plugin-mpeg4videoparse.xml: * docs/plugins/inspect/plugin-mpegtsparse.xml: * docs/plugins/inspect/plugin-mpegvideoparse.xml: * docs/plugins/inspect/plugin-musepack.xml: * docs/plugins/inspect/plugin-musicbrainz.xml: * docs/plugins/inspect/plugin-mve.xml: * docs/plugins/inspect/plugin-mythtv.xml * docs/plugins/inspect/plugin-nas.xml: * docs/plugins/inspect/plugin-neon.xml: * docs/plugins/inspect/plugin-nsfdec.xml: * docs/plugins/inspect/plugin-nuvdemux.xml: * docs/plugins/inspect/plugin-oss4.xml * docs/plugins/inspect/plugin-rawparse.xml: * docs/plugins/inspect/plugin-real.xml: * docs/plugins/inspect/plugin-replaygain.xml: * docs/plugins/inspect/plugin-rfbsrc.xml: * docs/plugins/inspect/plugin-sdl.xml: * docs/plugins/inspect/plugin-sdp.xml: * docs/plugins/inspect/plugin-selector.xml: * docs/plugins/inspect/plugin-sndfile.xml: * docs/plugins/inspect/plugin-soundtouch.xml: * docs/plugins/inspect/plugin-spcdec.xml: * docs/plugins/inspect/plugin-speed.xml: * docs/plugins/inspect/plugin-speexresample.xml: * docs/plugins/inspect/plugin-stereo.xml: * docs/plugins/inspect/plugin-subenc.xml * docs/plugins/inspect/plugin-timidity.xml: * docs/plugins/inspect/plugin-tta.xml: * docs/plugins/inspect/plugin-vcdsrc.xml: * docs/plugins/inspect/plugin-videosignal.xml: * docs/plugins/inspect/plugin-vmnc.xml: * docs/plugins/inspect/plugin-wildmidi.xml: * docs/plugins/inspect/plugin-x264.xml: * docs/plugins/inspect/plugin-xvid.xml: * docs/plugins/inspect/plugin-y4menc.xml: * ext/amrwb/gstamrwbdec.c: * ext/amrwb/gstamrwbenc.c: * ext/amrwb/gstamrwbparse.c: * ext/dc1394/gstdc1394.c: * ext/directfb/dfbvideosink.c: * ext/ivorbis/vorbisdec.c: * ext/jack/gstjackaudiosink.c: * ext/mpeg2enc/gstmpeg2enc.cc: * ext/mplex/gstmplex.cc: * ext/musicbrainz/gsttrm.c: * ext/mythtv/gstmythtvsrc.c: * ext/theora/theoradec.c: * ext/timidity/gsttimidity.c: * ext/timidity/gstwildmidi.c: * gst-libs/gst/app/gstappsink.c: * gst/deinterlace/gstdeinterlace.c: * gst/dvdspu/gstdvdspu.c: * gst/festival/gstfestival.c: * gst/freeze/gstfreeze.c: * gst/interleave/deinterleave.c: * gst/interleave/interleave.c: * gst/modplug/gstmodplug.cc: * gst/nuvdemux/gstnuvdemux.c: Add missing elements to docs. Fix doc-markup: use convinience syntax for examples (produces valid docbook), add several refsec2 when we have several titles. Fix some types.
2008-06-13 11:59:23 +00:00
* ]|
* Please note that the above stream misses the header, that is needed to play
* the stream.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstvoamrwbenc.h"
#define MR660 0
#define MR885 1
#define MR1265 2
#define MR1425 2
#define MR1585 3
#define MR1825 4
#define MR1985 5
#define MR2305 6
#define MR2385 7
#define MRDTX 8
#define L_FRAME16k 320 /* Frame size at 16kHz */
static GType
gst_voamrwbenc_bandmode_get_type (void)
{
static GType gst_voamrwbenc_bandmode_type = 0;
2015-01-21 09:17:04 +00:00
static const GEnumValue gst_voamrwbenc_bandmode[] = {
{MR660, "MR660", "MR660"},
{MR885, "MR885", "MR885"},
{MR1265, "MR1265", "MR1265"},
{MR1425, "MR1425", "MR1425"},
{MR1585, "MR1585", "MR1585"},
{MR1825, "MR1825", "MR1825"},
{MR1985, "MR1985", "MR1985"},
{MR2305, "MR2305", "MR2305"},
{MR2385, "MR2385", "MR2385"},
{MRDTX, "MRDTX", "MRDTX"},
{0, NULL, NULL},
};
if (!gst_voamrwbenc_bandmode_type) {
gst_voamrwbenc_bandmode_type =
g_enum_register_static ("GstVoAmrWbEncBandMode",
gst_voamrwbenc_bandmode);
}
return gst_voamrwbenc_bandmode_type;
}
#define GST_VOAMRWBENC_BANDMODE_TYPE (gst_voamrwbenc_bandmode_get_type())
#define BANDMODE_DEFAULT MR660
enum
{
PROP_0,
PROP_BANDMODE
};
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
2012-01-11 11:24:15 +00:00
GST_STATIC_CAPS ("audio/x-raw, "
"format = (string) " GST_AUDIO_NE (S16) ", "
2012-01-11 11:24:15 +00:00
"layout = (string) interleaved, "
"rate = (int) 16000, " "channels = (int) 1")
);
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/AMR-WB, "
"rate = (int) 16000, " "channels = (int) 1")
);
GST_DEBUG_CATEGORY_STATIC (gst_voamrwbenc_debug);
#define GST_CAT_DEFAULT gst_voamrwbenc_debug
2011-11-17 22:03:05 +00:00
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);
2012-01-11 11:24:15 +00:00
G_DEFINE_TYPE (GstVoAmrWbEnc, gst_voamrwbenc, GST_TYPE_AUDIO_ENCODER);
static void
gst_voamrwbenc_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstVoAmrWbEnc *self = GST_VOAMRWBENC (object);
switch (prop_id) {
case PROP_BANDMODE:
self->bandmode = g_value_get_enum (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
return;
}
static void
gst_voamrwbenc_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstVoAmrWbEnc *self = GST_VOAMRWBENC (object);
switch (prop_id) {
case PROP_BANDMODE:
g_value_set_enum (value, self->bandmode);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
return;
}
static void
2012-01-11 11:24:15 +00:00
gst_voamrwbenc_class_init (GstVoAmrWbEncClass * klass)
{
2012-01-11 11:24:15 +00:00
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
2012-01-11 11:24:15 +00:00
GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
object_class->set_property = gst_voamrwbenc_set_property;
object_class->get_property = gst_voamrwbenc_get_property;
gst_element_class_add_static_pad_template (element_class, &sink_template);
gst_element_class_add_static_pad_template (element_class, &src_template);
gst_element_class_set_static_metadata (element_class, "AMR-WB audio encoder",
"Codec/Encoder/Audio",
"Adaptive Multi-Rate Wideband audio encoder",
"Renato Araujo <renato.filho@indt.org.br>");
2011-11-17 22:03:05 +00:00
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_param_spec_enum ("band-mode", "Band Mode",
"Encoding Band Mode (Kbps)", GST_VOAMRWBENC_BANDMODE_TYPE,
BANDMODE_DEFAULT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2012-01-28 19:01:35 +00:00
GST_DEBUG_CATEGORY_INIT (gst_voamrwbenc_debug, "voamrwbenc", 0,
"voamrwb encoder");
}
static void
2012-01-11 11:24:15 +00:00
gst_voamrwbenc_init (GstVoAmrWbEnc * amrwbenc)
{
GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (amrwbenc));
/* init rest */
amrwbenc->handle = NULL;
amrwbenc->channels = 0;
amrwbenc->rate = 0;
}
2011-11-17 22:03:05 +00:00
static gboolean
gst_voamrwbenc_start (GstAudioEncoder * enc)
{
2011-11-17 22:03:05 +00:00
GstVoAmrWbEnc *voamrwbenc = GST_VOAMRWBENC (enc);
GST_DEBUG_OBJECT (enc, "start");
2011-11-17 22:03:05 +00:00
if (!(voamrwbenc->handle = E_IF_init ()))
return FALSE;
2011-11-17 22:03:05 +00:00
voamrwbenc->rate = 0;
voamrwbenc->channels = 0;
2011-11-17 22:03:05 +00:00
return TRUE;
}
static gboolean
2011-11-17 22:03:05 +00:00
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)
{
GstVoAmrWbEnc *amrwbenc;
GstCaps *copy;
2011-11-17 22:03:05 +00:00
amrwbenc = GST_VOAMRWBENC (benc);
/* get channel count */
2011-11-17 22:03:05 +00:00
amrwbenc->channels = GST_AUDIO_INFO_CHANNELS (info);
amrwbenc->rate = GST_AUDIO_INFO_RATE (info);
/* this is not wrong but will sound bad */
if (amrwbenc->channels != 1) {
GST_WARNING ("amrwbdec is only optimized for mono channels");
}
if (amrwbenc->rate != 16000) {
GST_WARNING ("amrwbdec is only optimized for 16000 Hz samplerate");
}
/* create reverse caps */
copy = gst_caps_new_simple ("audio/AMR-WB",
"channels", G_TYPE_INT, amrwbenc->channels,
"rate", G_TYPE_INT, amrwbenc->rate, NULL);
gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (amrwbenc), copy);
gst_caps_unref (copy);
2011-11-17 22:03:05 +00:00
/* 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;
}
static GstFlowReturn
2011-11-17 22:03:05 +00:00
gst_voamrwbenc_handle_frame (GstAudioEncoder * benc, GstBuffer * buffer)
{
GstVoAmrWbEnc *amrwbenc;
GstFlowReturn ret = GST_FLOW_OK;
const int buffer_size = sizeof (short) * L_FRAME16k;
2011-11-17 22:03:05 +00:00
GstBuffer *out;
gint outsize;
GstMapInfo map, omap;
2011-11-17 22:03:05 +00:00
amrwbenc = GST_VOAMRWBENC (benc);
2011-11-17 22:03:05 +00:00
g_return_val_if_fail (amrwbenc->handle, GST_FLOW_NOT_NEGOTIATED);
2011-11-17 22:03:05 +00:00
/* we don't deal with squeezing remnants, so simply discard those */
if (G_UNLIKELY (buffer == NULL)) {
GST_DEBUG_OBJECT (amrwbenc, "no data");
goto done;
}
gst_buffer_map (buffer, &map, GST_MAP_READ);
2012-01-11 11:24:15 +00:00
if (G_UNLIKELY (map.size < buffer_size)) {
GST_DEBUG_OBJECT (amrwbenc, "discarding trailing data %d", (gint) map.size);
gst_buffer_unmap (buffer, &map);
2011-11-17 22:03:05 +00:00
ret = gst_audio_encoder_finish_frame (benc, NULL, -1);
goto done;
}
2011-11-17 22:03:05 +00:00
out = gst_buffer_new_and_alloc (buffer_size);
gst_buffer_map (out, &omap, GST_MAP_WRITE);
2011-11-17 22:03:05 +00:00
/* encode */
outsize = E_IF_encode (amrwbenc->handle, amrwbenc->bandmode,
(const short *) map.data, (unsigned char *) omap.data, 0);
2011-11-17 22:03:05 +00:00
GST_LOG_OBJECT (amrwbenc, "encoded to %d bytes", outsize);
gst_buffer_unmap (out, &omap);
gst_buffer_unmap (buffer, &map);
gst_buffer_resize (out, 0, outsize);
2011-11-17 22:03:05 +00:00
ret = gst_audio_encoder_finish_frame (benc, out, L_FRAME16k);
2011-11-17 22:03:05 +00:00
done:
return ret;
}