Merge branch 'master' into 0.11

Conflicts:
	ext/amrnb/amrnbdec.c
This commit is contained in:
Wim Taymans 2011-10-04 17:50:33 +02:00
commit 76a13cbcdc
2 changed files with 100 additions and 248 deletions

View file

@ -96,34 +96,26 @@ static void gst_amrnbdec_set_property (GObject * object, guint prop_id,
static void gst_amrnbdec_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static gboolean gst_amrnbdec_event (GstPad * pad, GstEvent * event);
static GstFlowReturn gst_amrnbdec_chain (GstPad * pad, GstBuffer * buffer);
static gboolean gst_amrnbdec_setcaps (GstPad * pad, GstCaps * caps);
static GstStateChangeReturn gst_amrnbdec_state_change (GstElement * element,
GstStateChange transition);
static void gst_amrnbdec_finalize (GObject * object);
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 gboolean gst_amrnbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
gint * offset, gint * length);
static GstFlowReturn gst_amrnbdec_handle_frame (GstAudioDecoder * dec,
GstBuffer * buffer);
#define gst_amrnbdec_parent_class parent_class
G_DEFINE_TYPE (GstAmrnbDec, gst_amrnbdec, GST_TYPE_ELEMENT);
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;
object_class->finalize = gst_amrnbdec_finalize;
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));
element_class->change_state = GST_DEBUG_FUNCPTR (gst_amrnbdec_state_change);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sink_template));
@ -135,6 +127,18 @@ gst_amrnbdec_class_init (GstAmrnbDecClass * klass)
"Adaptive Multi-Rate Narrow-Band audio decoder",
"GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
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));
GST_DEBUG_CATEGORY_INIT (gst_amrnbdec_debug, "amrnbdec", 0,
"AMR-NB audio decoder");
}
@ -142,34 +146,32 @@ gst_amrnbdec_class_init (GstAmrnbDecClass * klass)
static void
gst_amrnbdec_init (GstAmrnbDec * amrnbdec)
{
/* create the sink pad */
amrnbdec->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
gst_pad_set_event_function (amrnbdec->sinkpad, gst_amrnbdec_event);
gst_pad_set_chain_function (amrnbdec->sinkpad, gst_amrnbdec_chain);
gst_element_add_pad (GST_ELEMENT (amrnbdec), amrnbdec->sinkpad);
/* create the src pad */
amrnbdec->srcpad = gst_pad_new_from_static_template (&src_template, "src");
gst_pad_use_fixed_caps (amrnbdec->srcpad);
gst_element_add_pad (GST_ELEMENT (amrnbdec), amrnbdec->srcpad);
amrnbdec->adapter = gst_adapter_new ();
/* init rest */
amrnbdec->handle = NULL;
}
static void
gst_amrnbdec_finalize (GObject * object)
static gboolean
gst_amrnbdec_start (GstAudioDecoder * dec)
{
GstAmrnbDec *amrnbdec;
GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
amrnbdec = GST_AMRNBDEC (object);
GST_DEBUG_OBJECT (dec, "start");
if (!(amrnbdec->handle = Decoder_Interface_init ()))
return FALSE;
gst_adapter_clear (amrnbdec->adapter);
g_object_unref (amrnbdec->adapter);
amrnbdec->rate = 0;
amrnbdec->channels = 0;
G_OBJECT_CLASS (parent_class)->finalize (object);
return TRUE;
}
static gboolean
gst_amrnbdec_stop (GstAudioDecoder * dec)
{
GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
GST_DEBUG_OBJECT (dec, "stop");
Decoder_Interface_exit (amrnbdec->handle);
return TRUE;
}
static void
@ -207,13 +209,13 @@ gst_amrnbdec_get_property (GObject * object, guint prop_id,
}
static gboolean
gst_amrnbdec_setcaps (GstPad * pad, GstCaps * caps)
gst_amrnbdec_set_format (GstAudioDecoder * dec, GstCaps * caps)
{
GstStructure *structure;
GstAmrnbDec *amrnbdec;
GstCaps *copy;
amrnbdec = GST_AMRNBDEC (gst_pad_get_parent (pad));
amrnbdec = GST_AMRNBDEC (dec);
structure = gst_caps_get_structure (caps, 0);
@ -222,177 +224,90 @@ gst_amrnbdec_setcaps (GstPad * pad, GstCaps * caps)
gst_structure_get_int (structure, "rate", &amrnbdec->rate);
/* create reverse caps */
copy = gst_caps_new_simple ("audio/x-raw-int",
copy = gst_caps_new_simple ("audio/x-raw",
"format", G_TYPE_STRING, GST_AUDIO_NE (S16),
"channels", G_TYPE_INT, amrnbdec->channels,
"width", G_TYPE_INT, 16,
"depth", G_TYPE_INT, 16,
"endianness", G_TYPE_INT, G_BYTE_ORDER,
"rate", G_TYPE_INT, amrnbdec->rate, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
amrnbdec->duration = gst_util_uint64_scale_int (GST_SECOND, 160,
amrnbdec->rate * amrnbdec->channels);
gst_pad_set_caps (amrnbdec->srcpad, copy);
"rate", G_TYPE_INT, amrnbdec->rate, NULL);
gst_audio_decoder_set_outcaps (dec, copy);
gst_caps_unref (copy);
gst_object_unref (amrnbdec);
return TRUE;
}
static gboolean
gst_amrnbdec_event (GstPad * pad, GstEvent * event)
static GstFlowReturn
gst_amrnbdec_parse (GstAudioDecoder * dec, GstAdapter * adapter,
gint * offset, gint * length)
{
GstAmrnbDec *amrnbdec;
gboolean ret = TRUE;
GstAmrnbDec *amrnbdec = GST_AMRNBDEC (dec);
guint8 head[1];
guint size;
gboolean sync, eos;
gint block, mode;
amrnbdec = GST_AMRNBDEC (gst_pad_get_parent (pad));
size = gst_adapter_available (adapter);
g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_CAPS:
{
GstCaps *caps;
gst_audio_decoder_get_parse_state (dec, &sync, &eos);
gst_event_parse_caps (event, &caps);
ret = gst_amrnbdec_setcaps (pad, caps);
gst_event_unref (event);
/* need to peek data to get the size */
if (gst_adapter_available (adapter) < 1)
return GST_FLOW_ERROR;
gst_adapter_copy (adapter, head, 0, 1);
/* get size */
switch (amrnbdec->variant) {
case GST_AMRNB_VARIANT_IF1:
mode = (head[0] >> 3) & 0x0F;
block = block_size_if1[mode] + 1;
break;
}
case GST_EVENT_FLUSH_START:
ret = gst_pad_push_event (amrnbdec->srcpad, event);
case GST_AMRNB_VARIANT_IF2:
mode = head[0] & 0x0F;
block = block_size_if2[mode] + 1;
break;
case GST_EVENT_FLUSH_STOP:
ret = gst_pad_push_event (amrnbdec->srcpad, event);
gst_adapter_clear (amrnbdec->adapter);
amrnbdec->ts = -1;
break;
case GST_EVENT_EOS:
gst_adapter_clear (amrnbdec->adapter);
ret = gst_pad_push_event (amrnbdec->srcpad, event);
break;
case GST_EVENT_SEGMENT:
{
GstSegment seg;
gst_event_copy_segment (event, &seg);
/* we need time for now */
if (seg.format != GST_FORMAT_TIME)
goto newseg_wrong_format;
GST_DEBUG_OBJECT (amrnbdec, "segment: %" GST_SEGMENT_FORMAT, &seg);
/* now configure the values */
amrnbdec->segment = seg;
ret = gst_pad_push_event (amrnbdec->srcpad, event);
break;
}
default:
ret = gst_pad_push_event (amrnbdec->srcpad, event);
g_assert_not_reached ();
return GST_FLOW_ERROR;
break;
}
done:
gst_object_unref (amrnbdec);
return ret;
GST_DEBUG_OBJECT (amrnbdec, "mode %d, block %d", mode, block);
/* ERRORS */
newseg_wrong_format:
{
GST_DEBUG_OBJECT (amrnbdec, "received non TIME newsegment");
goto done;
}
*offset = 0;
*length = block;
return GST_FLOW_OK;
}
static GstFlowReturn
gst_amrnbdec_chain (GstPad * pad, GstBuffer * buffer)
gst_amrnbdec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
{
GstAmrnbDec *amrnbdec;
GstFlowReturn ret;
guint8 *data;
short *out_data;
GstBuffer *out;
amrnbdec = GST_AMRNBDEC (gst_pad_get_parent (pad));
amrnbdec = GST_AMRNBDEC (dec);
/* no fancy flushing */
if (!buffer || !gst_buffer_get_size (buffer))
return GST_FLOW_OK;
if (amrnbdec->rate == 0 || amrnbdec->channels == 0)
goto not_negotiated;
/* discontinuity, don't combine samples before and after the
* DISCONT */
if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
gst_adapter_clear (amrnbdec->adapter);
amrnbdec->ts = -1;
amrnbdec->discont = TRUE;
}
data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
/* take latest timestamp, FIXME timestamp is the one of the
* first buffer in the adapter. */
if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
amrnbdec->ts = GST_BUFFER_TIMESTAMP (buffer);
/* get output */
out = gst_buffer_new_and_alloc (160 * 2);
/* decode */
out_data = gst_buffer_map (out, NULL, NULL, GST_MAP_WRITE);
Decoder_Interface_Decode (amrnbdec->handle, data, out_data, 0);
gst_buffer_unmap (out, out_data, 160 * 2);
gst_adapter_push (amrnbdec->adapter, buffer);
gst_buffer_unmap (buffer, data, -1);
ret = GST_FLOW_OK;
while (TRUE) {
GstBuffer *out;
guint8 head[1];
guint8 *data;
short *out_data;
gint block, mode;
/* need to peek data to get the size */
if (gst_adapter_available (amrnbdec->adapter) < 1)
break;
gst_adapter_copy (amrnbdec->adapter, head, 0, 1);
/* get size */
switch (amrnbdec->variant) {
case GST_AMRNB_VARIANT_IF1:
mode = (head[0] >> 3) & 0x0F;
block = block_size_if1[mode] + 1;
break;
case GST_AMRNB_VARIANT_IF2:
mode = head[0] & 0x0F;
block = block_size_if2[mode] + 1;
break;
default:
goto invalid_variant;
}
GST_DEBUG_OBJECT (amrnbdec, "mode %d, block %d", mode, block);
if (!block || gst_adapter_available (amrnbdec->adapter) < block)
break;
/* the library seems to write into the source data, hence
* the copy. */
data = gst_adapter_take (amrnbdec->adapter, block);
/* get output */
out = gst_buffer_new_and_alloc (160 * 2);
GST_BUFFER_DURATION (out) = amrnbdec->duration;
GST_BUFFER_TIMESTAMP (out) = amrnbdec->ts;
if (amrnbdec->ts != -1)
amrnbdec->ts += amrnbdec->duration;
if (amrnbdec->discont) {
GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT);
amrnbdec->discont = FALSE;
}
/* decode */
out_data = gst_buffer_map (out, NULL, NULL, GST_MAP_WRITE);
Decoder_Interface_Decode (amrnbdec->handle, data, out_data, 0);
gst_buffer_unmap (out, out_data, -1);
g_free (data);
/* send out */
ret = gst_pad_push (amrnbdec->srcpad, out);
}
gst_object_unref (amrnbdec);
return ret;
return gst_audio_decoder_finish_frame (dec, out, 1);
/* ERRORS */
not_negotiated:
@ -402,57 +317,4 @@ not_negotiated:
gst_object_unref (amrnbdec);
return GST_FLOW_NOT_NEGOTIATED;
}
invalid_variant:
{
GST_ELEMENT_ERROR (amrnbdec, STREAM, TYPE_NOT_FOUND, (NULL),
("Invalid variant"));
gst_object_unref (amrnbdec);
return GST_FLOW_ERROR;
}
}
static GstStateChangeReturn
gst_amrnbdec_state_change (GstElement * element, GstStateChange transition)
{
GstAmrnbDec *amrnbdec;
GstStateChangeReturn ret;
amrnbdec = GST_AMRNBDEC (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
if (!(amrnbdec->handle = Decoder_Interface_init ()))
goto init_failed;
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_adapter_clear (amrnbdec->adapter);
amrnbdec->rate = 0;
amrnbdec->channels = 0;
amrnbdec->ts = -1;
amrnbdec->discont = TRUE;
gst_segment_init (&amrnbdec->segment, GST_FORMAT_TIME);
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_READY_TO_NULL:
Decoder_Interface_exit (amrnbdec->handle);
break;
default:
break;
}
return ret;
/* ERRORS */
init_failed:
{
GST_ELEMENT_ERROR (amrnbdec, LIBRARY, INIT, (NULL),
("Failed to open AMR Decoder"));
return GST_STATE_CHANGE_FAILURE;
}
}

View file

@ -21,7 +21,7 @@
#define __GST_AMRNBDEC_H__
#include <gst/gst.h>
#include <gst/base/gstadapter.h>
#include <gst/audio/gstaudiodecoder.h>
#include <interf_dec.h>
G_BEGIN_DECLS
@ -47,29 +47,19 @@ typedef enum
} GstAmrnbVariant;
struct _GstAmrnbDec {
GstElement element;
/* pads */
GstPad *sinkpad, *srcpad;
guint64 ts;
GstAudioDecoder element;
GstAmrnbVariant variant;
GstAdapter *adapter;
/* library handle */
void *handle;
/* output settings */
gint channels, rate;
gint duration;
GstSegment segment;
gboolean discont;
};
struct _GstAmrnbDecClass {
GstElementClass parent_class;
GstAudioDecoderClass parent_class;
};
GType gst_amrnbdec_get_type (void);