gstreamer/ext/amrwbdec/amrwbdec.c

244 lines
6.8 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., 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
2011-10-04 15:12:35 +00:00
#include <gst/audio/audio.h>
#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,
2011-10-04 15:12:35 +00:00
GST_STATIC_CAPS ("audio/x-raw, "
"format = (string) " GST_AUDIO_NE (S16) ", "
"layout = (string) interleaved, "
"rate = (int) 16000, " "channels = (int) 1")
);
GST_DEBUG_CATEGORY_STATIC (gst_amrwbdec_debug);
#define GST_CAT_DEFAULT gst_amrwbdec_debug
#define L_FRAME16k 320 /* Frame size at 16kHz */
static const unsigned char block_size[16] =
{ 18, 24, 33, 37, 41, 47, 51, 59, 61,
6, 0, 0, 0, 0, 1, 1
};
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);
2011-10-04 15:12:35 +00:00
#define gst_amrwbdec_parent_class parent_class
G_DEFINE_TYPE (GstAmrwbDec, gst_amrwbdec, GST_TYPE_AUDIO_DECODER);
static void
2011-10-04 15:12:35 +00:00
gst_amrwbdec_class_init (GstAmrwbDecClass * klass)
{
GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sink_template));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&src_template));
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>");
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);
2011-10-04 15:12:35 +00:00
GST_DEBUG_CATEGORY_INIT (gst_amrwbdec_debug, "amrwbdec", 0,
"AMR-WB audio decoder");
}
static void
2011-10-04 15:12:35 +00:00
gst_amrwbdec_init (GstAmrwbDec * amrwbdec)
{
}
2011-10-05 10:05:34 +00:00
static gboolean
gst_amrwbdec_start (GstAudioDecoder * dec)
{
2011-10-05 10:05:34 +00:00
GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
2011-10-05 10:05:34 +00:00
GST_DEBUG_OBJECT (dec, "start");
if (!(amrwbdec->handle = D_IF_init ()))
return FALSE;
amrwbdec->rate = 0;
2011-10-05 10:05:34 +00:00
amrwbdec->channels = 0;
2011-10-05 10:05:34 +00:00
return TRUE;
}
static gboolean
2011-10-05 10:05:34 +00:00
gst_amrwbdec_stop (GstAudioDecoder * dec)
{
2011-10-05 10:05:34 +00:00
GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
2011-10-05 10:05:34 +00:00
GST_DEBUG_OBJECT (dec, "stop");
D_IF_exit (amrwbdec->handle);
2011-10-05 10:05:34 +00:00
return TRUE;
}
static gboolean
2011-10-05 10:05:34 +00:00
gst_amrwbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
{
GstStructure *structure;
GstAmrwbDec *amrwbdec;
GstAudioInfo info;
2011-10-05 10:05:34 +00:00
amrwbdec = GST_AMRWBDEC (dec);
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 */
gst_audio_info_init (&info);
gst_audio_info_set_format (&info,
GST_AUDIO_FORMAT_S16, amrwbdec->rate, amrwbdec->channels, NULL);
gst_audio_decoder_set_output_format (dec, &info);
return TRUE;
}
2011-10-05 10:05:34 +00:00
static GstFlowReturn
gst_amrwbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
gint * offset, gint * length)
{
2011-10-05 10:05:34 +00:00
GstAmrwbDec *amrwbdec = GST_AMRWBDEC (dec);
guint8 header[1];
2011-10-05 10:05:34 +00:00
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 */
if (size < 1)
2011-10-05 10:05:34 +00:00
return GST_FLOW_ERROR;
gst_adapter_copy (adapter, header, 0, 1);
mode = (header[0] >> 3) & 0x0F;
2011-10-05 10:05:34 +00:00
block = block_size[mode];
GST_DEBUG_OBJECT (amrwbdec, "mode %d, block %d", mode, block);
if (block) {
if (block > size)
2012-01-25 12:58:54 +00:00
return GST_FLOW_EOS;
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;
2012-01-03 14:27:42 +00:00
return GST_FLOW_EOS;
}
2011-10-05 10:05:34 +00:00
return GST_FLOW_OK;
}
static GstFlowReturn
2011-10-05 10:05:34 +00:00
gst_amrwbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
{
GstAmrwbDec *amrwbdec;
2011-10-05 10:05:34 +00:00
GstBuffer *out;
2012-01-25 06:24:59 +00:00
GstMapInfo inmap, outmap;
2011-10-05 10:05:34 +00:00
amrwbdec = GST_AMRWBDEC (dec);
2011-10-05 10:05:34 +00:00
/* no fancy flushing */
if (!buffer || !gst_buffer_get_size (buffer))
2011-10-05 10:05:34 +00:00
return GST_FLOW_OK;
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 */
2012-01-25 06:24:59 +00:00
gst_buffer_map (buffer, &inmap, GST_MAP_READ);
2011-10-05 10:05:34 +00:00
/* get output */
out = gst_buffer_new_and_alloc (sizeof (gint16) * L_FRAME16k);
2012-01-25 06:24:59 +00:00
gst_buffer_map (out, &outmap, GST_MAP_WRITE);
2011-10-05 10:05:34 +00:00
/* decode */
2012-01-25 06:24:59 +00:00
D_IF_decode (amrwbdec->handle, (unsigned char *) inmap.data,
(short int *) outmap.data, _good_frame);
2012-01-25 06:24:59 +00:00
gst_buffer_unmap (out, &outmap);
gst_buffer_unmap (buffer, &inmap);
2011-10-05 10:05:34 +00:00
/* send out */
return gst_audio_decoder_finish_frame (dec, out, 1);
/* ERRORS */
not_negotiated:
{
GST_ELEMENT_ERROR (amrwbdec, STREAM, TYPE_NOT_FOUND, (NULL),
("Decoder is not initialized"));
return GST_FLOW_NOT_NEGOTIATED;
}
}