sbc: port decoder to GstAudioDecoder

https://bugzilla.gnome.org/show_bug.cgi?id=690582
This commit is contained in:
Tim-Philipp Müller 2013-03-26 13:55:32 +00:00
parent fecddde2c2
commit ca9daee444
3 changed files with 152 additions and 218 deletions

View file

@ -1,8 +1,7 @@
/* GStreamer SBC audio decoder /* GStreamer SBC audio decoder
* BlueZ - Bluetooth protocol stack for Linux
* *
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org> * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
* * Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -20,15 +19,26 @@
* *
*/ */
/**
* SECTION:element-sbdec
*
* This element decodes a Bluetooth SBC audio streams to raw integer PCM audio
*
* <refsect2>
* <title>Example pipelines</title>
* |[
* gst-launch-1.0 -v filesrc location=audio.sbc ! sbcparse ! sbcdec ! audioconvert ! audioresample ! autoaudiosink
* ]| Decode a raw SBC file.
* </refsect2>
*/
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif #endif
#include <string.h> #include <string.h>
#include "gstsbcutil.h"
#include "gstsbcdec.h" #include "gstsbcdec.h"
#include <gst/audio/audio.h>
/* FIXME: where does this come from? how is it derived? */ /* FIXME: where does this come from? how is it derived? */
#define BUF_SIZE 8192 #define BUF_SIZE 8192
@ -36,15 +46,14 @@
GST_DEBUG_CATEGORY_STATIC (sbc_dec_debug); GST_DEBUG_CATEGORY_STATIC (sbc_dec_debug);
#define GST_CAT_DEFAULT sbc_dec_debug #define GST_CAT_DEFAULT sbc_dec_debug
static void gst_sbc_dec_finalize (GObject * obj);
/* FIXME: port to GstAudioDecoder base class */
#define parent_class gst_sbc_dec_parent_class #define parent_class gst_sbc_dec_parent_class
G_DEFINE_TYPE (GstSbcDec, gst_sbc_dec, GST_TYPE_ELEMENT); G_DEFINE_TYPE (GstSbcDec, gst_sbc_dec, GST_TYPE_AUDIO_DECODER);
static GstStaticPadTemplate sbc_dec_sink_factory = static GstStaticPadTemplate sbc_dec_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-sbc")); GST_STATIC_CAPS ("audio/x-sbc, channels = (int) [ 1, 2 ], "
"rate = (int) { 16000, 32000, 44100, 48000 }, "
"parsed = (boolean) true"));
static GstStaticPadTemplate sbc_dec_src_factory = static GstStaticPadTemplate sbc_dec_src_factory =
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
@ -53,215 +62,165 @@ GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
"channels = (int) [ 1, 2 ], layout=interleaved")); "channels = (int) [ 1, 2 ], layout=interleaved"));
static GstFlowReturn static GstFlowReturn
gst_sbc_dec_flush (GstSbcDec * dec, GstBuffer * outbuf, gst_sbc_dec_handle_frame (GstAudioDecoder * audio_dec, GstBuffer * buf)
gint outoffset, gint channels, gint rate)
{ {
GstClockTime outtime, duration; GstSbcDec *dec = GST_SBC_DEC (audio_dec);
GstBuffer *outbuf = NULL;
/* we will reuse the same caps object */
if (dec->send_caps) {
GstCaps *caps;
caps = gst_caps_new_simple ("audio/x-raw",
"format", G_TYPE_STRING, GST_AUDIO_NE (S16),
"rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels,
"layout", G_TYPE_STRING, "interleaved", NULL);
gst_pad_push_event (dec->srcpad, gst_event_new_caps (caps));
gst_caps_unref (caps);
}
/* calculate duration */
outtime = GST_BUFFER_TIMESTAMP (outbuf);
if (dec->next_timestamp != (guint64) - 1 && outtime != (guint64) - 1) {
duration = dec->next_timestamp - outtime;
} else if (outtime != (guint64) - 1) {
/* otherwise calculate duration based on outbuf size */
duration = gst_util_uint64_scale_int (outoffset / (2 * channels),
GST_SECOND, rate) - outtime;
} else {
duration = GST_CLOCK_TIME_NONE;
}
GST_BUFFER_DURATION (outbuf) = duration;
gst_buffer_resize (outbuf, 0, outoffset);
return gst_pad_push (dec->srcpad, outbuf);
}
static GstFlowReturn
sbc_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
{
GstSbcDec *dec = GST_SBC_DEC (parent);
GstFlowReturn res = GST_FLOW_OK;
const guint8 *indata;
guint insize;
GstClockTime timestamp;
gboolean discont;
GstMapInfo out_map; GstMapInfo out_map;
GstBuffer *outbuf; GstMapInfo in_map;
guint inoffset, outoffset; gsize output_size;
gint rate, channels; guint num_frames, i;
discont = GST_BUFFER_IS_DISCONT (buffer); /* no fancy draining */
if (discont) { if (G_UNLIKELY (buf == NULL))
/* reset previous buffer */ return GST_FLOW_OK;
gst_adapter_clear (dec->adapter);
/* we need a new timestamp to lock onto */
dec->next_sample = -1;
}
gst_adapter_push (dec->adapter, buffer); if (G_UNLIKELY (dec->frame_len == 0))
return GST_FLOW_NOT_NEGOTIATED;
timestamp = GST_BUFFER_TIMESTAMP (buffer); gst_buffer_map (buf, &in_map, GST_MAP_READ);
if (GST_CLOCK_TIME_IS_VALID (timestamp))
dec->next_timestamp = timestamp;
insize = gst_adapter_available (dec->adapter); if (G_UNLIKELY (in_map.size == 0))
indata = gst_adapter_map (dec->adapter, insize); goto done;
inoffset = 0; /* we assume all frames are of the same size, this is implied by the
outbuf = NULL; * input caps applying to the whole input buffer, and the parser should
channels = rate = 0; * also have made sure of that */
if (G_UNLIKELY (in_map.size % dec->frame_len != 0))
goto mixed_frames;
while (insize > 0) { num_frames = in_map.size / dec->frame_len;
gint inconsumed, outlen; output_size = num_frames * dec->samples_per_frame * sizeof (gint16);
gint outsize;
size_t outconsumed;
if (outbuf == NULL) { outbuf = gst_audio_decoder_allocate_output_buffer (audio_dec, output_size);
outbuf = gst_buffer_new_and_alloc (BUF_SIZE);
if (discont) { if (outbuf == NULL)
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT); goto no_buffer;
discont = FALSE;
}
GST_BUFFER_TIMESTAMP (outbuf) = dec->next_timestamp; gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE); for (i = 0; i < num_frames; ++i) {
outsize = out_map.size; gssize ret;
outoffset = 0; gsize written;
}
GST_INFO_OBJECT (dec, "inoffset %d/%d, outoffset %d/%d", inoffset, ret = sbc_decode (&dec->sbc, in_map.data + (i * dec->frame_len),
insize, outoffset, outsize); dec->frame_len, out_map.data + (i * dec->samples_per_frame * 2),
dec->samples_per_frame * 2, &written);
inconsumed = sbc_decode (&dec->sbc, indata + inoffset, insize, if (ret <= 0 || written != (dec->samples_per_frame * 2)) {
out_map.data + outoffset, outsize, &outconsumed); GST_WARNING_OBJECT (dec, "decoding error, ret = %" G_GSSIZE_FORMAT ", "
"written = %" G_GSSIZE_FORMAT, ret, written);
GST_INFO_OBJECT (dec, "consumed %d, produced %d", inconsumed, outconsumed); break;
if (inconsumed <= 0) {
guint frame_len = sbc_get_frame_length (&dec->sbc);
/* skip a frame */
if (insize > frame_len) {
insize -= frame_len;
inoffset += frame_len;
} else {
insize = 0;
}
continue;
}
inoffset += inconsumed;
if ((gint) insize > inconsumed)
insize -= inconsumed;
else
insize = 0;
outoffset += outconsumed;
outsize -= outconsumed;
rate = gst_sbc_parse_rate_from_sbc (dec->sbc.frequency);
channels = gst_sbc_get_channel_number (dec->sbc.mode);
/* calculate timestamp either from the incomming buffers or
* from our sample counter */
if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
/* lock onto timestamp when we have one */
dec->next_sample = gst_util_uint64_scale_int (timestamp,
rate, GST_SECOND);
timestamp = GST_CLOCK_TIME_NONE;
}
if (dec->next_sample != (guint64) - 1) {
/* calculate the next sample */
dec->next_sample += outconsumed / (2 * channels);
dec->next_timestamp = gst_util_uint64_scale_int (dec->next_sample,
GST_SECOND, rate);
}
/* check for space, push outbuf buffer */
outlen = sbc_get_codesize (&dec->sbc);
if (outsize < outlen) {
gst_buffer_unmap (outbuf, &out_map);
res = gst_sbc_dec_flush (dec, outbuf, outoffset, channels, rate);
outbuf = NULL;
if (res != GST_FLOW_OK)
goto done;
} }
} }
if (outbuf) { gst_buffer_unmap (outbuf, &out_map);
gst_buffer_unmap (outbuf, &out_map);
res = gst_sbc_dec_flush (dec, outbuf, outoffset, channels, rate); if (i > 0)
} gst_buffer_set_size (outbuf, i * dec->samples_per_frame * 2);
else
gst_buffer_replace (&outbuf, NULL);
done: done:
gst_adapter_unmap (dec->adapter); gst_buffer_unmap (buf, &in_map);
gst_adapter_flush (dec->adapter, inoffset);
return res; return gst_audio_decoder_finish_frame (audio_dec, outbuf, 1);
/* ERRORS */
mixed_frames:
{
GST_WARNING_OBJECT (dec, "inconsistent input data/frames, skipping");
goto done;
}
no_buffer:
{
GST_ERROR_OBJECT (dec, "could not allocate output buffer");
goto done;
}
} }
static GstStateChangeReturn static gboolean
gst_sbc_dec_change_state (GstElement * element, GstStateChange transition) gst_sbc_dec_set_format (GstAudioDecoder * audio_dec, GstCaps * caps)
{ {
GstStateChangeReturn result; GstSbcDec *dec = GST_SBC_DEC (audio_dec);
GstSbcDec *dec = GST_SBC_DEC (element); const gchar *channel_mode;
GstAudioInfo info;
GstStructure *s;
gint channels, rate, subbands, blocks, bitpool;
switch (transition) { s = gst_caps_get_structure (caps, 0);
case GST_STATE_CHANGE_READY_TO_PAUSED: gst_structure_get_int (s, "channels", &channels);
GST_DEBUG ("Setup subband codec"); gst_structure_get_int (s, "rate", &rate);
sbc_init (&dec->sbc, 0);
dec->send_caps = TRUE; /* save input format */
dec->next_sample = -1; channel_mode = gst_structure_get_string (s, "channel-mode");
break; if (channel_mode == NULL ||
default: !gst_structure_get_int (s, "subbands", &subbands) ||
break; !gst_structure_get_int (s, "blocks", &blocks) ||
!gst_structure_get_int (s, "bitpool", &bitpool))
return FALSE;
if (strcmp (channel_mode, "mono") == 0) {
dec->frame_len = 4 + (subbands * 1) / 2 + (blocks * 1 * bitpool) / 8;
} else if (strcmp (channel_mode, "dual") == 0) {
dec->frame_len = 4 + (subbands * 2) / 2 + (blocks * 2 * bitpool) / 8;
} else if (strcmp (channel_mode, "stereo") == 0) {
dec->frame_len = 4 + (subbands * 2) / 2 + (blocks * bitpool) / 8;
} else if (strcmp (channel_mode, "joint") == 0) {
dec->frame_len = 4 + (subbands * 2) / 2 + (subbands + blocks * bitpool) / 8;
} else {
return FALSE;
} }
result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); dec->samples_per_frame = channels * blocks * subbands;
switch (transition) { GST_INFO_OBJECT (dec, "frame len: %" G_GSIZE_FORMAT ", samples per frame "
case GST_STATE_CHANGE_PAUSED_TO_READY: "%" G_GSIZE_FORMAT, dec->frame_len, dec->samples_per_frame);
GST_DEBUG ("Finish subband codec");
gst_adapter_clear (dec->adapter);
sbc_finish (&dec->sbc);
dec->send_caps = TRUE;
break;
default: /* set up output format */
break; gst_audio_info_init (&info);
} gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, channels, NULL);
gst_audio_decoder_set_output_format (audio_dec, &info);
return result; return TRUE;
}
static gboolean
gst_sbc_dec_start (GstAudioDecoder * dec)
{
GstSbcDec *sbcdec = GST_SBC_DEC (dec);
GST_INFO_OBJECT (dec, "Setup subband codec");
sbc_init (&sbcdec->sbc, 0);
return TRUE;
}
static gboolean
gst_sbc_dec_stop (GstAudioDecoder * dec)
{
GstSbcDec *sbcdec = GST_SBC_DEC (dec);
GST_INFO_OBJECT (sbcdec, "Finish subband codec");
sbc_finish (&sbcdec->sbc);
sbcdec->samples_per_frame = 0;
sbcdec->frame_len = 0;
return TRUE;
} }
static void static void
gst_sbc_dec_class_init (GstSbcDecClass * klass) gst_sbc_dec_class_init (GstSbcDecClass * klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GstAudioDecoderClass *audio_decoder_class = (GstAudioDecoderClass *) klass;
GstElementClass *element_class = GST_ELEMENT_CLASS (klass); GstElementClass *element_class = (GstElementClass *) klass;
object_class->finalize = gst_sbc_dec_finalize; audio_decoder_class->start = GST_DEBUG_FUNCPTR (gst_sbc_dec_start);
audio_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_sbc_dec_stop);
element_class->change_state = GST_DEBUG_FUNCPTR (gst_sbc_dec_change_state); audio_decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_sbc_dec_set_format);
audio_decoder_class->handle_frame =
GST_DEBUG_FUNCPTR (gst_sbc_dec_handle_frame);
gst_element_class_add_pad_template (element_class, gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sbc_dec_sink_factory)); gst_static_pad_template_get (&sbc_dec_sink_factory));
@ -277,27 +236,8 @@ gst_sbc_dec_class_init (GstSbcDecClass * klass)
} }
static void static void
gst_sbc_dec_init (GstSbcDec * self) gst_sbc_dec_init (GstSbcDec * dec)
{ {
self->sinkpad = dec->samples_per_frame = 0;
gst_pad_new_from_static_template (&sbc_dec_sink_factory, "sink"); dec->frame_len = 0;
gst_pad_set_chain_function (self->sinkpad, GST_DEBUG_FUNCPTR (sbc_dec_chain));
gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
self->srcpad = gst_pad_new_from_static_template (&sbc_dec_src_factory, "src");
gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
self->adapter = gst_adapter_new ();
self->send_caps = TRUE;
}
static void
gst_sbc_dec_finalize (GObject * obj)
{
GstSbcDec *self = GST_SBC_DEC (obj);
g_object_unref (self->adapter);
self->adapter = NULL;
G_OBJECT_CLASS (parent_class)->finalize (obj);
} }

View file

@ -1,8 +1,7 @@
/* GStreamer SBC audio decoder /* GStreamer SBC audio decoder
* BlueZ - Bluetooth protocol stack for Linux
* *
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org> * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
* * Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -21,7 +20,7 @@
*/ */
#include <gst/gst.h> #include <gst/gst.h>
#include <gst/base/gstadapter.h> #include <gst/audio/audio.h>
#include <sbc/sbc.h> #include <sbc/sbc.h>
@ -42,22 +41,17 @@ typedef struct _GstSbcDec GstSbcDec;
typedef struct _GstSbcDecClass GstSbcDecClass; typedef struct _GstSbcDecClass GstSbcDecClass;
struct _GstSbcDec { struct _GstSbcDec {
GstElement element; GstAudioDecoder audio_decoder;
GstPad *sinkpad; /*< private >*/
GstPad *srcpad; sbc_t sbc;
GstAdapter *adapter; gsize frame_len;
gsize samples_per_frame; /* for all channels */
gboolean send_caps;
sbc_t sbc;
guint64 next_sample;
guint64 next_timestamp;
}; };
struct _GstSbcDecClass { struct _GstSbcDecClass {
GstElementClass parent_class; GstAudioDecoderClass audio_decoder_class;
}; };
GType gst_sbc_dec_get_type (void); GType gst_sbc_dec_get_type (void);

View file

@ -29,7 +29,7 @@
static gboolean static gboolean
plugin_init (GstPlugin * plugin) plugin_init (GstPlugin * plugin)
{ {
gst_element_register (plugin, "sbcdec", GST_RANK_NONE, GST_TYPE_SBC_DEC); gst_element_register (plugin, "sbcdec", GST_RANK_PRIMARY, GST_TYPE_SBC_DEC);
gst_element_register (plugin, "sbcenc", GST_RANK_NONE, GST_TYPE_SBC_ENC); gst_element_register (plugin, "sbcenc", GST_RANK_NONE, GST_TYPE_SBC_ENC);
return TRUE; return TRUE;
} }