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. --- ChangeLog | 34 ++ ext/dts/gstdtsdec.c | 80 ++++- ext/faad/gstfaad.c | 405 +++++++++++++++++++----- ext/faad/gstfaad.h | 5 + gst-libs/gst/audio/Makefile.am | 30 +- gst-libs/gst/audio/audio.c | 3 + gst-libs/gst/audio/multichannel.c | 634 ++++++++++++++++++++++++++++++++++++++ gst-libs/gst/audio/multichannel.h | 86 ++++++ gst-libs/gst/audio/testchannels.c | 55 ++++ 9 files changed, 1243 insertions(+), 89 deletions(-) create mode 100644 gst-libs/gst/audio/multichannel.c create mode 100644 gst-libs/gst/audio/multichannel.h create mode 100644 gst-libs/gst/audio/testchannels.c diff --git a/ChangeLog b/ChangeLog index e6cf91c2..4d5c2166 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,37 @@ +2004-11-25 Ronald S. Bultje + + * 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. + 2004-11-25 Ronald S. Bultje * ext/ogg/gstoggdemux.c: (gst_ogg_demux_push): 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 { diff --git a/gst-libs/gst/audio/Makefile.am b/gst-libs/gst/audio/Makefile.am index be353e36..99089947 100644 --- a/gst-libs/gst/audio/Makefile.am +++ b/gst-libs/gst/audio/Makefile.am @@ -1,15 +1,32 @@ +# variables used for enum/marshal generation +glib_enum_headers=multichannel.h +glib_enum_define=GST_AUDIO +glib_enum_prefix=gst_audio + +built_sources = multichannel-enumtypes.c +built_headers = multichannel-enumtypes.h +BUILT_SOURCES = $(built_sources) $(built_headers) + librarydir = $(libdir)/gstreamer-@GST_MAJORMINOR@ library_LTLIBRARIES = libgstaudio.la libgstaudiofilter.la noinst_LTLIBRARIES = libgstaudiofilterexample.la EXTRA_DIST = gstaudiofiltertemplate.c make_filter -CLEANFILES = gstaudiofilterexample.c +CLEANFILES = gstaudiofilterexample.c \ + $(BUILT_SOURCES) -libgstaudio_la_SOURCES = audio.c audioclock.c +libgstaudio_la_SOURCES = audio.c audioclock.c \ + multichannel.c +nodist_libgstaudio_la_SOURCES = $(built_sources) libgstaudioincludedir = $(includedir)/gstreamer-@GST_MAJORMINOR@/gst/audio -libgstaudioinclude_HEADERS = audio.h audioclock.h gstaudiofilter.h +libgstaudioinclude_HEADERS = \ + audio.h \ + audioclock.h \ + gstaudiofilter.h \ + multichannel.h \ + multichannel-enumtypes.h libgstaudio_la_LIBADD = libgstaudio_la_CFLAGS = $(GST_CFLAGS) @@ -25,3 +42,10 @@ libgstaudiofilterexample_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) gstaudiofilterexample.c: $(srcdir)/make_filter $(srcdir)/gstaudiofiltertemplate.c $(srcdir)/make_filter AudiofilterExample $(srcdir)/gstaudiofiltertemplate.c + +noinst_PROGRAMS = testchannels +testchannels_SOURCES = testchannels.c +testchannels_CFLAGS = $(GST_CFLAGS) +testchannels_LDFLAGS = $(GST_LIBS) + +include $(top_srcdir)/common/glib-gen.mak diff --git a/gst-libs/gst/audio/audio.c b/gst-libs/gst/audio/audio.c index ae0571fa..b6a1edb5 100644 --- a/gst-libs/gst/audio/audio.c +++ b/gst-libs/gst/audio/audio.c @@ -22,6 +22,7 @@ #endif #include "audio.h" +#include "multichannel-enumtypes.h" #include @@ -267,6 +268,8 @@ gst_audio_structure_set_int (GstStructure * structure, GstAudioFieldFlag flag) static gboolean plugin_init (GstPlugin * plugin) { + gst_audio_channel_position_get_type (); + return TRUE; } diff --git a/gst-libs/gst/audio/multichannel.c b/gst-libs/gst/audio/multichannel.c new file mode 100644 index 00000000..67c055d9 --- /dev/null +++ b/gst-libs/gst/audio/multichannel.c @@ -0,0 +1,634 @@ +/* GStreamer Multichannel-Audio helper functions + * (c) 2004 Ronald Bultje + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "multichannel.h" + +#define GST_AUDIO_CHANNEL_POSITIONS_PROPERTY_NAME "channel-positions" + +/* + * This function checks if basic assumptions apply: + * - does each position occur at most once? + * - do conflicting positions occur? + * + front_mono vs. front_left/right + * + front_center vs. front_left/right_of_center + * + rear_center vs. rear_left/right + * It also adds some hacks that 0.8.x needs for compatibility: + * - if channels == 1, are we really mono? + * - if channels == 2, are we really stereo? + */ + +static gboolean +gst_audio_check_channel_positions (const GstAudioChannelPosition * pos, + gint channels) +{ + gint i, n; + struct + { + GstAudioChannelPosition pos1[2]; + GstAudioChannelPosition pos2[1]; + } conf[] = { + /* front: mono <-> stereo */ + { { + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, { + GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}}, + /* front center: 2 <-> 1 */ + { { + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, { + GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}}, + /* rear: 2 <-> 1 */ + { { + GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, + GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, { + GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}}, { { + GST_AUDIO_CHANNEL_POSITION_INVALID}} + }; + + /* check for invalid channel positions */ + for (n = 0; n < channels; n++) { + if (pos[n] == GST_AUDIO_CHANNEL_POSITION_INVALID) { + g_warning ("Position %d is invalid, not allowed", n); + return FALSE; + } + } + + /* check for multiple position occurrences */ + for (i = GST_AUDIO_CHANNEL_POSITION_INVALID + 1; + i < GST_AUDIO_CHANNEL_POSITION_NUM; i++) { + gint count = 0; + + for (n = 0; n < channels; n++) { + if (pos[n] == i) + count++; + } + + if (count > 1) { + g_warning ("Channel position %d occurred %d times, not allowed", + i, count); + return FALSE; + } + } + + /* check for position conflicts */ + for (i = 0; conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; i++) { + gboolean found1 = FALSE, found2 = FALSE; + + for (n = 0; n < channels; n++) { + if (pos[n] == conf[i].pos1[0] || pos[n] == conf[i].pos1[1]) + found1 = TRUE; + else if (pos[n] == conf[i].pos2[0]) + found2 = TRUE; + } + + if (found1 && found2) { + g_warning ("Found conflicting channel positions %d/%d and %d", + conf[i].pos1[0], conf[i].pos1[1], conf[i].pos2[0]); + return FALSE; + } + } + + /* 0.8.x evilry */ + if ((channels == 1 && pos[0] != GST_AUDIO_CHANNEL_POSITION_FRONT_MONO) || + (channels == 2 && (pos[0] != GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT || + pos[1] != GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT))) { + g_warning ("0.8.x: channels=1 implies mono; channels=2 implies stereo"); + return FALSE; + } + + return TRUE; +} + +/** + * gst_audio_get_channel_positions: + * @str: A #GstStructure to retrieve channel positions from. + * + * Retrieves a number of (fixed!) audio channel positions from + * the provided #GstStructure and returns it as a newly allocated + * array. The caller should g_free () this array. The caller + * should also check that the members in this #GstStructure are + * indeed "fixed" before calling this function. + * + * Returns: a newly allocated array containing the channel + * positions as provided in the given #GstStructure. Returns + * NULL on error. + */ + +GstAudioChannelPosition * +gst_audio_get_channel_positions (GstStructure * str) +{ + GstAudioChannelPosition *pos; + gint channels, n; + const GValue *pos_val_arr, *pos_val_entry; + gboolean res; + GType t; + + /* get number of channels, general type checkups */ + g_return_val_if_fail (str != NULL, NULL); + res = gst_structure_get_int (str, "channels", &channels); + g_return_val_if_fail (res, NULL); + g_return_val_if_fail (channels > 0, NULL); + pos_val_arr = gst_structure_get_value (str, + GST_AUDIO_CHANNEL_POSITIONS_PROPERTY_NAME); + + /* The following checks are here to retain compatibility for plugins not + * implementing this property. They expect that channels=1 implies mono + * and channels=2 implies stereo, so we follow that. + * This might be removed during 0.9.x. */ + if (!pos_val_arr && (channels == 1 || channels == 2)) { + pos = g_new (GstAudioChannelPosition, channels); + if (channels == 1) { + pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO; + } else { + pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + } + return pos; + } + g_return_val_if_fail (pos_val_arr != NULL, NULL); + g_return_val_if_fail (gst_value_list_get_size (pos_val_arr) == channels, + NULL); + for (n = 0; n < channels; n++) { + t = G_VALUE_TYPE (gst_value_list_get_value (pos_val_arr, n)); + g_return_val_if_fail (t == GST_TYPE_AUDIO_CHANNEL_POSITION, NULL); + } + + /* ... and fill array */ + pos = g_new (GstAudioChannelPosition, channels); + for (n = 0; n < channels; n++) { + pos_val_entry = gst_value_list_get_value (pos_val_arr, n); + pos[n] = g_value_get_enum (pos_val_entry); + } + + if (!gst_audio_check_channel_positions (pos, channels)) { + g_free (pos); + return NULL; + } + + return pos; +} + +/** + * gst_audio_set_channel_positions: + * @str: A #GstStructure to retrieve channel positions from. + * @pos: an array of channel positions. The number of members + * in this array should be equal to the (fixed!) number + * of the "channels" property in the given #GstStructure. + * + * Adds a "channel-positions" property to the given #GstStructure, + * which will represent the channel positions as given in the + * provided #GstAudioChannelPosition array. + */ + +void +gst_audio_set_channel_positions (GstStructure * str, + const GstAudioChannelPosition * pos) +{ + GValue pos_val_arr = { 0 }, pos_val_entry = { + 0}; + gint channels, n; + gboolean res; + + /* get number of channels, checkups */ + g_return_if_fail (str != NULL); + g_return_if_fail (pos != NULL); + res = gst_structure_get_int (str, "channels", &channels); + g_return_if_fail (res); + g_return_if_fail (channels > 0); + if (!gst_audio_check_channel_positions (pos, channels)) + return; + + /* build gvaluearray from positions */ + g_value_init (&pos_val_entry, GST_TYPE_AUDIO_CHANNEL_POSITION); + g_value_init (&pos_val_arr, GST_TYPE_FIXED_LIST); + for (n = 0; n < channels; n++) { + g_value_set_enum (&pos_val_entry, pos[n]); + gst_value_list_append_value (&pos_val_arr, &pos_val_entry); + } + g_value_unset (&pos_val_entry); + + /* add to structure */ + gst_structure_set_value (str, + GST_AUDIO_CHANNEL_POSITIONS_PROPERTY_NAME, &pos_val_arr); + g_value_unset (&pos_val_arr); +} + +/** + * gst_audio_set_structure_channel_positions_list: + * @str: #GstStructure to set the list of channel positions + * on. + * @pos: the array containing one or more possible audio + * channel positions that we should add in each value + * of the array in the given structure. + * @num_positions: the number of values in pos. + * + * Sets a (possibly non-fixed) list of possible audio channel + * positions (given in pos) on the given structure. The + * structure, after this function has been called, will contain + * a "channel-positions" property with an array of the size of + * the "channels" property value in the given structure (note + * that this means that the channels property in the provided + * structure should be fixed!). Each value in the array will + * contain each of the values given in the pos array. + */ + +void +gst_audio_set_structure_channel_positions_list (GstStructure * str, + const GstAudioChannelPosition * pos, gint num_positions) +{ + gint channels, n, c; + GValue pos_val_arr = { 0 }, pos_val_list = { + 0}, pos_val_entry = { + 0}; + gboolean res; + + /* get number of channels, general type checkups */ + g_return_if_fail (str != NULL); + g_return_if_fail (num_positions > 0); + g_return_if_fail (pos != NULL); + res = gst_structure_get_int (str, "channels", &channels); + g_return_if_fail (res); + g_return_if_fail (channels > 0); + + /* 0.8.x: channels=1 or channels=2 is mono/stereo, no positions needed + * there (we discard them anyway) */ + if (channels == 1 || channels == 2) + return; + + /* create the array of lists */ + g_value_init (&pos_val_arr, GST_TYPE_FIXED_LIST); + g_value_init (&pos_val_entry, GST_TYPE_AUDIO_CHANNEL_POSITION); + for (n = 0; n < channels; n++) { + g_value_init (&pos_val_list, GST_TYPE_LIST); + for (c = 0; c < num_positions; c++) { + g_value_set_enum (&pos_val_entry, pos[c]); + gst_value_list_append_value (&pos_val_list, &pos_val_entry); + } + gst_value_list_append_value (&pos_val_arr, &pos_val_list); + g_value_unset (&pos_val_list); + } + g_value_unset (&pos_val_entry); + gst_structure_set_value (str, GST_AUDIO_CHANNEL_POSITIONS_PROPERTY_NAME, + &pos_val_arr); + g_value_unset (&pos_val_arr); +} + +/* + * Helper function for below. The structure will be conserved, + * but might be cut down. Any additional structures that were + * created will be stored in the returned caps. + */ + +static GstCaps * +add_list_to_struct (GstStructure * str, + const GstAudioChannelPosition * pos, gint num_positions) +{ + GstCaps *caps = gst_caps_new_empty (); + const GValue *chan_val; + + chan_val = gst_structure_get_value (str, "channels"); + if (G_VALUE_TYPE (chan_val) == G_TYPE_INT) { + gst_audio_set_structure_channel_positions_list (str, pos, num_positions); + } else if (G_VALUE_TYPE (chan_val) == GST_TYPE_LIST) { + gint size; + const GValue *sub_val; + + size = gst_value_list_get_size (chan_val); + sub_val = gst_value_list_get_value (chan_val, 0); + gst_structure_set_value (str, "channels", sub_val); + gst_caps_append (caps, add_list_to_struct (str, pos, num_positions)); + while (--size > 0) { + str = gst_structure_copy (str); + sub_val = gst_value_list_get_value (chan_val, size); + gst_structure_set_value (str, "channels", sub_val); + gst_caps_append (caps, add_list_to_struct (str, pos, num_positions)); + gst_caps_append_structure (caps, str); + } + } else if (G_VALUE_TYPE (chan_val) == GST_TYPE_INT_RANGE) { + gint min, max; + + min = gst_value_get_int_range_min (chan_val); + max = gst_value_get_int_range_max (chan_val); + + gst_structure_set (str, "channels", G_TYPE_INT, min, NULL); + gst_audio_set_structure_channel_positions_list (str, pos, num_positions); + for (++min; min < max; min++) { + str = gst_structure_copy (str); + gst_structure_set (str, "channels", G_TYPE_INT, min, NULL); + gst_audio_set_structure_channel_positions_list (str, pos, num_positions); + gst_caps_append_structure (caps, str); + } + } else { + g_warning ("Unknown value type for channels property"); + } + + return caps; +} + +/** + * gst_audio_set_caps_channel_positions_list: + * @caps: #GstCaps to set the list of channel positions on. + * @pos: the array containing one or more possible audio + * channel positions that we should add in each value + * of the array in the given structure. + * @num_positions: the number of values in pos. + * + * Sets a (possibly non-fixed) list of possible audio channel + * positions (given in pos) on the given caps. Each of the + * structures of the caps, after this function has been called, + * will contain a "channel-positions" property with an array. + * Each value in the array will contain each of the values given + * in the pos array. Note that the size of the caps might be + * increased by this, since each structure with a "channel- + * positions" property needs to have a fixed "channels" property. + * The input caps is not required to have this. + */ + +void +gst_audio_set_caps_channel_positions_list (GstCaps * caps, + const GstAudioChannelPosition * pos, gint num_positions) +{ + gint size, n; + + /* get number of channels, general type checkups */ + g_return_if_fail (caps != NULL); + g_return_if_fail (num_positions > 0); + g_return_if_fail (pos != NULL); + + size = gst_caps_get_size (caps); + for (n = 0; n < size; n++) { + gst_caps_append (caps, add_list_to_struct (gst_caps_get_structure (caps, + n), pos, num_positions)); + } +} + +/** + * gst_audio_fixate_channel_positions: + * @str: a #GstStructure containing a (possibly unfixed) + * "channel-positions" property. + * + * Custom fixate function. Elements that implement some sort of + * channel conversion algorhithm should use this function for + * fixating on GstAudioChannelPosition properties. It will take + * care of equal channel positioning (left/right). Caller g_free()s + * the return value. The input properties may be (and are supposed + * to be) unfixed. + * Note that this function is mostly a hack because we currently + * have no way to add default fixation functions for new GTypes. + * + * Returns: fixed values that the caller could use as a fixed + * set of #GstAudioChannelPosition values. + */ + +GstAudioChannelPosition * +gst_audio_fixate_channel_positions (GstStructure * str) +{ + GstAudioChannelPosition *pos; + gint channels, n, num_unfixed = 0, i, c; + const GValue *pos_val_arr, *pos_val_entry, *pos_val; + gboolean res, is_stereo = TRUE; + GType t; + + /* + * We're going to do this cluelessly. We'll make an array of values that + * conflict with each other and, for each iteration in this array, pick + * either one until all unknown values are filled. This might not work in + * corner cases but should work OK for the general case. + */ + struct + { + GstAudioChannelPosition pos1[2]; + GstAudioChannelPosition pos2[1]; + } conf[] = { + /* front: mono <-> stereo */ + { { + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, { + GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}}, + /* front center: 2 <-> 1 */ + { { + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, { + GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}}, + /* rear: 2 <-> 1 */ + { { + GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, + GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, { + GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}}, { { + GST_AUDIO_CHANNEL_POSITION_INVALID, GST_AUDIO_CHANNEL_POSITION_INVALID}, { + GST_AUDIO_CHANNEL_POSITION_LFE}}, { { + GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT, + GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}, { + GST_AUDIO_CHANNEL_POSITION_INVALID}}, { { + GST_AUDIO_CHANNEL_POSITION_INVALID, GST_AUDIO_CHANNEL_POSITION_INVALID}, { + GST_AUDIO_CHANNEL_POSITION_INVALID}} + }; + struct + { + gint num_opt[3]; + guint num_opts[3]; + gboolean is_fixed[3]; + gint choice; /* -1 is none, 0 is the two, 1 is the one */ + } opt; + + /* get number of channels, general type checkups */ + g_return_val_if_fail (str != NULL, NULL); + res = gst_structure_get_int (str, "channels", &channels); + g_return_val_if_fail (res, NULL); + g_return_val_if_fail (channels > 0, NULL); + + /* 0.8.x mono/stereo checks */ + pos_val_arr = gst_structure_get_value (str, + GST_AUDIO_CHANNEL_POSITIONS_PROPERTY_NAME); + if (!pos_val_arr && (channels == 1 || channels == 2)) { + pos = g_new (GstAudioChannelPosition, channels); + if (channels == 1) { + pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO; + } else { + pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; + pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; + } + return pos; + } + g_return_val_if_fail (pos_val_arr != NULL, NULL); + g_return_val_if_fail (gst_value_list_get_size (pos_val_arr) == channels, + NULL); + for (n = 0; n < channels; n++) { + t = G_VALUE_TYPE (gst_value_list_get_value (pos_val_arr, n)); + g_return_val_if_fail (t == GST_TYPE_LIST || + t == GST_TYPE_AUDIO_CHANNEL_POSITION, NULL); + } + + /* all unknown, to start with */ + pos = g_new (GstAudioChannelPosition, channels); + for (n = 0; n < channels; n++) + pos[n] = GST_AUDIO_CHANNEL_POSITION_INVALID; + num_unfixed = channels; + + /* Iterate the array of conflicting values */ + for (i = 0; conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID || + conf[i].pos2[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; i++) { + /* front/center only important if not mono (obviously) */ + if (conf[i].pos1[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER && + !is_stereo) + continue; + + /* init values */ + for (n = 0; n < 3; n++) { + opt.num_opt[n] = -1; + opt.num_opts[n] = -1; + opt.is_fixed[n] = FALSE; + } + + /* Now, we'll see for each channel if it allows for any of the values in + * the set of conflicting audio channel positions and keep scores. */ + for (n = 0; n < channels; n++) { + /* if the channel is already taken, don't bother */ + if (pos[n] != GST_AUDIO_CHANNEL_POSITION_INVALID) + continue; + + pos_val_entry = gst_value_list_get_value (pos_val_arr, n); + t = G_VALUE_TYPE (pos_val_entry); + if (t == GST_TYPE_LIST) { + /* This algorhythm is suboptimal. */ + for (c = 0; c < gst_value_list_get_size (pos_val_entry); c++) { + pos_val = gst_value_list_get_value (pos_val_entry, c); + if (g_value_get_enum (pos_val) == conf[i].pos1[0] && + opt.num_opts[0] > gst_value_list_get_size (pos_val_entry) && + !opt.is_fixed[0]) { + /* Now test if the old position of num_opt[0] also allows for + * the other channel (which was skipped previously). If so, + * keep score. */ + if (opt.num_opt[0] != -1) { + gint c1; + + pos_val_entry = gst_value_list_get_value (pos_val_arr, + opt.num_opt[0]); + if (G_VALUE_TYPE (pos_val_entry) == GST_TYPE_LIST) { + for (c1 = 0; c1 < gst_value_list_get_size (pos_val_entry); c1++) { + pos_val = gst_value_list_get_value (pos_val_entry, c1); + if (g_value_get_enum (pos_val) == conf[i].pos1[1] && + opt.num_opts[1] > opt.num_opts[0] && !opt.is_fixed[1]) { + opt.num_opts[1] = opt.num_opts[0]; + opt.num_opt[1] = opt.num_opt[0]; + } + } + pos_val = gst_value_list_get_value (pos_val_entry, c); + } + pos_val_entry = gst_value_list_get_value (pos_val_arr, n); + } + + /* and save values */ + opt.num_opts[0] = gst_value_list_get_size (pos_val_entry); + opt.num_opt[0] = n; + } else if (g_value_get_enum (pos_val) == conf[i].pos1[1] && + opt.num_opts[1] > gst_value_list_get_size (pos_val_entry) && + !opt.is_fixed[1] && n != opt.num_opt[0]) { + opt.num_opts[1] = gst_value_list_get_size (pos_val_entry); + opt.num_opt[1] = n; + } + + /* 2 goes separately, because 0/1 vs. 2 are separate */ + if (g_value_get_enum (pos_val) == conf[i].pos2[0] && + opt.num_opts[2] > gst_value_list_get_size (pos_val_entry) && + !opt.is_fixed[2]) { + opt.num_opts[2] = gst_value_list_get_size (pos_val_entry); + opt.num_opt[2] = n; + } + } + } else { + if (g_value_get_enum (pos_val_entry) == conf[i].pos1[0]) { + opt.num_opt[0] = n; + opt.is_fixed[0] = TRUE; + } else if (g_value_get_enum (pos_val_entry) == conf[i].pos1[1]) { + opt.num_opt[1] = n; + opt.is_fixed[1] = TRUE; + } else if (g_value_get_enum (pos_val_entry) == conf[i].pos2[0]) { + opt.num_opt[2] = n; + opt.is_fixed[2] = TRUE; + } + } + } + + /* check our results and choose either one */ + if ((opt.is_fixed[0] || opt.is_fixed[1]) && opt.is_fixed[2]) { + g_warning ("Pre-fixated on both %d/%d and %d - conflict!", + conf[i].pos1[0], conf[i].pos1[1], conf[i].pos2[0]); + g_free (pos); + return NULL; + } else if ((opt.is_fixed[0] && opt.num_opt[1] == -1) || + (opt.is_fixed[1] && opt.num_opt[0] == -1)) { + g_warning ("Pre-fixated one side, but other side n/a of %d/%d", + conf[i].pos1[0], conf[i].pos1[1]); + g_free (pos); + return NULL; + } else if (opt.is_fixed[0] || opt.is_fixed[1]) { + opt.choice = 0; + } else if (opt.is_fixed[2]) { + opt.choice = 1; + } else if (opt.num_opt[0] != -1 && opt.num_opt[1] != -1) { + opt.choice = 0; + } else if (opt.num_opt[2] != -1) { + opt.choice = 1; + } else { + opt.choice = -1; + } + + /* stereo? Note that we keep is_stereo to TRUE if we didn't decide on + * any arrangement. The mono/stereo channels might be handled elsewhere + * which is clearly outside the scope of this element, so we cannot + * know and expect the application to handle that then. */ + if (conf[i].pos2[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_MONO && + opt.choice == 1) { + is_stereo = FALSE; + } + + /* now actually decide what we'll do and fixate on that */ + if (opt.choice == 0) { + g_assert (conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID && + conf[i].pos1[1] != GST_AUDIO_CHANNEL_POSITION_INVALID); + pos[opt.num_opt[0]] = conf[i].pos1[0]; + pos[opt.num_opt[1]] = conf[i].pos1[1]; + num_unfixed -= 2; + } else if (opt.choice == 1) { + g_assert (conf[i].pos2[0] != GST_AUDIO_CHANNEL_POSITION_INVALID); + pos[opt.num_opt[2]] = conf[i].pos2[0]; + num_unfixed--; + } + } + + /* safety check */ + if (num_unfixed > 0) { + g_warning ("%d unfixed channel positions left after fixation!", + num_unfixed); + g_free (pos); + return NULL; + } + + if (!gst_audio_check_channel_positions (pos, channels)) { + g_free (pos); + return NULL; + } + + return pos; +} diff --git a/gst-libs/gst/audio/multichannel.h b/gst-libs/gst/audio/multichannel.h new file mode 100644 index 00000000..5194fffa --- /dev/null +++ b/gst-libs/gst/audio/multichannel.h @@ -0,0 +1,86 @@ +/* GStreamer Multichannel-Audio helper functions + * (c) 2004 Ronald Bultje + * + * 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. + */ + +#ifndef __GST_AUDIO_MULTICHANNEL_H__ +#define __GST_AUDIO_MULTICHANNEL_H__ + +#include +#include + +typedef enum { + GST_AUDIO_CHANNEL_POSITION_INVALID = -1, + + /* Main front speakers. Mono and left/right are mututally exclusive! */ + GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, + + /* rear. Left/right and center are mututally exclusive! */ + GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, + GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, + GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT, + + /* subwoofer/low-frequency */ + GST_AUDIO_CHANNEL_POSITION_LFE, + + /* Center front speakers. Center and left/right_of_center cannot be + * used together! */ + GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, + GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER, + + /* sides */ + GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT, + GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT, + + /* don't use - counter */ + GST_AUDIO_CHANNEL_POSITION_NUM +} GstAudioChannelPosition; + +/* Retrieves or sets the positions from/to a GstStructure. Only + * works with fixed caps, caller should check for that! Caller + * g_free()s result of the getter. */ +GstAudioChannelPosition * + gst_audio_get_channel_positions (GstStructure *str); +void gst_audio_set_channel_positions (GstStructure *str, + const GstAudioChannelPosition *pos); + +/* Sets a (non-fixed) list of possible audio channel positions + * on a structure (this requires the "channels" property to + * be fixed!) or on a caps (here, the "channels" property may be + * unfixed and the caps may even contain multiple structures). */ +void gst_audio_set_structure_channel_positions_list + (GstStructure *str, + const GstAudioChannelPosition *pos, + gint num_positions); +void gst_audio_set_caps_channel_positions_list + (GstCaps *caps, + const GstAudioChannelPosition *pos, + gint num_positions); + +/* Custom fixate function. Elements that implement some sort of + * channel conversion algorhithm should use this function for + * fixating on GstAudioChannelPosition properties. It will take + * care of equal channel positioning (left/right). Caller g_free()s + * the return value. The input properties may be (and are supposed + * to be) unfixed. */ +GstAudioChannelPosition * + gst_audio_fixate_channel_positions (GstStructure *str); + +#endif /* __GST_AUDIO_MULTICHANNEL_H__ */ diff --git a/gst-libs/gst/audio/testchannels.c b/gst-libs/gst/audio/testchannels.c new file mode 100644 index 00000000..b886c820 --- /dev/null +++ b/gst-libs/gst/audio/testchannels.c @@ -0,0 +1,55 @@ +/* GStreamer Multichannel Test + * (c) 2004 Ronald Bultje + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include +#include + +gint +main (gint argc, gchar * argv[]) +{ + gchar *str; + GstCaps *caps; + GstAudioChannelPosition pos[2] = { GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, + GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT + }; + + /* register multichannel type */ + gst_init (&argc, &argv); + gst_audio_channel_position_get_type (); + + /* test some caps-string conversions */ + caps = gst_caps_new_simple ("audio/x-raw-int", + "channels", G_TYPE_INT, 2, NULL); + str = gst_caps_to_string (caps); + g_print ("Test caps #1: %s\n", str); + g_free (str); + gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos); + str = gst_caps_to_string (caps); + g_print ("Test caps #2: %s\n", str); + g_free (str); + gst_caps_free (caps); + + return 0; +} -- cgit v1.2.1