gstreamer/ext/amrnb/amrnbdec.c

308 lines
8.7 KiB
C
Raw Normal View History

/* GStreamer Adaptive Multi-Rate Narrow-Band (AMR-NB) plugin
* Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* 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-amrnbdec
* @title: amrnbdec
* @see_also: #GstAmrnbEnc, #GstAmrParse
*
* AMR narrowband decoder based on the
* [opencore codec implementation](http://sourceforge.net/projects/opencore-amr).
*
* ## Example launch line
* |[
* gst-launch-1.0 filesrc location=abc.amr ! amrparse ! amrnbdec ! audioconvert ! audioresample ! autoaudiosink
* ]|
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "amrnbdec.h"
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/AMR, " "rate = (int) 8000, " "channels = (int) 1")
);
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
2011-10-30 15:04:22 +00:00
GST_STATIC_CAPS ("audio/x-raw, format = (string) " GST_AUDIO_NE (S16) ", "
"layout = (string) interleaved, "
"rate = (int) 8000," "channels = (int) 1")
);
GST_DEBUG_CATEGORY_STATIC (gst_amrnbdec_debug);
#define GST_CAT_DEFAULT gst_amrnbdec_debug
static const gint block_size_if1[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5,
0, 0, 0, 0, 0, 0, 0
};
static const gint block_size_if2[16] = { 12, 13, 15, 17, 18, 20, 25, 30, 5,
0, 0, 0, 0, 0, 0, 0
};
static GType
gst_amrnb_variant_get_type (void)
{
static GType gst_amrnb_variant_type = 0;
static const GEnumValue gst_amrnb_variant[] = {
{GST_AMRNB_VARIANT_IF1, "IF1", "IF1"},
{GST_AMRNB_VARIANT_IF2, "IF2", "IF2"},
{0, NULL, NULL},
};
if (!gst_amrnb_variant_type) {
gst_amrnb_variant_type =
g_enum_register_static ("GstAmrnbVariant", gst_amrnb_variant);
}
return gst_amrnb_variant_type;
}
#define GST_AMRNB_VARIANT_TYPE (gst_amrnb_variant_get_type())
#define VARIANT_DEFAULT GST_AMRNB_VARIANT_IF1
enum
{
PROP_0,
PROP_VARIANT
};
static void gst_amrnbdec_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_amrnbdec_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
2011-10-03 15:19:02 +00:00
static gboolean gst_amrnbdec_start (GstAudioDecoder * dec);
static gboolean gst_amrnbdec_stop (GstAudioDecoder * dec);
static gboolean gst_amrnbdec_set_format (GstAudioDecoder * dec, GstCaps * caps);
static GstFlowReturn gst_amrnbdec_parse (GstAudioDecoder * dec,
GstAdapter * adapter, gint * offset, gint * length);
2011-10-03 15:19:02 +00:00
static GstFlowReturn gst_amrnbdec_handle_frame (GstAudioDecoder * dec,
GstBuffer * buffer);
2011-10-04 15:35:40 +00:00
#define gst_amrnbdec_parent_class parent_class
G_DEFINE_TYPE (GstAmrnbDec, gst_amrnbdec, GST_TYPE_AUDIO_DECODER);
static void
gst_amrnbdec_class_init (GstAmrnbDecClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
object_class->set_property = gst_amrnbdec_set_property;
object_class->get_property = gst_amrnbdec_get_property;
2011-10-04 15:35:40 +00:00
gst_element_class_add_static_pad_template (element_class, &sink_template);
gst_element_class_add_static_pad_template (element_class, &src_template);
2011-10-04 15:35:40 +00:00
gst_element_class_set_static_metadata (element_class, "AMR-NB audio decoder",
2011-10-04 15:35:40 +00:00
"Codec/Decoder/Audio",
"Adaptive Multi-Rate Narrow-Band audio decoder",
"GStreamer maintainers <gstreamer-devel@lists.freedesktop.org>");
2011-10-04 15:35:40 +00:00
2011-10-03 15:19:02 +00:00
base_class->start = GST_DEBUG_FUNCPTR (gst_amrnbdec_start);
base_class->stop = GST_DEBUG_FUNCPTR (gst_amrnbdec_stop);
base_class->set_format = GST_DEBUG_FUNCPTR (gst_amrnbdec_set_format);
base_class->parse = GST_DEBUG_FUNCPTR (gst_amrnbdec_parse);
base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_amrnbdec_handle_frame);
g_object_class_install_property (object_class, PROP_VARIANT,
g_param_spec_enum ("variant", "Variant",
"The decoder variant", GST_AMRNB_VARIANT_TYPE,
VARIANT_DEFAULT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2011-10-04 15:35:40 +00:00
GST_DEBUG_CATEGORY_INIT (gst_amrnbdec_debug, "amrnbdec", 0,
"AMR-NB audio decoder");
gst_type_mark_as_plugin_api (GST_AMRNB_VARIANT_TYPE, 0);
}
static void
2011-10-04 15:35:40 +00:00
gst_amrnbdec_init (GstAmrnbDec * amrnbdec)
{
gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (amrnbdec), TRUE);
gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
(amrnbdec), TRUE);
GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (amrnbdec));
}
2011-10-03 15:19:02 +00:00
static gboolean
gst_amrnbdec_start (GstAudioDecoder * dec)
{
2011-10-03 15:19:02 +00:00
GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
2011-10-03 15:19:02 +00:00
GST_DEBUG_OBJECT (dec, "start");
if (!(amrnbdec->handle = Decoder_Interface_init ()))
return FALSE;
2011-10-03 15:19:02 +00:00
amrnbdec->rate = 0;
amrnbdec->channels = 0;
2011-10-03 15:19:02 +00:00
return TRUE;
}
2011-10-03 15:19:02 +00:00
static gboolean
gst_amrnbdec_stop (GstAudioDecoder * dec)
{
2011-10-03 15:19:02 +00:00
GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
2011-10-03 15:19:02 +00:00
GST_DEBUG_OBJECT (dec, "stop");
Decoder_Interface_exit (amrnbdec->handle);
2011-10-03 15:19:02 +00:00
return TRUE;
}
static void
gst_amrnbdec_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstAmrnbDec *self = GST_AMRNBDEC (object);
switch (prop_id) {
case PROP_VARIANT:
self->variant = g_value_get_enum (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
return;
}
static void
gst_amrnbdec_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstAmrnbDec *self = GST_AMRNBDEC (object);
switch (prop_id) {
case PROP_VARIANT:
g_value_set_enum (value, self->variant);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
return;
}
static gboolean
2011-10-03 15:19:02 +00:00
gst_amrnbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
{
GstStructure *structure;
GstAmrnbDec *amrnbdec;
GstAudioInfo info;
2011-10-03 15:19:02 +00:00
amrnbdec = GST_AMRNBDEC (dec);
structure = gst_caps_get_structure (caps, 0);
/* get channel count */
gst_structure_get_int (structure, "channels", &amrnbdec->channels);
gst_structure_get_int (structure, "rate", &amrnbdec->rate);
/* create reverse caps */
gst_audio_info_init (&info);
gst_audio_info_set_format (&info,
GST_AUDIO_FORMAT_S16, amrnbdec->rate, amrnbdec->channels, NULL);
return gst_audio_decoder_set_output_format (dec, &info);
}
2011-10-03 15:19:02 +00:00
static GstFlowReturn
gst_amrnbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
gint * offset, gint * length)
{
2011-10-03 15:19:02 +00:00
GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
guint8 head[1];
2011-10-03 15:19:02 +00:00
guint size;
gboolean sync, eos;
gint block, mode;
2011-10-03 15:19:02 +00:00
size = gst_adapter_available (adapter);
if (size < 1)
return GST_FLOW_ERROR;
2011-10-03 15:19:02 +00:00
gst_audio_decoder_get_parse_state (dec, &sync, &eos);
2011-10-04 15:35:40 +00:00
2011-10-03 15:19:02 +00:00
/* need to peek data to get the size */
gst_adapter_copy (adapter, head, 0, 1);
2011-10-04 15:35:40 +00:00
2011-10-03 15:19:02 +00:00
/* get size */
switch (amrnbdec->variant) {
case GST_AMRNB_VARIANT_IF1:
mode = (head[0] >> 3) & 0x0F;
2011-10-03 15:19:02 +00:00
block = block_size_if1[mode] + 1;
break;
2011-10-03 15:19:02 +00:00
case GST_AMRNB_VARIANT_IF2:
mode = head[0] & 0x0F;
2011-10-03 15:19:02 +00:00
block = block_size_if2[mode] + 1;
break;
default:
2011-10-03 15:19:02 +00:00
g_assert_not_reached ();
return GST_FLOW_ERROR;
break;
}
2011-10-03 15:19:02 +00:00
GST_DEBUG_OBJECT (amrnbdec, "mode %d, block %d", mode, block);
if (block > size)
2012-01-25 12:58:54 +00:00
return GST_FLOW_EOS;
2011-10-03 15:19:02 +00:00
*offset = 0;
*length = block;
return GST_FLOW_OK;
}
static GstFlowReturn
2011-10-03 15:19:02 +00:00
gst_amrnbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
{
GstAmrnbDec *amrnbdec;
2012-01-25 06:24:59 +00:00
GstMapInfo inmap, outmap;
2011-10-03 15:19:02 +00:00
GstBuffer *out;
2011-10-03 15:19:02 +00:00
amrnbdec = GST_AMRNBDEC (dec);
/* no fancy flushing */
if (!buffer || !gst_buffer_get_size (buffer))
2011-10-03 15:19:02 +00:00
return GST_FLOW_OK;
2012-01-25 06:24:59 +00:00
gst_buffer_map (buffer, &inmap, GST_MAP_READ);
2011-10-03 15:19:02 +00:00
/* get output */
out = gst_buffer_new_and_alloc (160 * 2);
/* decode */
2012-01-25 06:24:59 +00:00
gst_buffer_map (out, &outmap, GST_MAP_WRITE);
Decoder_Interface_Decode (amrnbdec->handle, inmap.data,
(gint16 *) outmap.data, 0);
gst_buffer_unmap (out, &outmap);
2012-01-25 06:24:59 +00:00
gst_buffer_unmap (buffer, &inmap);
2011-10-03 15:19:02 +00:00
return gst_audio_decoder_finish_frame (dec, out, 1);
}