2009-07-27 18:12:20 +00:00
|
|
|
/* 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., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-amrwbdec
|
|
|
|
* @see_also: #GstAmrwbEnc
|
|
|
|
*
|
|
|
|
* AMR wideband decoder based on the
|
|
|
|
* <ulink url="http://sourceforge.net/projects/opencore-amr">opencore codec implementation</ulink>.
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example launch line</title>
|
|
|
|
* |[
|
|
|
|
* gst-launch filesrc location=abc.amr ! amrparse ! amrwbdec ! audioresample ! audioconvert ! alsasink
|
|
|
|
* ]|
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "amrwbdec.h"
|
|
|
|
|
|
|
|
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("audio/AMR-WB, "
|
|
|
|
"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/x-raw-int, "
|
|
|
|
"width = (int) 16, "
|
|
|
|
"depth = (int) 16, "
|
|
|
|
"signed = (boolean) TRUE, "
|
|
|
|
"endianness = (int) BYTE_ORDER, "
|
|
|
|
"rate = (int) 16000, " "channels = (int) 1")
|
|
|
|
);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_amrwbdec_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_amrwbdec_debug
|
|
|
|
|
2010-04-27 08:39:11 +00:00
|
|
|
#define L_FRAME16k 320 /* Frame size at 16kHz */
|
|
|
|
|
2009-07-27 18:12:20 +00:00
|
|
|
static const unsigned char block_size[16] =
|
|
|
|
{ 18, 24, 33, 37, 41, 47, 51, 59, 61,
|
2011-02-07 18:58:45 +00:00
|
|
|
6, 0, 0, 0, 0, 1, 1
|
2009-07-27 18:12:20 +00:00
|
|
|
};
|
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
static gboolean gst_amrwbdec_start (GstAudioDecoder * dec);
|
|
|
|
static gboolean gst_amrwbdec_stop (GstAudioDecoder * dec);
|
|
|
|
static gboolean gst_amrwbdec_set_format (GstAudioDecoder * dec, GstCaps * caps);
|
|
|
|
static gboolean gst_amrwbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
|
|
|
|
gint * offset, gint * length);
|
|
|
|
static GstFlowReturn gst_amrwbdec_handle_frame (GstAudioDecoder * dec,
|
|
|
|
GstBuffer * buffer);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
|
|
|
#define _do_init(bla) \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_amrwbdec_debug, "amrwbdec", 0, "AMR-WB audio decoder");
|
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
GST_BOILERPLATE_FULL (GstAmrwbDec, gst_amrwbdec, GstAudioDecoder,
|
|
|
|
GST_TYPE_AUDIO_DECODER, _do_init);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_amrwbdec_base_init (gpointer klass)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
2011-11-28 13:10:01 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&sink_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class, &src_template);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
2010-03-18 14:53:14 +00:00
|
|
|
gst_element_class_set_details_simple (element_class, "AMR-WB audio decoder",
|
|
|
|
"Codec/Decoder/Audio",
|
|
|
|
"Adaptive Multi-Rate Wideband audio decoder",
|
|
|
|
"Renato Araujo <renato.filho@indt.org.br>");
|
2009-07-27 18:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_amrwbdec_class_init (GstAmrwbDecClass * klass)
|
|
|
|
{
|
2011-10-05 10:05:34 +00:00
|
|
|
GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
base_class->start = GST_DEBUG_FUNCPTR (gst_amrwbdec_start);
|
|
|
|
base_class->stop = GST_DEBUG_FUNCPTR (gst_amrwbdec_stop);
|
|
|
|
base_class->set_format = GST_DEBUG_FUNCPTR (gst_amrwbdec_set_format);
|
|
|
|
base_class->parse = GST_DEBUG_FUNCPTR (gst_amrwbdec_parse);
|
|
|
|
base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_amrwbdec_handle_frame);
|
2009-07-27 18:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_amrwbdec_init (GstAmrwbDec * amrwbdec, GstAmrwbDecClass * klass)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
static gboolean
|
|
|
|
gst_amrwbdec_start (GstAudioDecoder * dec)
|
2009-07-27 18:12:20 +00:00
|
|
|
{
|
2011-10-05 10:05:34 +00:00
|
|
|
GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "start");
|
|
|
|
if (!(amrwbdec->handle = D_IF_init ()))
|
|
|
|
return FALSE;
|
2009-07-27 18:12:20 +00:00
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
amrwbdec->rate = 0;
|
|
|
|
amrwbdec->channels = 0;
|
2009-07-27 18:12:20 +00:00
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
return TRUE;
|
2009-07-27 18:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-10-05 10:05:34 +00:00
|
|
|
gst_amrwbdec_stop (GstAudioDecoder * dec)
|
|
|
|
{
|
|
|
|
GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (dec, "stop");
|
|
|
|
D_IF_exit (amrwbdec->handle);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_amrwbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
|
2009-07-27 18:12:20 +00:00
|
|
|
{
|
|
|
|
GstStructure *structure;
|
|
|
|
GstAmrwbDec *amrwbdec;
|
|
|
|
GstCaps *copy;
|
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
amrwbdec = GST_AMRWBDEC (dec);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
|
|
|
|
/* get channel count */
|
|
|
|
gst_structure_get_int (structure, "channels", &amrwbdec->channels);
|
|
|
|
gst_structure_get_int (structure, "rate", &amrwbdec->rate);
|
|
|
|
|
|
|
|
/* create reverse caps */
|
|
|
|
copy = gst_caps_new_simple ("audio/x-raw-int",
|
|
|
|
"channels", G_TYPE_INT, amrwbdec->channels,
|
|
|
|
"width", G_TYPE_INT, 16,
|
|
|
|
"depth", G_TYPE_INT, 16,
|
|
|
|
"endianness", G_TYPE_INT, G_BYTE_ORDER,
|
|
|
|
"rate", G_TYPE_INT, amrwbdec->rate, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
|
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (amrwbdec), copy);
|
2009-07-27 18:12:20 +00:00
|
|
|
gst_caps_unref (copy);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_amrwbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
|
|
|
|
gint * offset, gint * length)
|
2009-07-27 18:12:20 +00:00
|
|
|
{
|
2011-10-05 10:05:34 +00:00
|
|
|
GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
|
|
|
|
const guint8 *data;
|
|
|
|
guint size;
|
|
|
|
gboolean sync, eos;
|
|
|
|
gint block, mode;
|
|
|
|
|
|
|
|
size = gst_adapter_available (adapter);
|
|
|
|
g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
|
|
|
|
|
|
|
|
gst_audio_decoder_get_parse_state (dec, &sync, &eos);
|
|
|
|
|
|
|
|
/* need to peek data to get the size */
|
2012-01-17 10:55:59 +00:00
|
|
|
if (size < 1)
|
2011-10-05 10:05:34 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
|
|
|
data = gst_adapter_peek (adapter, 1);
|
|
|
|
mode = (data[0] >> 3) & 0x0F;
|
|
|
|
block = block_size[mode];
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (amrwbdec, "mode %d, block %d", mode, block);
|
|
|
|
|
|
|
|
if (block) {
|
2012-01-17 10:55:59 +00:00
|
|
|
if (block > size)
|
|
|
|
return GST_FLOW_UNEXPECTED;
|
2011-10-05 10:05:34 +00:00
|
|
|
*offset = 0;
|
|
|
|
*length = block;
|
|
|
|
} else {
|
|
|
|
/* no frame yet, skip one byte */
|
|
|
|
GST_LOG_OBJECT (amrwbdec, "skipping byte");
|
|
|
|
*offset = 1;
|
|
|
|
return GST_FLOW_UNEXPECTED;
|
2009-07-27 18:12:20 +00:00
|
|
|
}
|
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
return GST_FLOW_OK;
|
2009-07-27 18:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-10-05 10:05:34 +00:00
|
|
|
gst_amrwbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
|
2009-07-27 18:12:20 +00:00
|
|
|
{
|
|
|
|
GstAmrwbDec *amrwbdec;
|
2011-10-05 10:05:34 +00:00
|
|
|
GstBuffer *out;
|
|
|
|
const guint8 *data;
|
|
|
|
|
|
|
|
amrwbdec = GST_AMRWBDEC (dec);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
/* no fancy flushing */
|
|
|
|
if (!buffer || !GST_BUFFER_SIZE (buffer))
|
|
|
|
return GST_FLOW_OK;
|
2009-07-27 18:12:20 +00:00
|
|
|
|
|
|
|
if (amrwbdec->rate == 0 || amrwbdec->channels == 0)
|
|
|
|
goto not_negotiated;
|
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
/* the library seems to write into the source data, hence the copy. */
|
|
|
|
/* should be no problem */
|
|
|
|
data = GST_BUFFER_DATA (buffer);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
/* get output */
|
|
|
|
out = gst_buffer_new_and_alloc (sizeof (gint16) * L_FRAME16k);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
/* decode */
|
|
|
|
D_IF_decode (amrwbdec->handle, (unsigned char *) data,
|
|
|
|
(Word16 *) GST_BUFFER_DATA (out), _good_frame);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
2011-10-05 10:05:34 +00:00
|
|
|
/* send out */
|
|
|
|
return gst_audio_decoder_finish_frame (dec, out, 1);
|
2009-07-27 18:12:20 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
not_negotiated:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (amrwbdec, STREAM, TYPE_NOT_FOUND, (NULL),
|
|
|
|
("Decoder is not initialized"));
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
|
|
|
}
|