From bf45760b330d18dfde219b5601d2efbf4e88d8bf Mon Sep 17 00:00:00 2001 From: "Ronald S. Bultje" Date: Thu, 25 Nov 2004 20:36:29 +0000 Subject: Surround sound support. Original commit message from CVS: * ext/a52dec/gsta52dec.c: (gst_a52dec_channels), (gst_a52dec_push), (gst_a52dec_reneg), (gst_a52dec_loop), (plugin_init): * ext/alsa/gstalsa.c: (gst_alsa_get_caps): * ext/alsa/gstalsaplugin.c: (plugin_init): * ext/dts/gstdtsdec.c: (gst_dtsdec_channels), (gst_dtsdec_renegotiate), (gst_dtsdec_loop), (plugin_init): * ext/faad/gstfaad.c: (gst_faad_init), (gst_faad_chanpos_from_gst), (gst_faad_chanpos_to_gst), (gst_faad_sinkconnect), (gst_faad_srcgetcaps), (gst_faad_srcconnect), (gst_faad_chain), (gst_faad_change_state), (plugin_init): * ext/faad/gstfaad.h: * ext/vorbis/vorbis.c: (plugin_init): * ext/vorbis/vorbisdec.c: (vorbis_dec_chain): * gst-libs/gst/audio/Makefile.am: * gst-libs/gst/audio/audio.c: (plugin_init): * gst-libs/gst/audio/multichannel.c: (gst_audio_check_channel_positions), (gst_audio_get_channel_positions), (gst_audio_set_channel_positions), (gst_audio_set_structure_channel_positions_list), (add_list_to_struct), (gst_audio_set_caps_channel_positions_list), (gst_audio_fixate_channel_positions): * gst-libs/gst/audio/multichannel.h: * gst-libs/gst/audio/testchannels.c: (main): * gst/audioconvert/gstaudioconvert.c: (gst_audio_convert_class_init), (gst_audio_convert_init), (gst_audio_convert_dispose), (gst_audio_convert_getcaps), (gst_audio_convert_parse_caps), (gst_audio_convert_link), (gst_audio_convert_fixate), (gst_audio_convert_channels): * gst/audioconvert/plugin.c: (plugin_init): Surround sound support. --- ext/dts/gstdtsdec.c | 80 ++++++++++- ext/faad/gstfaad.c | 405 +++++++++++++++++++++++++++++++++++++++++----------- ext/faad/gstfaad.h | 5 + 3 files changed, 404 insertions(+), 86 deletions(-) (limited to 'ext') diff --git a/ext/dts/gstdtsdec.c b/ext/dts/gstdtsdec.c index db4bd7de..edcc8bcc 100644 --- a/ext/dts/gstdtsdec.c +++ b/ext/dts/gstdtsdec.c @@ -26,6 +26,8 @@ #include #include +#include + #include #include "gstdtsdec.h" @@ -180,42 +182,102 @@ gst_dtsdec_init (GstDtsDec * dtsdec) } static gint -gst_dtsdec_channels (uint32_t flags) +gst_dtsdec_channels (uint32_t flags, GstAudioChannelPosition ** pos) { gint chans = 0; switch (flags & DTS_CHANNEL_MASK) { case DTS_MONO: chans = 1; + if (pos) { + *pos = g_new (GstAudioChannelPosition, 2); + *pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO; + } break; - case DTS_CHANNEL: + /* case DTS_CHANNEL: */ case DTS_STEREO: case DTS_STEREO_SUMDIFF: case DTS_STEREO_TOTAL: case DTS_DOLBY: chans = 2; + if (pos) { + *pos = g_new (GstAudioChannelPosition, 3); + *pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + *pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + } break; case DTS_3F: + chans = 3; + if (pos) { + *pos = g_new (GstAudioChannelPosition, 4); + *pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER; + *pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + *pos[2] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + } + break; case DTS_2F1R: chans = 3; + if (pos) { + *pos = g_new (GstAudioChannelPosition, 4); + *pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + *pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + *pos[2] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER; + } break; case DTS_3F1R: + chans = 4; + if (pos) { + *pos = g_new (GstAudioChannelPosition, 5); + *pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER; + *pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + *pos[2] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + *pos[3] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER; + } + break; case DTS_2F2R: chans = 4; + if (pos) { + *pos = g_new (GstAudioChannelPosition, 5); + *pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + *pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + *pos[2] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT; + *pos[3] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT; + } break; case DTS_3F2R: chans = 5; + if (pos) { + *pos = g_new (GstAudioChannelPosition, 6); + *pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER; + *pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + *pos[2] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + *pos[3] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT; + *pos[4] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT; + } break; case DTS_4F2R: chans = 6; + if (pos) { + *pos = g_new (GstAudioChannelPosition, 7); + *pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER; + *pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER; + *pos[2] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + *pos[3] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + *pos[4] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT; + *pos[5] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT; + } break; default: /* error */ g_warning ("dtsdec: invalid flags 0x%x", flags); return 0; } - if (flags & DTS_LFE) + if (flags & DTS_LFE) { + if (pos) { + *pos[chans] = GST_AUDIO_CHANNEL_POSITION_LFE; + } chans += 1; + } return chans; } @@ -223,8 +285,12 @@ gst_dtsdec_channels (uint32_t flags) static gboolean gst_dtsdec_renegotiate (GstDtsDec * dts) { + GstAudioChannelPosition *pos; GstCaps *caps = gst_caps_from_string (DTS_CAPS); - gint channels = gst_dtsdec_channels (dts->using_channels); + gint channels = gst_dtsdec_channels (dts->using_channels, &pos); + + if (!channels) + return FALSE; GST_INFO ("dtsdec renegotiate, channels=%d, rate=%d", channels, dts->sample_rate); @@ -232,6 +298,8 @@ gst_dtsdec_renegotiate (GstDtsDec * dts) gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, (gint) dts->sample_rate, NULL); + gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos); + g_free (pos); return gst_pad_set_explicit_caps (dts->srcpad, caps); } @@ -381,7 +449,7 @@ gst_dtsdec_loop (GstElement * element) } samples = dts_samples (dts->state); - num_c = gst_dtsdec_channels (dts->using_channels); + num_c = gst_dtsdec_channels (dts->using_channels, NULL); out = gst_buffer_new_and_alloc ((SAMPLE_WIDTH / 8) * 256 * num_c); GST_BUFFER_TIMESTAMP (out) = timestamp; GST_BUFFER_DURATION (out) = GST_SECOND * 256 / dts->sample_rate; @@ -497,7 +565,7 @@ gst_dtsdec_get_property (GObject * object, guint prop_id, GValue * value, static gboolean plugin_init (GstPlugin * plugin) { - if (!gst_library_load ("gstbytestream")) + if (!gst_library_load ("gstbytestream") || !gst_library_load ("gstaudio")) return FALSE; if (!gst_element_register (plugin, "dtsdec", GST_RANK_PRIMARY, diff --git a/ext/faad/gstfaad.c b/ext/faad/gstfaad.c index 883790e2..3448f357 100644 --- a/ext/faad/gstfaad.c +++ b/ext/faad/gstfaad.c @@ -23,6 +23,8 @@ #include +#include + #include "gstfaad.h" static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", @@ -31,35 +33,61 @@ static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) { 2, 4 }") ); -static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", +#define STATIC_INT_CAPS(bpp) \ + "audio/x-raw-int, " \ + "endianness = (int) BYTE_ORDER, " \ + "signed = (bool) TRUE, " \ + "width = (int) " G_STRINGIFY (bpp) ", " \ + "depth = (int) " G_STRINGIFY (bpp) ", " \ + "rate = (int) [ 8000, 96000 ], " \ + "channels = (int) [ 1, 8 ]" + +#define STATIC_FLOAT_CAPS(bpp) \ + "audio/x-raw-float, " \ + "endianness = (int) BYTE_ORDER, " \ + "depth = (int) " G_STRINGIFY (bpp) ", " \ + "rate = (int) [ 8000, 96000 ], " \ + "channels = (int) [ 1, 8 ]" + +/* + * All except 16-bit integer are disabled until someone fixes FAAD. + * FAAD allocates approximately 8*1024*2 bytes bytes, which is enough + * for 1 frame (1024 samples) of 6 channel (5.1) 16-bit integer 16bpp + * audio, but not for any other. You'll get random segfaults, crashes + * and even valgrind goes crazy. + */ + +#define STATIC_CAPS \ + STATIC_INT_CAPS (16) +#if 0 +"; " +STATIC_INT_CAPS (24) + "; " +STATIC_INT_CAPS (32) + "; " +STATIC_FLOAT_CAPS (32) + "; " +STATIC_FLOAT_CAPS (64) +#endif + static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("audio/x-raw-int, " - "endianness = (int) BYTE_ORDER, " - "signed = (bool) TRUE, " - "width = (int) { 16, 24, 32 }, " - "depth = (int) { 16, 24, 32 }, " - "rate = (int) [ 8000, 96000 ], " - "channels = (int) [ 1, 6 ]; " - "audio/x-raw-float, " - "endianness = (int) BYTE_ORDER, " - "depth = (int) { 32, 64 }, " - "rate = (int) [ 8000, 96000 ], " "channels = (int) [ 1, 6 ]") + GST_STATIC_CAPS (STATIC_CAPS) ); -static void gst_faad_base_init (GstFaadClass * klass); -static void gst_faad_class_init (GstFaadClass * klass); -static void gst_faad_init (GstFaad * faad); + static void gst_faad_base_init (GstFaadClass * klass); + static void gst_faad_class_init (GstFaadClass * klass); + static void gst_faad_init (GstFaad * faad); -static GstPadLinkReturn -gst_faad_sinkconnect (GstPad * pad, const GstCaps * caps); -static GstPadLinkReturn -gst_faad_srcconnect (GstPad * pad, const GstCaps * caps); -static GstCaps *gst_faad_srcgetcaps (GstPad * pad); -static void gst_faad_chain (GstPad * pad, GstData * data); -static GstElementStateReturn gst_faad_change_state (GstElement * element); + static GstPadLinkReturn + gst_faad_sinkconnect (GstPad * pad, const GstCaps * caps); + static GstPadLinkReturn + gst_faad_srcconnect (GstPad * pad, const GstCaps * caps); + static GstCaps *gst_faad_srcgetcaps (GstPad * pad); + static void gst_faad_chain (GstPad * pad, GstData * data); + static GstElementStateReturn gst_faad_change_state (GstElement * element); -static GstElementClass *parent_class = NULL; + static GstElementClass *parent_class = NULL; /* static guint gst_faad_signals[LAST_SIGNAL] = { 0 }; */ @@ -123,6 +151,9 @@ gst_faad_init (GstFaad * faad) faad->samplerate = -1; faad->channels = -1; faad->tempbuf = NULL; + faad->need_channel_setup = TRUE; + faad->channel_positions = NULL; + faad->init = FALSE; GST_FLAG_SET (faad, GST_ELEMENT_EVENT_AWARE); @@ -142,6 +173,102 @@ gst_faad_init (GstFaad * faad) gst_pad_set_getcaps_function (faad->srcpad, gst_faad_srcgetcaps); } +/* + * Channel identifier conversion - caller g_free()s result! + */ + +static guchar * +gst_faad_chanpos_from_gst (GstAudioChannelPosition * pos, guint num) +{ + guchar *fpos = g_new (guchar, num); + guint n; + + for (n = 0; n < num; n++) { + switch (pos[n]) { + case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT: + fpos[n] = FRONT_CHANNEL_LEFT; + break; + case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT: + fpos[n] = FRONT_CHANNEL_RIGHT; + break; + case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER: + fpos[n] = FRONT_CHANNEL_CENTER; + break; + case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT: + fpos[n] = SIDE_CHANNEL_LEFT; + break; + case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT: + fpos[n] = SIDE_CHANNEL_RIGHT; + break; + case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT: + fpos[n] = BACK_CHANNEL_LEFT; + break; + case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT: + fpos[n] = BACK_CHANNEL_RIGHT; + break; + case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER: + fpos[n] = BACK_CHANNEL_CENTER; + break; + case GST_AUDIO_CHANNEL_POSITION_LFE: + fpos[n] = LFE_CHANNEL; + break; + default: + GST_WARNING ("Unsupported GST channel position 0x%x encountered", + pos[n]); + g_free (fpos); + return NULL; + } + } + + return fpos; +} + +static GstAudioChannelPosition * +gst_faad_chanpos_to_gst (guchar * fpos, guint num) +{ + GstAudioChannelPosition *pos = g_new (GstAudioChannelPosition, num); + guint n; + + for (n = 0; n < num; n++) { + switch (fpos[n]) { + case FRONT_CHANNEL_LEFT: + pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + break; + case FRONT_CHANNEL_RIGHT: + pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + break; + case FRONT_CHANNEL_CENTER: + pos[n] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER; + break; + case SIDE_CHANNEL_LEFT: + pos[n] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT; + break; + case SIDE_CHANNEL_RIGHT: + pos[n] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT; + break; + case BACK_CHANNEL_LEFT: + pos[n] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT; + break; + case BACK_CHANNEL_RIGHT: + pos[n] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT; + break; + case BACK_CHANNEL_CENTER: + pos[n] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER; + break; + case LFE_CHANNEL: + pos[n] = GST_AUDIO_CHANNEL_POSITION_LFE; + break; + default: + GST_WARNING ("Unsupported FAAD channel position 0x%x encountered", + fpos[n]); + g_free (pos); + return NULL; + } + } + + return pos; +} + static GstPadLinkReturn gst_faad_sinkconnect (GstPad * pad, const GstCaps * caps) { @@ -160,17 +287,20 @@ gst_faad_sinkconnect (GstPad * pad, const GstCaps * caps) GST_BUFFER_SIZE (buf), &samplerate, &channels) < 0) return GST_PAD_LINK_REFUSED; - faad->samplerate = samplerate; - faad->channels = channels; + //faad->samplerate = samplerate; + //faad->channels = channels; + faad->init = TRUE; if (faad->tempbuf) { gst_buffer_unref (faad->tempbuf); faad->tempbuf = NULL; } - - return GST_PAD_LINK_OK; + } else { + faad->init = FALSE; } + faad->need_channel_setup = TRUE; + /* if there's no decoderspecificdata, it's all fine. We cannot know * much more at this point... */ return GST_PAD_LINK_OK; @@ -180,27 +310,45 @@ static GstCaps * gst_faad_srcgetcaps (GstPad * pad) { GstFaad *faad = GST_FAAD (gst_pad_get_parent (pad)); + static GstAudioChannelPosition *supported_positions = NULL; + static gint num_supported_positions = LFE_CHANNEL - FRONT_CHANNEL_CENTER; + GstCaps *templ; + + if (!supported_positions) { + guchar *supported_fpos = g_new0 (guchar, + LFE_CHANNEL - FRONT_CHANNEL_CENTER); + gint n; + + for (n = 0; n < LFE_CHANNEL - FRONT_CHANNEL_CENTER; n++) { + supported_fpos[n] = n + FRONT_CHANNEL_CENTER; + } + supported_positions = gst_faad_chanpos_to_gst (supported_fpos, n); + g_free (supported_fpos); + } if (faad->handle != NULL && faad->channels != -1 && faad->samplerate != -1) { GstCaps *caps = gst_caps_new_empty (); GstStructure *str; gint fmt[] = { FAAD_FMT_16BIT, +#if 0 FAAD_FMT_24BIT, FAAD_FMT_32BIT, FAAD_FMT_FLOAT, FAAD_FMT_DOUBLE, +#endif -1 } , n; for (n = 0; fmt[n] != -1; n++) { - switch (n) { + switch (fmt[n]) { case FAAD_FMT_16BIT: str = gst_structure_new ("audio/x-raw-int", "signed", G_TYPE_BOOLEAN, TRUE, "width", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL); break; +#if 0 case FAAD_FMT_24BIT: str = gst_structure_new ("audio/x-raw-int", "signed", G_TYPE_BOOLEAN, TRUE, @@ -219,6 +367,7 @@ gst_faad_srcgetcaps (GstPad * pad) str = gst_structure_new ("audio/x-raw-float", "depth", G_TYPE_INT, 64, NULL); break; +#endif default: str = NULL; break; @@ -234,8 +383,26 @@ gst_faad_srcgetcaps (GstPad * pad) if (faad->channels != -1) { gst_structure_set (str, "channels", G_TYPE_INT, faad->channels, NULL); + + /* put channel information here */ + if (faad->channel_positions) { + GstAudioChannelPosition *pos; + + pos = gst_faad_chanpos_to_gst (faad->channel_positions, + faad->channels); + if (!pos) { + gst_structure_free (str); + continue; + } + gst_audio_set_channel_positions (str, pos); + g_free (pos); + } else { + gst_audio_set_structure_channel_positions_list (str, + supported_positions, num_supported_positions); + } } else { - gst_structure_set (str, "channels", GST_TYPE_INT_RANGE, 1, 6, NULL); + gst_structure_set (str, "channels", GST_TYPE_INT_RANGE, 1, 8, NULL); + /* we set channel positions later */ } gst_structure_set (str, "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL); @@ -243,10 +410,20 @@ gst_faad_srcgetcaps (GstPad * pad) gst_caps_append_structure (caps, str); } + if (faad->channels == -1) { + gst_audio_set_caps_channel_positions_list (caps, + supported_positions, num_supported_positions); + } + return caps; } - return gst_caps_copy (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad))); + /* template with channel positions */ + templ = gst_caps_copy (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad))); + gst_audio_set_caps_channel_positions_list (templ, + supported_positions, num_supported_positions); + + return templ; } static GstPadLinkReturn @@ -258,11 +435,13 @@ gst_faad_srcconnect (GstPad * pad, const GstCaps * caps) gint depth, rate, channels; GstFaad *faad = GST_FAAD (gst_pad_get_parent (pad)); - if (!faad->handle || (faad->samplerate == -1 || faad->channels == -1)) { + structure = gst_caps_get_structure (caps, 0); + + if (!faad->handle || (faad->samplerate == -1 || faad->channels == -1) || + !faad->channel_positions) { return GST_PAD_LINK_DELAYED; } - structure = gst_caps_get_structure (caps, 0); mimetype = gst_structure_get_name (structure); /* Samplerate and channels are normally provided through @@ -273,6 +452,30 @@ gst_faad_srcconnect (GstPad * pad, const GstCaps * caps) return GST_PAD_LINK_REFUSED; } + /* Another internal checkup. */ + if (faad->need_channel_setup) { + GstAudioChannelPosition *pos; + guchar *fpos; + guint i; + + pos = gst_audio_get_channel_positions (structure); + if (!pos) { + return GST_PAD_LINK_DELAYED; + } + fpos = gst_faad_chanpos_from_gst (pos, faad->channels); + g_free (pos); + if (!fpos) + return GST_PAD_LINK_REFUSED; + + for (i = 0; i < faad->channels; i++) { + if (fpos[i] != faad->channel_positions[i]) { + g_free (fpos); + return GST_PAD_LINK_REFUSED; + } + } + g_free (fpos); + } + if (!strcmp (mimetype, "audio/x-raw-int")) { gint width; @@ -286,39 +489,47 @@ gst_faad_srcconnect (GstPad * pad, const GstCaps * caps) case 16: fmt = FAAD_FMT_16BIT; break; +#if 0 case 24: fmt = FAAD_FMT_24BIT; break; case 32: fmt = FAAD_FMT_32BIT; break; +#endif } } else { if (!gst_structure_get_int (structure, "depth", &depth)) return GST_PAD_LINK_REFUSED; switch (depth) { +#if 0 case 32: fmt = FAAD_FMT_FLOAT; break; case 64: fmt = FAAD_FMT_DOUBLE; break; +#endif } } if (fmt != -1) { faacDecConfiguration *conf; + g_print ("Set format %d\n", fmt); conf = faacDecGetCurrentConfiguration (faad->handle); conf->outputFormat = fmt; - faacDecSetConfiguration (faad->handle, conf); + g_print ("Trying to conf\n"); + if (faacDecSetConfiguration (faad->handle, conf) == 0) + return GST_PAD_LINK_REFUSED; + g_print ("Done\n"); /* FIXME: handle return value, how? */ faad->bps = depth / 8; return GST_PAD_LINK_OK; } - + g_print ("Format not recognized\n"); return GST_PAD_LINK_REFUSED; } @@ -329,7 +540,7 @@ gst_faad_chain (GstPad * pad, GstData * data) guchar *input_data; GstFaad *faad = GST_FAAD (gst_pad_get_parent (pad)); GstBuffer *buf, *outbuf; - faacDecFrameInfo info; + faacDecFrameInfo *info; void *out; if (GST_IS_EVENT (data)) { @@ -338,18 +549,8 @@ gst_faad_chain (GstPad * pad, GstData * data) switch (GST_EVENT_TYPE (event)) { case GST_EVENT_EOS: if (faad->tempbuf != NULL) { - /* Try to decode the remaining data */ - out = faacDecDecode (faad->handle, &info, - GST_BUFFER_DATA (faad->tempbuf), GST_BUFFER_SIZE (faad->tempbuf)); gst_buffer_unref (faad->tempbuf); faad->tempbuf = NULL; - if (out && !info.error && info.samples > 0) { - outbuf = gst_buffer_new_and_alloc (info.samples * faad->bps); - /* ugh */ - memcpy (GST_BUFFER_DATA (outbuf), out, GST_BUFFER_SIZE (outbuf)); - - gst_pad_push (faad->srcpad, GST_DATA (outbuf)); - } } gst_element_set_eos (GST_ELEMENT (faad)); gst_pad_push (faad->srcpad, data); @@ -360,55 +561,89 @@ gst_faad_chain (GstPad * pad, GstData * data) } } + info = g_new0 (faacDecFrameInfo, 1); + + /* buffer + remaining data */ buf = GST_BUFFER (data); + if (faad->tempbuf) { + buf = gst_buffer_join (faad->tempbuf, buf); + faad->tempbuf = NULL; + } - if (faad->samplerate == -1 || faad->channels == -1) { - GstPadLinkReturn ret; + /* init if not already done during capsnego */ + if (!faad->init) { gulong samplerate; guchar channels; faacDecInit (faad->handle, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf), &samplerate, &channels); - faad->samplerate = samplerate; - faad->channels = channels; - - ret = gst_pad_renegotiate (faad->srcpad); - if (GST_PAD_LINK_FAILED (ret)) { - GST_ELEMENT_ERROR (faad, CORE, NEGOTIATION, (NULL), (NULL)); - gst_buffer_unref (buf); - return; - } - } + faad->init = TRUE; - /* Use the leftovers */ - if (faad->tempbuf) { - buf = gst_buffer_join (faad->tempbuf, buf); - faad->tempbuf = NULL; + /* store for renegotiation later on */ + info->samplerate = samplerate; + info->channels = channels; + } else { + info->samplerate = 0; + info->channels = 0; } + /* decode cycle */ input_data = GST_BUFFER_DATA (buf); input_size = GST_BUFFER_SIZE (buf); - info.bytesconsumed = input_size; - while (input_size > (faad->channels * FAAD_MIN_STREAMSIZE) - && info.bytesconsumed > 0) { - out = faacDecDecode (faad->handle, &info, input_data, input_size); - if (info.error) { + info->bytesconsumed = input_size; + while (input_size >= FAAD_MIN_STREAMSIZE && info->bytesconsumed > 0) { + g_print ("Decoding %d bytes of data\n", input_size); + out = faacDecDecode (faad->handle, info, input_data, input_size); + g_print ("done, rec. %p\n", out); + if (info->error) { GST_ELEMENT_ERROR (faad, STREAM, DECODE, (NULL), - ("Failed to decode buffer: %s", faacDecGetErrorMessage (info.error))); + ("Failed to decode buffer: %s", + faacDecGetErrorMessage (info->error))); break; } - input_size -= info.bytesconsumed; - input_data += info.bytesconsumed; + if (info->bytesconsumed > input_size) + info->bytesconsumed = input_size; + input_size -= info->bytesconsumed; + input_data += info->bytesconsumed; + + if (out && info->samples > 0) { + gboolean fmt_change = FALSE; - if (out) { + /* see if we need to renegotiate */ + if (info->samplerate != faad->samplerate || + info->channels != faad->channels || !faad->channel_positions) { + fmt_change = TRUE; + } else { + gint i; - if (info.samplerate != faad->samplerate - || info.channels != faad->channels) { + for (i = 0; i < info->channels; i++) { + if (info->channel_position[i] != faad->channel_positions[i]) + fmt_change = TRUE; + } + } + + if (fmt_change) { GstPadLinkReturn ret; - faad->samplerate = info.samplerate; - faad->channels = info.channels; + g_print ("Format change\n"); + g_print ("To %ld Hz, %d chans, %d/%d/%d/%d/%d/%d\n", + info->samplerate, info->channels, + info->channel_position[0], + info->channel_position[1], + info->channel_position[2], + info->channel_position[3], + info->channel_position[4], info->channel_position[5]); + /* store new negotiation information */ + faad->samplerate = info->samplerate; + faad->channels = info->channels; + if (faad->channel_positions) + g_free (faad->channel_positions); + faad->channel_positions = g_new (guint8, faad->channels); + memcpy (faad->channel_positions, info->channel_position, + faad->channels); + + /* and negotiate */ ret = gst_pad_renegotiate (faad->srcpad); if (GST_PAD_LINK_FAILED (ret)) { GST_ELEMENT_ERROR (faad, CORE, NEGOTIATION, (NULL), (NULL)); @@ -416,30 +651,35 @@ gst_faad_chain (GstPad * pad, GstData * data) } } - if (info.samples > 0) { - outbuf = gst_buffer_new_and_alloc (info.samples * faad->bps); + /* play decoded data */ + if (info->samples > 0) { + g_print ("Playing %ld samples from buf %p\n", info->samples, out); + outbuf = gst_buffer_new_and_alloc (info->samples * faad->bps); /* ugh */ memcpy (GST_BUFFER_DATA (outbuf), out, GST_BUFFER_SIZE (outbuf)); + g_print ("done, to %p\n", GST_BUFFER_DATA (outbuf)); GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf); GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf); gst_pad_push (faad->srcpad, GST_DATA (outbuf)); } } - }; + } /* Keep the leftovers */ if (input_size > 0) { - if (input_size < GST_BUFFER_SIZE (buf)) + if (input_size < GST_BUFFER_SIZE (buf)) { faad->tempbuf = gst_buffer_create_sub (buf, GST_BUFFER_SIZE (buf) - input_size, input_size); - else { + } else { faad->tempbuf = buf; gst_buffer_ref (buf); } } gst_buffer_unref (buf); + + g_free (info); } static GstElementStateReturn @@ -463,6 +703,10 @@ gst_faad_change_state (GstElement * element) case GST_STATE_PAUSED_TO_READY: faad->samplerate = -1; faad->channels = -1; + faad->need_channel_setup = TRUE; + faad->init = FALSE; + g_free (faad->channel_positions); + faad->channel_positions = NULL; break; case GST_STATE_READY_TO_NULL: faacDecClose (faad->handle); @@ -485,7 +729,8 @@ gst_faad_change_state (GstElement * element) static gboolean plugin_init (GstPlugin * plugin) { - return gst_element_register (plugin, "faad", GST_RANK_PRIMARY, GST_TYPE_FAAD); + return gst_library_load ("gstaudio") && + gst_element_register (plugin, "faad", GST_RANK_PRIMARY, GST_TYPE_FAAD); } GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, diff --git a/ext/faad/gstfaad.h b/ext/faad/gstfaad.h index e5c66b03..2f048635 100644 --- a/ext/faad/gstfaad.h +++ b/ext/faad/gstfaad.h @@ -52,6 +52,11 @@ typedef struct _GstFaad { /* FAAD object */ faacDecHandle handle; + gboolean init; + + /* FAAD channel setup */ + guchar *channel_positions; + gboolean need_channel_setup; } GstFaad; typedef struct _GstFaadClass { -- cgit v1.2.1