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.
This commit is contained in:
Ronald S. Bultje 2004-11-25 20:36:29 +00:00
parent 78cfd8e8af
commit 3a0a2898af
12 changed files with 1184 additions and 30 deletions

View file

@ -1,3 +1,37 @@
2004-11-25 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
* 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 <rbultje@ronald.bitfreak.net> 2004-11-25 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
* ext/ogg/gstoggdemux.c: (gst_ogg_demux_push): * ext/ogg/gstoggdemux.c: (gst_ogg_demux_push):

View file

@ -26,6 +26,7 @@
#include <sys/time.h> #include <sys/time.h>
#include "gst/gst-i18n-plugin.h" #include "gst/gst-i18n-plugin.h"
#include <gst/audio/multichannel.h>
#include "gst/propertyprobe/propertyprobe.h" #include "gst/propertyprobe/propertyprobe.h"
#include "gstalsa.h" #include "gstalsa.h"
#include "gstalsaclock.h" #include "gstalsaclock.h"
@ -853,14 +854,55 @@ gst_alsa_get_caps (GstPad * pad)
/* we can never use a format we can't set caps for */ /* we can never use a format we can't set caps for */
if (caps != NULL) { if (caps != NULL) {
gint n;
g_assert (gst_caps_get_size (caps) == 1); g_assert (gst_caps_get_size (caps) == 1);
add_channels (gst_caps_get_structure (caps, 0), min_rate, max_rate, add_channels (gst_caps_get_structure (caps, 0), min_rate, max_rate,
min_channels, max_channels); min_channels, max_channels);
if (ret) {
gst_caps_append (ret, caps); /* channel configuration */
} else { for (n = min_channels; n < max_channels; n++) {
ret = caps; if (snd_pcm_hw_params_test_channels (this->handle, hw_params, n) == 0) {
GstStructure *str;
GstAudioChannelPosition pos[8] = {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
GST_AUDIO_CHANNEL_POSITION_LFE,
GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
};
switch (n) {
case 1:
pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO;
break;
case 2:
case 4:
case 6:
case 8:
/* keep above */
break;
default:
/* unsupported */
pos[0] = GST_AUDIO_CHANNEL_POSITION_INVALID;
break;
}
if (pos[0] != GST_AUDIO_CHANNEL_POSITION_INVALID) {
str = gst_structure_copy (gst_caps_get_structure (caps, 0));
gst_structure_set (str, "channels", G_TYPE_INT, n, NULL);
gst_audio_set_channel_positions (str, pos);
if (!ret) {
ret = gst_caps_new_empty ();
}
gst_caps_append_structure (ret, str);
}
}
} }
gst_caps_free (caps);
} }
} }
} }

View file

@ -56,6 +56,9 @@ plugin_init (GstPlugin * plugin)
{ {
int err; int err;
if (!gst_library_load ("gstaudio"))
return FALSE;
if (!gst_element_register (plugin, "alsamixer", GST_RANK_NONE, if (!gst_element_register (plugin, "alsamixer", GST_RANK_NONE,
GST_TYPE_ALSA_MIXER)) GST_TYPE_ALSA_MIXER))
return FALSE; return FALSE;

View file

@ -33,10 +33,8 @@ GST_DEBUG_CATEGORY (vorbisparse_debug);
static gboolean static gboolean
plugin_init (GstPlugin * plugin) plugin_init (GstPlugin * plugin)
{ {
if (!gst_library_load ("gstbytestream")) if (!gst_library_load ("gstbytestream") ||
return FALSE; !gst_library_load ("gstaudio") || !gst_library_load ("gsttags"))
if (!gst_library_load ("gsttags"))
return FALSE; return FALSE;
if (!gst_element_register (plugin, "vorbisenc", GST_RANK_NONE, if (!gst_element_register (plugin, "vorbisenc", GST_RANK_NONE,

View file

@ -24,6 +24,7 @@
#include "vorbisdec.h" #include "vorbisdec.h"
#include <string.h> #include <string.h>
#include <gst/tag/tag.h> #include <gst/tag/tag.h>
#include <gst/audio/multichannel.h>
GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug); GST_DEBUG_CATEGORY_EXTERN (vorbisdec_debug);
#define GST_CAT_DEFAULT vorbisdec_debug #define GST_CAT_DEFAULT vorbisdec_debug
@ -53,7 +54,7 @@ GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_ALWAYS, GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-raw-float, " GST_STATIC_CAPS ("audio/x-raw-float, "
"rate = (int) [ 8000, 50000 ], " "rate = (int) [ 8000, 50000 ], "
"channels = (int) [ 1, 2 ], " "endianness = (int) BYTE_ORDER, " "channels = (int) [ 1, 6 ], " "endianness = (int) BYTE_ORDER, "
/* no ifdef in macros, please /* no ifdef in macros, please
#ifdef GST_VORBIS_DEC_SEQUENTIAL #ifdef GST_VORBIS_DEC_SEQUENTIAL
"layout = \"sequential\", " "layout = \"sequential\", "
@ -431,6 +432,7 @@ vorbis_dec_chain (GstPad * pad, GstData * data)
gst_element_found_tags_for_pad (GST_ELEMENT (vd), vd->srcpad, 0, list); gst_element_found_tags_for_pad (GST_ELEMENT (vd), vd->srcpad, 0, list);
} else if (packet.packetno == 2) { } else if (packet.packetno == 2) {
GstCaps *caps; GstCaps *caps;
const GstAudioChannelPosition *pos = NULL;
/* done */ /* done */
vorbis_synthesis_init (&vd->vd, &vd->vi); vorbis_synthesis_init (&vd->vd, &vd->vi);
@ -440,6 +442,63 @@ vorbis_dec_chain (GstPad * pad, GstData * data)
"channels", G_TYPE_INT, vd->vi.channels, "channels", G_TYPE_INT, vd->vi.channels,
"endianness", G_TYPE_INT, G_BYTE_ORDER, "endianness", G_TYPE_INT, G_BYTE_ORDER,
"width", G_TYPE_INT, 32, "buffer-frames", G_TYPE_INT, 0, NULL); "width", G_TYPE_INT, 32, "buffer-frames", G_TYPE_INT, 0, NULL);
switch (vd->vi.channels) {
case 1:
case 2:
/* nothing */
break;
case 3:{
static GstAudioChannelPosition pos3[] = {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
};
pos = pos3;
break;
}
case 4:{
static GstAudioChannelPosition pos4[] = {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
};
pos = pos4;
break;
}
case 5:{
static GstAudioChannelPosition pos5[] = {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
};
pos = pos5;
break;
}
case 6:{
static GstAudioChannelPosition pos6[] = {
GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
GST_AUDIO_CHANNEL_POSITION_LFE
};
pos = pos6;
break;
}
default:
gst_data_unref (data);
gst_caps_free (caps);
GST_ELEMENT_ERROR (vd, STREAM, NOT_IMPLEMENTED, (NULL),
("Unsupported channel count %d", vd->vi.channels));
return;
}
if (pos) {
gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
}
if (!gst_pad_set_explicit_caps (vd->srcpad, caps)) { if (!gst_pad_set_explicit_caps (vd->srcpad, caps)) {
gst_caps_free (caps); gst_caps_free (caps);
return; return;

View file

@ -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@ librarydir = $(libdir)/gstreamer-@GST_MAJORMINOR@
library_LTLIBRARIES = libgstaudio.la libgstaudiofilter.la library_LTLIBRARIES = libgstaudio.la libgstaudiofilter.la
noinst_LTLIBRARIES = libgstaudiofilterexample.la noinst_LTLIBRARIES = libgstaudiofilterexample.la
EXTRA_DIST = gstaudiofiltertemplate.c make_filter 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 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_LIBADD =
libgstaudio_la_CFLAGS = $(GST_CFLAGS) libgstaudio_la_CFLAGS = $(GST_CFLAGS)
@ -25,3 +42,10 @@ libgstaudiofilterexample_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
gstaudiofilterexample.c: $(srcdir)/make_filter $(srcdir)/gstaudiofiltertemplate.c gstaudiofilterexample.c: $(srcdir)/make_filter $(srcdir)/gstaudiofiltertemplate.c
$(srcdir)/make_filter AudiofilterExample $(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

View file

@ -22,6 +22,7 @@
#endif #endif
#include "audio.h" #include "audio.h"
#include "multichannel-enumtypes.h"
#include <gst/gststructure.h> #include <gst/gststructure.h>
@ -267,6 +268,8 @@ gst_audio_structure_set_int (GstStructure * structure, GstAudioFieldFlag flag)
static gboolean static gboolean
plugin_init (GstPlugin * plugin) plugin_init (GstPlugin * plugin)
{ {
gst_audio_channel_position_get_type ();
return TRUE; return TRUE;
} }

View file

@ -0,0 +1,634 @@
/* GStreamer Multichannel-Audio helper functions
* (c) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* 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 <config.h>
#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;
}

View file

@ -0,0 +1,86 @@
/* GStreamer Multichannel-Audio helper functions
* (c) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* 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 <gst/audio/audio.h>
#include <gst/audio/multichannel-enumtypes.h>
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__ */

View file

@ -0,0 +1,55 @@
/* GStreamer Multichannel Test
* (c) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
*
* 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 <config.h>
#endif
#include <gst/gst.h>
#include <multichannel.c>
#include <multichannel-enumtypes.c>
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;
}

View file

@ -37,6 +37,7 @@
#endif #endif
#include <gst/gst.h> #include <gst/gst.h>
#include <gst/audio/multichannel.h>
#include <string.h> #include <string.h>
#include "plugin.h" #include "plugin.h"
@ -64,6 +65,7 @@ struct _GstAudioConvertCaps
gint width; gint width;
gint rate; gint rate;
gint channels; gint channels;
GstAudioChannelPosition *pos;
/* int audio caps */ /* int audio caps */
gboolean sign; gboolean sign;
@ -104,6 +106,7 @@ static GstElementDetails audio_convert_details = {
static void gst_audio_convert_base_init (gpointer g_class); static void gst_audio_convert_base_init (gpointer g_class);
static void gst_audio_convert_class_init (GstAudioConvertClass * klass); static void gst_audio_convert_class_init (GstAudioConvertClass * klass);
static void gst_audio_convert_init (GstAudioConvert * audio_convert); static void gst_audio_convert_init (GstAudioConvert * audio_convert);
static void gst_audio_convert_dispose (GObject * obj);
/* gstreamer functions */ /* gstreamer functions */
static void gst_audio_convert_chain (GstPad * pad, GstData * _data); static void gst_audio_convert_chain (GstPad * pad, GstData * _data);
@ -147,33 +150,35 @@ GST_BOILERPLATE_FULL (GstAudioConvert, gst_audio_convert, GstElement,
GST_STATIC_CAPS ( \ GST_STATIC_CAPS ( \
"audio/x-raw-int, " \ "audio/x-raw-int, " \
"rate = (int) [ 1, MAX ], " \ "rate = (int) [ 1, MAX ], " \
"channels = (int) [ 1, 2 ], " \ "channels = (int) [ 1, 8 ], " \
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \ "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
"width = (int) 8, " \ "width = (int) 8, " \
"depth = (int) [ 1, 8 ], " \ "depth = (int) [ 1, 8 ], " \
"signed = (boolean) { true, false }; " \ "signed = (boolean) { true, false }; " \
"audio/x-raw-int, " \ "audio/x-raw-int, " \
"rate = (int) [ 1, MAX ], " \ "rate = (int) [ 1, MAX ], " \
"channels = (int) [ 1, 2 ], " \ "channels = (int) [ 1, 8 ], " \
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \ "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
"width = (int) 16, " \ "width = (int) 16, " \
"depth = (int) [ 1, 16 ], " \ "depth = (int) [ 1, 16 ], " \
"signed = (boolean) { true, false }; " \ "signed = (boolean) { true, false }; " \
"audio/x-raw-int, " \ "audio/x-raw-int, " \
"rate = (int) [ 1, MAX ], " \ "rate = (int) [ 1, MAX ], " \
"channels = (int) [ 1, 2 ], " \ "channels = (int) [ 1, 8 ], " \
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \ "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
"width = (int) 32, " \ "width = (int) 32, " \
"depth = (int) [ 1, 32 ], " \ "depth = (int) [ 1, 32 ], " \
"signed = (boolean) { true, false }; " \ "signed = (boolean) { true, false }; " \
"audio/x-raw-float, " \ "audio/x-raw-float, " \
"rate = (int) [ 1, MAX ], " \ "rate = (int) [ 1, MAX ], " \
"channels = (int) [ 1, 2 ], " \ "channels = (int) [ 1, 8 ], " \
"endianness = (int) BYTE_ORDER, " \ "endianness = (int) BYTE_ORDER, " \
"width = (int) 32, " \ "width = (int) 32, " \
"buffer-frames = (int) [ 0, MAX ]" \ "buffer-frames = (int) [ 0, MAX ]" \
) )
static GstAudioChannelPosition *supported_positions;
static GstStaticPadTemplate gst_audio_convert_src_template = static GstStaticPadTemplate gst_audio_convert_src_template =
GST_STATIC_PAD_TEMPLATE ("src", GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC, GST_PAD_SRC,
@ -204,8 +209,16 @@ static void
gst_audio_convert_class_init (GstAudioConvertClass * klass) gst_audio_convert_class_init (GstAudioConvertClass * klass)
{ {
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass); GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gint i;
gstelement_class->change_state = gst_audio_convert_change_state; gstelement_class->change_state = gst_audio_convert_change_state;
gobject_class->dispose = gst_audio_convert_dispose;
supported_positions = g_new0 (GstAudioChannelPosition,
GST_AUDIO_CHANNEL_POSITION_NUM);
for (i = 0; i < GST_AUDIO_CHANNEL_POSITION_NUM; i++)
supported_positions[i] = i;
} }
static void static void
@ -233,6 +246,24 @@ gst_audio_convert_init (GstAudioConvert * this)
/* clear important variables */ /* clear important variables */
this->convert_internal = NULL; this->convert_internal = NULL;
this->sinkcaps.pos = NULL;
this->srccaps.pos = NULL;
}
static void
gst_audio_convert_dispose (GObject * obj)
{
GstAudioConvert *this = GST_AUDIO_CONVERT (obj);
if (this->sinkcaps.pos) {
g_free (this->sinkcaps.pos);
this->sinkcaps.pos = NULL;
}
if (this->srccaps.pos) {
g_free (this->srccaps.pos);
this->srccaps.pos = NULL;
}
} }
/*** GSTREAMER FUNCTIONS ******************************************************/ /*** GSTREAMER FUNCTIONS ******************************************************/
@ -310,6 +341,7 @@ gst_audio_convert_getcaps (GstPad * pad)
for (i = size - 1; i >= 0; i--) { for (i = size - 1; i >= 0; i--) {
structure = gst_caps_get_structure (othercaps, i); structure = gst_caps_get_structure (othercaps, i);
gst_structure_remove_field (structure, "channels"); gst_structure_remove_field (structure, "channels");
gst_structure_remove_field (structure, "channel-positions");
gst_structure_remove_field (structure, "endianness"); gst_structure_remove_field (structure, "endianness");
gst_structure_remove_field (structure, "width"); gst_structure_remove_field (structure, "width");
gst_structure_remove_field (structure, "depth"); gst_structure_remove_field (structure, "depth");
@ -332,6 +364,10 @@ gst_audio_convert_getcaps (GstPad * pad)
caps = gst_caps_intersect (othercaps, templcaps); caps = gst_caps_intersect (othercaps, templcaps);
gst_caps_free (othercaps); gst_caps_free (othercaps);
/* Get the channel positions in as well. */
gst_audio_set_caps_channel_positions_list (caps, supported_positions,
GST_AUDIO_CHANNEL_POSITION_NUM);
return caps; return caps;
} }
@ -344,10 +380,17 @@ gst_audio_convert_parse_caps (const GstCaps * gst_caps,
g_return_val_if_fail (gst_caps_is_fixed (gst_caps), FALSE); g_return_val_if_fail (gst_caps_is_fixed (gst_caps), FALSE);
g_return_val_if_fail (caps != NULL, FALSE); g_return_val_if_fail (caps != NULL, FALSE);
/* cleanup old */
if (caps->pos) {
g_free (caps->pos);
caps->pos = NULL;
}
caps->endianness = G_BYTE_ORDER; caps->endianness = G_BYTE_ORDER;
caps->is_int = caps->is_int =
(strcmp (gst_structure_get_name (structure), "audio/x-raw-int") == 0); (strcmp (gst_structure_get_name (structure), "audio/x-raw-int") == 0);
if (!gst_structure_get_int (structure, "channels", &caps->channels) if (!gst_structure_get_int (structure, "channels", &caps->channels)
|| !(caps->pos = gst_audio_get_channel_positions (structure))
|| !gst_structure_get_int (structure, "width", &caps->width) || !gst_structure_get_int (structure, "width", &caps->width)
|| !gst_structure_get_int (structure, "rate", &caps->rate) || !gst_structure_get_int (structure, "rate", &caps->rate)
|| (caps->is_int || (caps->is_int
@ -359,10 +402,14 @@ gst_audio_convert_parse_caps (const GstCaps * gst_caps,
&& !gst_structure_get_int (structure, "buffer-frames", && !gst_structure_get_int (structure, "buffer-frames",
&caps->buffer_frames))) { &caps->buffer_frames))) {
GST_DEBUG ("could not get some values from structure"); GST_DEBUG ("could not get some values from structure");
g_free (caps->pos);
caps->pos = NULL;
return FALSE; return FALSE;
} }
if (caps->is_int && caps->depth > caps->width) { if (caps->is_int && caps->depth > caps->width) {
GST_DEBUG ("width > depth, not allowed - make us advertise correct caps"); GST_DEBUG ("width > depth, not allowed - make us advertise correct caps");
g_free (caps->pos);
caps->pos = NULL;
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
@ -386,6 +433,7 @@ gst_audio_convert_link (GstPad * pad, const GstCaps * caps)
this = GST_AUDIO_CONVERT (GST_OBJECT_PARENT (pad)); this = GST_AUDIO_CONVERT (GST_OBJECT_PARENT (pad));
otherpad = (pad == this->src ? this->sink : this->src); otherpad = (pad == this->src ? this->sink : this->src);
ac_caps.pos = NULL;
if (!gst_audio_convert_parse_caps (caps, &ac_caps)) if (!gst_audio_convert_parse_caps (caps, &ac_caps))
return GST_PAD_LINK_REFUSED; return GST_PAD_LINK_REFUSED;
@ -406,11 +454,14 @@ gst_audio_convert_link (GstPad * pad, const GstCaps * caps)
} }
} }
if (this->sink == pad) { if (this->sink == pad) {
g_free (this->sinkcaps.pos);
this->sinkcaps = ac_caps; this->sinkcaps = ac_caps;
} else { } else {
g_free (this->srccaps.pos);
this->srccaps = ac_caps; this->srccaps = ac_caps;
} }
GST_LOG_OBJECT (this, "trying to set caps to %" GST_PTR_FORMAT, othercaps); GST_LOG_OBJECT (this, "trying to set caps to %" GST_PTR_FORMAT, othercaps);
ret = gst_pad_try_set_caps_nonfixed (otherpad, othercaps); ret = gst_pad_try_set_caps_nonfixed (otherpad, othercaps);
gst_caps_free (othercaps); gst_caps_free (othercaps);
if (ret < GST_PAD_LINK_OK) if (ret < GST_PAD_LINK_OK)
@ -419,18 +470,24 @@ gst_audio_convert_link (GstPad * pad, const GstCaps * caps)
/* woohoo, got it */ /* woohoo, got it */
othercaps = (GstCaps *) gst_pad_get_negotiated_caps (otherpad); othercaps = (GstCaps *) gst_pad_get_negotiated_caps (otherpad);
if (othercaps) { if (othercaps) {
other_ac_caps.pos = NULL;
if (!gst_audio_convert_parse_caps (othercaps, &other_ac_caps)) { if (!gst_audio_convert_parse_caps (othercaps, &other_ac_caps)) {
g_critical ("internal negotiation error"); g_critical ("internal negotiation error");
return GST_PAD_LINK_REFUSED; return GST_PAD_LINK_REFUSED;
} }
} else { } else {
other_ac_caps = ac_caps; other_ac_caps = ac_caps;
other_ac_caps.pos = g_memdup (ac_caps.pos,
ac_caps.channels * sizeof (GstAudioChannelPosition));
} }
if (this->sink == pad) { if (this->sink == pad) {
g_free (this->srccaps.pos);
this->srccaps = other_ac_caps; this->srccaps = other_ac_caps;
this->sinkcaps = ac_caps; this->sinkcaps = ac_caps;
} else { } else {
g_free (this->sinkcaps.pos);
this->srccaps = ac_caps; this->srccaps = ac_caps;
this->sinkcaps = other_ac_caps; this->sinkcaps = other_ac_caps;
} }
@ -489,6 +546,7 @@ _fixate_caps_to_int (GstCaps ** caps, const gchar * field, gint value)
static GstCaps * static GstCaps *
gst_audio_convert_fixate (GstPad * pad, const GstCaps * caps) gst_audio_convert_fixate (GstPad * pad, const GstCaps * caps)
{ {
const GValue *pos_val;
GstAudioConvert *this = GstAudioConvert *this =
GST_AUDIO_CONVERT (gst_object_get_parent (GST_OBJECT (pad))); GST_AUDIO_CONVERT (gst_object_get_parent (GST_OBJECT (pad)));
GstPad *otherpad = (pad == this->sink ? this->src : this->sink); GstPad *otherpad = (pad == this->sink ? this->src : this->sink);
@ -508,15 +566,63 @@ gst_audio_convert_fixate (GstPad * pad, const GstCaps * caps)
try.endianness = ac_caps.is_int ? ac_caps.endianness : G_BYTE_ORDER; try.endianness = ac_caps.is_int ? ac_caps.endianness : G_BYTE_ORDER;
} }
if (_fixate_caps_to_int (&copy, "channels", try.channels)) if (_fixate_caps_to_int (&copy, "channels", try.channels)) {
int n;
if (try.channels > 2) {
/* make sure we have a channelpositions structure or array here */
GstStructure *str;
for (n = 0; n < gst_caps_get_size (copy); n++) {
str = gst_caps_get_structure (copy, n);
if (!gst_structure_get_value (str, "channel-positions")) {
/* first try otherpad's positions, else anything */
if (ac_caps.pos != NULL) {
gst_audio_set_channel_positions (str, ac_caps.pos);
} else {
gst_audio_set_structure_channel_positions_list (str,
supported_positions, GST_AUDIO_CHANNEL_POSITION_NUM);
/* FIXME: fixate (else we'll be less fixed than we used to) */
}
}
}
} else {
/* make sure we don't */
for (n = 0; n < gst_caps_get_size (copy); n++) {
gst_structure_remove_field (gst_caps_get_structure (copy, n),
"channel-positions");
}
}
return copy; return copy;
}
if (_fixate_caps_to_int (&copy, "width", try.width)) if (_fixate_caps_to_int (&copy, "width", try.width))
return copy; return copy;
if (_fixate_caps_to_int (&copy, "depth", try.depth)) if (_fixate_caps_to_int (&copy, "depth", try.depth))
return copy; return copy;
if (_fixate_caps_to_int (&copy, "endianness", try.endianness)) if (_fixate_caps_to_int (&copy, "endianness", try.endianness))
return copy; return copy;
if ((pos_val = gst_structure_get_value (gst_caps_get_structure (copy, 0),
"channel-positions")) != NULL) {
GstAudioChannelPosition *pos;
const GValue *pos_val_entry;
gint i;
for (i = 0; i < gst_value_list_get_size (pos_val); i++) {
pos_val_entry = gst_value_list_get_value (pos_val, i);
if (G_VALUE_TYPE (pos_val_entry) == GST_TYPE_LIST) {
/* unfixed */
pos =
gst_audio_fixate_channel_positions (gst_caps_get_structure (copy,
0));
if (pos) {
gst_audio_set_channel_positions (gst_caps_get_structure (copy, 0),
pos);
g_free (pos);
return copy;
}
}
}
}
gst_caps_free (copy); gst_caps_free (copy);
return NULL; return NULL;
@ -786,28 +892,135 @@ static GstBuffer *
gst_audio_convert_channels (GstAudioConvert * this, GstBuffer * buf) gst_audio_convert_channels (GstAudioConvert * this, GstBuffer * buf)
{ {
GstBuffer *ret; GstBuffer *ret;
gint i, count; gint c, i, count, ci, co;
gint32 *src, *dest; gint32 *src, *dest;
if (this->sinkcaps.channels == this->srccaps.channels) /* Conversions from one-channel to compatible two-channel configs */
return buf; struct
{
GstAudioChannelPosition pos1[2];
GstAudioChannelPosition pos2[1];
} conv[] = {
/* 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}}
};
gboolean set[8] = { FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE };
if (this->sinkcaps.channels == this->srccaps.channels) {
for (i = 0; i < this->sinkcaps.channels; i++) {
if (this->sinkcaps.pos[i] != this->srccaps.pos[i])
break;
}
if (i == this->sinkcaps.channels)
return buf;
}
count = GST_BUFFER_SIZE (buf) / 4 / this->sinkcaps.channels; count = GST_BUFFER_SIZE (buf) / 4 / this->sinkcaps.channels;
ret = gst_audio_convert_get_buffer (buf, count * 4 * this->srccaps.channels); ret = gst_audio_convert_get_buffer (buf, count * 4 * this->srccaps.channels);
/* conversions from compatible (but not the same) channel schemes. This
* goes two ways: if the sink has both pos1[0,1] and src has pos2[0] or
* if the src has both pos1[0,1] and sink has pos2[0], then we do the
* conversion. We hereby assume that the existance of pos1[0,1] and
* pos2[0] are mututally exclusive. There are no checks for that,
* unfortunately. This shouldn't lead to issues (like crashes or so),
* though. */
for (c = 0; conv[c].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; c++) {
gint pos1_0 = -1, pos1_1 = -1, pos2_0 = -1, n;
/* Try to go from the given 2 channels to the given 1 channel */
for (n = 0; n < this->sinkcaps.channels; n++) {
if (this->sinkcaps.pos[n] == conv[c].pos1[0])
pos1_0 = n;
else if (this->sinkcaps.pos[n] == conv[c].pos1[1])
pos1_1 = n;
}
for (n = 0; n < this->srccaps.channels; n++) {
if (this->srccaps.pos[n] == conv[c].pos2[0])
pos2_0 = n;
}
if (pos1_0 != -1 && pos1_1 != -1 && pos2_0 != -1) {
src = (gint32 *) GST_BUFFER_DATA (buf);
dest = (gint32 *) GST_BUFFER_DATA (ret);
for (i = 0; i < count; i++) {
dest[pos2_0] = (src[pos1_0] >> 1) + (src[pos1_1] >> 1) +
((src[pos1_0] & 1) & (src[pos1_1] & 1));
src += this->sinkcaps.channels;
dest += this->srccaps.channels;
}
set[pos2_0] = TRUE;
}
/* Try to go from the given 1 channel to the given 2 channels */
pos1_0 = -1;
pos1_1 = -1;
pos2_0 = -1;
for (n = 0; n < this->srccaps.channels; n++) {
if (this->srccaps.pos[n] == conv[c].pos1[0])
pos1_0 = n;
else if (this->srccaps.pos[n] == conv[c].pos1[1])
pos1_1 = n;
}
for (n = 0; n < this->sinkcaps.channels; n++) {
if (this->sinkcaps.pos[n] == conv[c].pos2[0])
pos2_0 = n;
}
if (pos1_0 != -1 && pos1_1 != -1 && pos2_0 != -1) {
src = (gint32 *) GST_BUFFER_DATA (buf);
dest = (gint32 *) GST_BUFFER_DATA (ret);
for (i = 0; i < count; i++) {
dest[pos1_0] = dest[pos1_1] = src[pos2_0];
src += this->sinkcaps.channels;
dest += this->srccaps.channels;
}
set[pos1_0] = set[pos1_1] = TRUE;
}
}
/* reset data pointers */
src = (gint32 *) GST_BUFFER_DATA (buf); src = (gint32 *) GST_BUFFER_DATA (buf);
dest = (gint32 *) GST_BUFFER_DATA (ret); dest = (gint32 *) GST_BUFFER_DATA (ret);
if (this->sinkcaps.channels > this->srccaps.channels) { /* Apart from the compatible channel assignments, we can also have
for (i = 0; i < count; i++) { * same channel assignments. This is much simpler, we simply copy
*dest = *src >> 1; * the value from source to dest! */
src++; for (co = 0; co < this->srccaps.channels; co++) {
*dest += (*src >> 1) + (*src & 1); /* find a channel in input with same position */
src++; for (ci = 0; ci < this->sinkcaps.channels; ci++) {
dest++; if (this->sinkcaps.pos[ci] == this->srccaps.pos[co]) {
for (i = 0; i < count; i++) {
dest[i * this->srccaps.channels + co] =
src[i * this->sinkcaps.channels + ci];
}
set[co] = TRUE;
break;
}
} }
} else {
for (i = count - 1; i >= 0; i--) { /* if not found, then silence */
dest[2 * i] = dest[2 * i + 1] = src[i]; if (ci == this->sinkcaps.channels && !set[co]) {
for (i = 0; i < count; i++) {
dest[i * this->srccaps.channels + co] = 0;
}
} }
} }

View file

@ -28,6 +28,9 @@
static gboolean static gboolean
plugin_init (GstPlugin * plugin) plugin_init (GstPlugin * plugin)
{ {
if (!gst_library_load ("gstaudio"))
return FALSE;
if (!gst_element_register (plugin, "audioconvert", if (!gst_element_register (plugin, "audioconvert",
GST_RANK_PRIMARY, gst_audio_convert_get_type ()) || GST_RANK_PRIMARY, gst_audio_convert_get_type ()) ||
!gst_element_register (plugin, "buffer-frames-convert", !gst_element_register (plugin, "buffer-frames-convert",