2005-10-25 16:12:03 +00:00
|
|
|
/*
|
|
|
|
* Farsight
|
2005-10-26 15:45:11 +00:00
|
|
|
* GStreamer GSM encoder
|
2005-10-25 16:12:03 +00:00
|
|
|
* Copyright (C) 2005 Philippe Khalaf <burger@speedy.org>
|
2001-12-23 14:29:43 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-06-29 19:46:13 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2001-12-23 14:29:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gstgsmdec.h"
|
|
|
|
|
2006-06-23 09:09:44 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gsmdec_debug);
|
2005-10-25 16:12:03 +00:00
|
|
|
#define GST_CAT_DEFAULT (gsmdec_debug)
|
|
|
|
|
2001-12-23 14:29:43 +00:00
|
|
|
/* GSMDec signals and args */
|
2004-03-14 22:34:33 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-12-23 14:29:43 +00:00
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
enum
|
|
|
|
{
|
2005-10-25 16:12:03 +00:00
|
|
|
/* FILL ME */
|
2004-05-21 23:28:57 +00:00
|
|
|
ARG_0
|
2001-12-23 14:29:43 +00:00
|
|
|
};
|
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
static gboolean gst_gsmdec_start (GstAudioDecoder * dec);
|
|
|
|
static gboolean gst_gsmdec_stop (GstAudioDecoder * dec);
|
|
|
|
static gboolean gst_gsmdec_set_format (GstAudioDecoder * dec, GstCaps * caps);
|
|
|
|
static GstFlowReturn gst_gsmdec_parse (GstAudioDecoder * dec,
|
|
|
|
GstAdapter * adapter, gint * offset, gint * length);
|
|
|
|
static GstFlowReturn gst_gsmdec_handle_frame (GstAudioDecoder * dec,
|
|
|
|
GstBuffer * in_buf);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2002-03-19 04:10:06 +00:00
|
|
|
/*static guint gst_gsmdec_signals[LAST_SIGNAL] = { 0 }; */
|
2001-12-23 14:29:43 +00:00
|
|
|
|
2008-03-31 16:24:42 +00:00
|
|
|
#define ENCODED_SAMPLES 160
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gsmdec_sink_template =
|
2006-09-19 10:10:12 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
2003-12-22 01:47:09 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2006-09-19 10:10:12 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-gsm, rate = (int) 8000, channels = (int) 1; "
|
2008-03-31 16:24:42 +00:00
|
|
|
"audio/ms-gsm, rate = (int) [1, MAX], channels = (int) 1")
|
2004-03-14 22:34:33 +00:00
|
|
|
);
|
2003-12-22 01:47:09 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gsmdec_src_template =
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
2003-12-22 01:47:09 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw-int, "
|
2004-03-15 19:32:27 +00:00
|
|
|
"endianness = (int) BYTE_ORDER, "
|
|
|
|
"signed = (boolean) true, "
|
|
|
|
"width = (int) 16, "
|
2008-03-31 16:24:42 +00:00
|
|
|
"depth = (int) 16, " "rate = (int) [1, MAX], " "channels = (int) 1")
|
2004-03-14 22:34:33 +00:00
|
|
|
);
|
2003-11-01 16:35:25 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
GST_BOILERPLATE (GstGSMDec, gst_gsmdec, GstAudioDecoder,
|
|
|
|
GST_TYPE_AUDIO_DECODER);
|
|
|
|
|
2003-11-01 16:35:25 +00:00
|
|
|
static void
|
|
|
|
gst_gsmdec_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
2011-11-28 13:08:27 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gsmdec_sink_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gsmdec_src_template);
|
2010-03-18 16:30:26 +00:00
|
|
|
gst_element_class_set_details_simple (element_class, "GSM audio decoder",
|
|
|
|
"Codec/Decoder/Audio",
|
|
|
|
"Decodes GSM encoded audio", "Philippe Khalaf <burger@speedy.org>");
|
2003-11-01 16:35:25 +00:00
|
|
|
}
|
|
|
|
|
2001-12-23 14:29:43 +00:00
|
|
|
static void
|
2012-01-17 17:33:09 +00:00
|
|
|
gst_gsmdec_class_init (GstGSMDecClass * klass)
|
2001-12-23 14:29:43 +00:00
|
|
|
{
|
2012-01-17 17:33:09 +00:00
|
|
|
GstAudioDecoderClass *base_class;
|
2001-12-23 14:29:43 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
base_class = (GstAudioDecoderClass *) klass;
|
2001-12-23 14:29:43 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
base_class->start = GST_DEBUG_FUNCPTR (gst_gsmdec_start);
|
|
|
|
base_class->stop = GST_DEBUG_FUNCPTR (gst_gsmdec_stop);
|
|
|
|
base_class->set_format = GST_DEBUG_FUNCPTR (gst_gsmdec_set_format);
|
|
|
|
base_class->parse = GST_DEBUG_FUNCPTR (gst_gsmdec_parse);
|
|
|
|
base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_gsmdec_handle_frame);
|
2005-10-25 16:12:03 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gsmdec_debug, "gsmdec", 0, "GSM Decoder");
|
2001-12-23 14:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-17 17:33:09 +00:00
|
|
|
gst_gsmdec_init (GstGSMDec * gsmdec, GstGSMDecClass * klass)
|
2001-12-23 14:29:43 +00:00
|
|
|
{
|
2012-01-17 17:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_gsmdec_start (GstAudioDecoder * dec)
|
|
|
|
{
|
|
|
|
GstGSMDec *gsmdec = GST_GSMDEC (dec);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (dec, "start");
|
2001-12-23 14:29:43 +00:00
|
|
|
|
|
|
|
gsmdec->state = gsm_create ();
|
2005-10-25 16:12:03 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
return TRUE;
|
2001-12-23 14:29:43 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gsmdec_stop (GstAudioDecoder * dec)
|
2006-03-29 16:54:12 +00:00
|
|
|
{
|
2012-01-17 17:33:09 +00:00
|
|
|
GstGSMDec *gsmdec = GST_GSMDEC (dec);
|
2006-03-29 16:54:12 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "stop");
|
2006-03-29 16:54:12 +00:00
|
|
|
|
|
|
|
gsm_destroy (gsmdec->state);
|
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
return TRUE;
|
2006-03-29 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
2006-07-27 10:50:39 +00:00
|
|
|
static gboolean
|
2012-01-17 17:33:09 +00:00
|
|
|
gst_gsmdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
|
2006-07-27 10:50:39 +00:00
|
|
|
{
|
|
|
|
GstGSMDec *gsmdec;
|
|
|
|
GstCaps *srccaps;
|
2006-09-19 10:10:12 +00:00
|
|
|
GstStructure *s;
|
2008-03-31 16:24:42 +00:00
|
|
|
gboolean ret = FALSE;
|
2012-01-17 17:33:09 +00:00
|
|
|
gint rate;
|
2006-07-27 10:50:39 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
gsmdec = GST_GSMDEC (dec);
|
2006-07-27 10:50:39 +00:00
|
|
|
|
2006-09-19 10:10:12 +00:00
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
if (s == NULL)
|
|
|
|
goto wrong_caps;
|
|
|
|
|
|
|
|
/* figure out if we deal with plain or MSGSM */
|
|
|
|
if (gst_structure_has_name (s, "audio/x-gsm"))
|
|
|
|
gsmdec->use_wav49 = 0;
|
|
|
|
else if (gst_structure_has_name (s, "audio/ms-gsm"))
|
|
|
|
gsmdec->use_wav49 = 1;
|
|
|
|
else
|
|
|
|
goto wrong_caps;
|
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
gsmdec->needed = 33;
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (s, "rate", &rate)) {
|
2008-03-31 16:24:42 +00:00
|
|
|
GST_WARNING_OBJECT (gsmdec, "missing sample rate parameter from sink caps");
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2006-09-19 10:10:12 +00:00
|
|
|
/* MSGSM needs different framing */
|
|
|
|
gsm_option (gsmdec->state, GSM_OPT_WAV49, &gsmdec->use_wav49);
|
|
|
|
|
2008-03-31 16:24:42 +00:00
|
|
|
/* Setting up src caps based on the input sample rate. */
|
|
|
|
srccaps = gst_caps_new_simple ("audio/x-raw-int",
|
2009-03-04 10:07:52 +00:00
|
|
|
"endianness", G_TYPE_INT, G_BYTE_ORDER,
|
2008-03-31 16:24:42 +00:00
|
|
|
"signed", G_TYPE_BOOLEAN, TRUE,
|
|
|
|
"width", G_TYPE_INT, 16,
|
|
|
|
"depth", G_TYPE_INT, 16,
|
2012-01-17 17:33:09 +00:00
|
|
|
"rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, 1, NULL);
|
2008-03-31 16:24:42 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
ret = gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), srccaps);
|
2008-03-31 16:24:42 +00:00
|
|
|
gst_caps_unref (srccaps);
|
2006-07-27 10:50:39 +00:00
|
|
|
|
2008-03-31 16:24:42 +00:00
|
|
|
return ret;
|
2006-09-19 10:10:12 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
wrong_caps:
|
2008-03-31 16:24:42 +00:00
|
|
|
|
|
|
|
GST_ERROR_OBJECT (gsmdec, "invalid caps received");
|
|
|
|
|
|
|
|
beach:
|
|
|
|
|
|
|
|
return ret;
|
2006-07-27 10:50:39 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_gsmdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
|
|
|
|
gint * offset, gint * length)
|
2006-03-29 16:54:12 +00:00
|
|
|
{
|
2012-01-17 17:33:09 +00:00
|
|
|
GstGSMDec *gsmdec = GST_GSMDEC (dec);
|
|
|
|
guint size;
|
|
|
|
|
|
|
|
size = gst_adapter_available (adapter);
|
|
|
|
g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
|
2006-03-29 16:54:12 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
/* WAV49 requires alternating 33 and 32 bytes of input */
|
|
|
|
if (gsmdec->use_wav49) {
|
|
|
|
gsmdec->needed = (gsmdec->needed == 33 ? 32 : 33);
|
2006-03-29 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
if (size < gsmdec->needed)
|
|
|
|
return GST_FLOW_UNEXPECTED;
|
2006-03-29 16:54:12 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
*offset = 0;
|
|
|
|
*length = gsmdec->needed;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
2006-03-29 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
2005-09-23 17:05:29 +00:00
|
|
|
static GstFlowReturn
|
2012-01-17 17:33:09 +00:00
|
|
|
gst_gsmdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
|
2001-12-23 14:29:43 +00:00
|
|
|
{
|
|
|
|
GstGSMDec *gsmdec;
|
2005-10-25 16:12:03 +00:00
|
|
|
gsm_byte *data;
|
2005-10-26 15:45:11 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2012-01-17 17:33:09 +00:00
|
|
|
GstBuffer *outbuf;
|
|
|
|
|
|
|
|
/* no fancy draining */
|
|
|
|
if (G_UNLIKELY (!buffer))
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
gsmdec = GST_GSMDEC (dec);
|
|
|
|
|
|
|
|
/* always the same amount of output samples */
|
|
|
|
outbuf = gst_buffer_new_and_alloc (ENCODED_SAMPLES * sizeof (gsm_signal));
|
|
|
|
|
|
|
|
/* now encode frame into the output buffer */
|
|
|
|
data = (gsm_byte *) GST_BUFFER_DATA (buffer);
|
|
|
|
if (gsm_decode (gsmdec->state, data,
|
|
|
|
(gsm_signal *) GST_BUFFER_DATA (outbuf)) < 0) {
|
|
|
|
/* invalid frame */
|
|
|
|
GST_AUDIO_DECODER_ERROR (gsmdec, 1, STREAM, DECODE, (NULL),
|
|
|
|
("tried to decode an invalid frame"), ret);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto exit;
|
|
|
|
gst_buffer_unref (outbuf);
|
|
|
|
outbuf = NULL;
|
2005-09-23 17:05:29 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
gst_audio_decoder_finish_frame (dec, outbuf, 1);
|
2005-09-23 17:05:29 +00:00
|
|
|
|
2012-01-17 17:33:09 +00:00
|
|
|
exit:
|
2005-10-26 15:45:11 +00:00
|
|
|
return ret;
|
2001-12-23 14:29:43 +00:00
|
|
|
}
|