2003-04-14 01:20:30 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
|
|
|
|
*
|
|
|
|
* gstaudioconvert.c: Convert audio to different audio formats automatically
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2004-01-27 09:00:01 +00:00
|
|
|
/* Element-Checklist-Version: 5 */
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-03-06 15:31:25 +00:00
|
|
|
/*
|
|
|
|
* design decisions:
|
|
|
|
* - audioconvert converts buffers in a set of supported caps. If it supports
|
|
|
|
* a caps, it supports conversion from these caps to any other caps it
|
|
|
|
* supports. (example: if it does A=>B and A=>C, it also does B=>C)
|
|
|
|
* - audioconvert does not save state between buffers. Every incoming buffer is
|
|
|
|
* converted and the converted buffer is pushed out.
|
|
|
|
* conclusion:
|
|
|
|
* audioconvert is not supposed to be a one-element-does-anything solution for
|
|
|
|
* audio conversions.
|
|
|
|
*/
|
|
|
|
|
2003-06-29 19:46:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2004-02-25 18:02:19 +00:00
|
|
|
#include "config.h"
|
2003-06-29 19:46:12 +00:00
|
|
|
#endif
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <string.h>
|
2004-04-09 12:39:30 +00:00
|
|
|
#include "plugin.h"
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (audio_convert_debug);
|
|
|
|
#define GST_CAT_DEFAULT (audio_convert_debug)
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/*** DEFINITIONS **************************************************************/
|
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
#define GST_TYPE_AUDIO_CONVERT (gst_audio_convert_get_type())
|
|
|
|
#define GST_AUDIO_CONVERT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_CONVERT,GstAudioConvert))
|
|
|
|
#define GST_AUDIO_CONVERT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_CONVERT,GstAudioConvert))
|
|
|
|
#define GST_IS_AUDIO_CONVERT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_CONVERT))
|
|
|
|
#define GST_IS_AUDIO_CONVERT_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_CONVERT))
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
typedef struct _GstAudioConvert GstAudioConvert;
|
2003-07-19 22:58:41 +00:00
|
|
|
typedef struct _GstAudioConvertCaps GstAudioConvertCaps;
|
2003-04-14 01:20:30 +00:00
|
|
|
typedef struct _GstAudioConvertClass GstAudioConvertClass;
|
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
/* this struct is a handy way of passing around all the caps info ... */
|
2004-03-14 22:34:34 +00:00
|
|
|
struct _GstAudioConvertCaps
|
|
|
|
{
|
2003-07-19 22:58:41 +00:00
|
|
|
/* general caps */
|
2004-03-14 22:34:34 +00:00
|
|
|
gboolean is_int;
|
|
|
|
gint endianness;
|
|
|
|
gint width;
|
|
|
|
gint rate;
|
|
|
|
gint channels;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
|
|
|
/* int audio caps */
|
2004-03-14 22:34:34 +00:00
|
|
|
gboolean sign;
|
|
|
|
gint depth;
|
2004-02-20 14:17:57 +00:00
|
|
|
|
|
|
|
/* float audio caps */
|
2004-03-14 22:34:34 +00:00
|
|
|
gint buffer_frames;
|
2003-07-19 22:58:41 +00:00
|
|
|
};
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
struct _GstAudioConvert
|
|
|
|
{
|
|
|
|
GstElement element;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* pads */
|
2004-03-14 22:34:34 +00:00
|
|
|
GstPad *sink;
|
|
|
|
GstPad *src;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
GstAudioConvertCaps srccaps;
|
|
|
|
GstAudioConvertCaps sinkcaps;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
|
|
|
/* conversion functions */
|
2004-03-14 22:34:34 +00:00
|
|
|
GstBuffer *(*convert_internal) (GstAudioConvert * this, GstBuffer * buf);
|
2003-04-14 01:20:30 +00:00
|
|
|
};
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
struct _GstAudioConvertClass
|
|
|
|
{
|
2003-04-14 01:20:30 +00:00
|
|
|
GstElementClass parent_class;
|
|
|
|
};
|
|
|
|
|
2003-11-01 11:41:42 +00:00
|
|
|
static GstElementDetails audio_convert_details = {
|
|
|
|
"Audio Conversion",
|
2003-11-16 22:02:21 +00:00
|
|
|
"Filter/Converter/Audio",
|
2003-11-01 11:41:42 +00:00
|
|
|
"Convert audio to different formats",
|
2004-02-20 14:17:57 +00:00
|
|
|
"Benjamin Otte <in7y118@public.uni-hamburg.de>",
|
2003-11-01 11:41:42 +00:00
|
|
|
};
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* type functions */
|
2004-03-14 22:34:34 +00:00
|
|
|
static void gst_audio_convert_base_init (gpointer g_class);
|
|
|
|
static void gst_audio_convert_class_init (GstAudioConvertClass * klass);
|
|
|
|
static void gst_audio_convert_init (GstAudioConvert * audio_convert);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
|
|
|
/* gstreamer functions */
|
2004-03-14 22:34:34 +00:00
|
|
|
static void gst_audio_convert_chain (GstPad * pad, GstData * _data);
|
|
|
|
static GstPadLinkReturn gst_audio_convert_link (GstPad * pad,
|
|
|
|
const GstCaps * caps);
|
2004-03-21 02:54:37 +00:00
|
|
|
static GstCaps *gst_audio_convert_fixate (GstPad * pad, const GstCaps * caps);
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstCaps *gst_audio_convert_getcaps (GstPad * pad);
|
|
|
|
static GstElementStateReturn gst_audio_convert_change_state (GstElement *
|
|
|
|
element);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstBuffer *gst_audio_convert_buffer_to_default_format (GstAudioConvert *
|
|
|
|
this, GstBuffer * buf);
|
|
|
|
static GstBuffer *gst_audio_convert_buffer_from_default_format (GstAudioConvert
|
|
|
|
* this, GstBuffer * buf);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstBuffer *gst_audio_convert_channels (GstAudioConvert * this,
|
|
|
|
GstBuffer * buf);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
/* AudioConvert signals and args */
|
2004-03-14 22:34:34 +00:00
|
|
|
enum
|
|
|
|
{
|
2003-04-14 01:20:30 +00:00
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
enum
|
|
|
|
{
|
2003-04-14 01:20:30 +00:00
|
|
|
ARG_0,
|
2004-05-21 22:39:30 +00:00
|
|
|
ARG_AGGRESSIVE
|
2003-04-14 01:20:30 +00:00
|
|
|
};
|
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
#define DEBUG_INIT(bla) \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (audio_convert_debug, "audioconvert", 0, "audio conversion element");
|
2004-03-14 22:34:34 +00:00
|
|
|
|
|
|
|
GST_BOILERPLATE_FULL (GstAudioConvert, gst_audio_convert, GstElement,
|
|
|
|
GST_TYPE_ELEMENT, DEBUG_INIT);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/*** GSTREAMER PROTOTYPES *****************************************************/
|
|
|
|
|
2004-03-06 13:26:12 +00:00
|
|
|
#define STATIC_CAPS \
|
|
|
|
GST_STATIC_CAPS ( \
|
|
|
|
"audio/x-raw-int, " \
|
|
|
|
"rate = (int) [ 1, MAX ], " \
|
|
|
|
"channels = (int) [ 1, 2 ], " \
|
|
|
|
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
|
2004-05-25 20:14:10 +00:00
|
|
|
"width = (int) 8, " \
|
|
|
|
"depth = (int) [ 1, 8 ], " \
|
|
|
|
"signed = (boolean) { true, false }; " \
|
|
|
|
"audio/x-raw-int, " \
|
|
|
|
"rate = (int) [ 1, MAX ], " \
|
|
|
|
"channels = (int) [ 1, 2 ], " \
|
|
|
|
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
|
|
|
|
"width = (int) 16, " \
|
|
|
|
"depth = (int) [ 1, 16 ], " \
|
|
|
|
"signed = (boolean) { true, false }; " \
|
|
|
|
"audio/x-raw-int, " \
|
|
|
|
"rate = (int) [ 1, MAX ], " \
|
|
|
|
"channels = (int) [ 1, 2 ], " \
|
|
|
|
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
|
|
|
|
"width = (int) 32, " \
|
2004-03-06 13:26:12 +00:00
|
|
|
"depth = (int) [ 1, 32 ], " \
|
|
|
|
"signed = (boolean) { true, false }; " \
|
|
|
|
"audio/x-raw-float, " \
|
|
|
|
"rate = (int) [ 1, MAX ], " \
|
|
|
|
"channels = (int) [ 1, 2 ], " \
|
|
|
|
"endianness = (int) BYTE_ORDER, " \
|
|
|
|
"width = (int) 32, " \
|
|
|
|
"buffer-frames = (int) [ 0, MAX ]" \
|
|
|
|
)
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_audio_convert_src_template =
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
STATIC_CAPS);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate gst_audio_convert_sink_template =
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
STATIC_CAPS);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
/*** TYPE FUNCTIONS ***********************************************************/
|
|
|
|
|
2003-11-01 11:41:42 +00:00
|
|
|
static void
|
|
|
|
gst_audio_convert_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&gst_audio_convert_src_template));
|
2003-11-01 11:41:42 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
2003-12-22 01:47:09 +00:00
|
|
|
gst_static_pad_template_get (&gst_audio_convert_sink_template));
|
2003-11-01 11:41:42 +00:00
|
|
|
gst_element_class_set_details (element_class, &audio_convert_details);
|
|
|
|
}
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_convert_class_init (GstAudioConvertClass * klass)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
2004-01-26 03:54:21 +00:00
|
|
|
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
gstelement_class->change_state = gst_audio_convert_change_state;
|
|
|
|
}
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_convert_init (GstAudioConvert * this)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
|
|
|
/* sinkpad */
|
2004-03-14 22:34:34 +00:00
|
|
|
this->sink =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&gst_audio_convert_sink_template), "sink");
|
2004-01-15 21:05:17 +00:00
|
|
|
gst_pad_set_getcaps_function (this->sink, gst_audio_convert_getcaps);
|
2003-04-14 01:20:30 +00:00
|
|
|
gst_pad_set_link_function (this->sink, gst_audio_convert_link);
|
2004-03-21 02:54:37 +00:00
|
|
|
gst_pad_set_fixate_function (this->sink, gst_audio_convert_fixate);
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (this), this->sink);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* srcpad */
|
2004-03-14 22:34:34 +00:00
|
|
|
this->src =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&gst_audio_convert_src_template), "src");
|
2004-01-15 21:05:17 +00:00
|
|
|
gst_pad_set_getcaps_function (this->src, gst_audio_convert_getcaps);
|
2003-04-14 01:20:30 +00:00
|
|
|
gst_pad_set_link_function (this->src, gst_audio_convert_link);
|
2004-03-21 02:54:37 +00:00
|
|
|
gst_pad_set_fixate_function (this->src, gst_audio_convert_fixate);
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (this), this->src);
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_pad_set_chain_function (this->sink, gst_audio_convert_chain);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/* clear important variables */
|
2003-07-19 22:58:41 +00:00
|
|
|
this->convert_internal = NULL;
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*** GSTREAMER FUNCTIONS ******************************************************/
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_convert_chain (GstPad * pad, GstData * data)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
2004-01-26 03:54:21 +00:00
|
|
|
GstBuffer *buf = GST_BUFFER (data);
|
2003-04-14 01:20:30 +00:00
|
|
|
GstAudioConvert *this;
|
|
|
|
|
2004-01-12 19:46:45 +00:00
|
|
|
g_return_if_fail (GST_IS_PAD (pad));
|
|
|
|
g_return_if_fail (buf != NULL);
|
|
|
|
g_return_if_fail (GST_IS_AUDIO_CONVERT (GST_OBJECT_PARENT (pad)));
|
|
|
|
this = GST_AUDIO_CONVERT (GST_OBJECT_PARENT (pad));
|
2003-04-14 01:20:30 +00:00
|
|
|
|
|
|
|
/* FIXME */
|
|
|
|
if (GST_IS_EVENT (buf)) {
|
|
|
|
gst_pad_event_default (pad, GST_EVENT (buf));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (!gst_pad_is_negotiated (this->sink)) {
|
2004-03-06 00:42:20 +00:00
|
|
|
GST_ELEMENT_ERROR (this, CORE, NEGOTIATION, (NULL),
|
2004-04-14 16:09:10 +00:00
|
|
|
("Sink pad (connected to %s:%s) not negotiated before chain function",
|
|
|
|
GST_DEBUG_PAD_NAME (gst_pad_get_peer (this->sink))));
|
2004-02-16 11:45:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-01-26 03:54:21 +00:00
|
|
|
if (!gst_pad_is_negotiated (this->src)) {
|
|
|
|
gst_data_unref (data);
|
|
|
|
return;
|
|
|
|
}
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
/**
|
|
|
|
* Theory of operation:
|
2003-07-19 22:58:41 +00:00
|
|
|
* - convert the format (endianness, signedness, width, depth) to
|
2003-04-14 01:20:30 +00:00
|
|
|
* (G_BYTE_ORDER, TRUE, 32, 32)
|
|
|
|
* - convert rate and channels
|
|
|
|
* - convert back to output format
|
|
|
|
*/
|
|
|
|
|
|
|
|
buf = gst_audio_convert_buffer_to_default_format (this, buf);
|
|
|
|
|
|
|
|
buf = gst_audio_convert_channels (this, buf);
|
|
|
|
|
|
|
|
buf = gst_audio_convert_buffer_from_default_format (this, buf);
|
|
|
|
|
2003-10-08 16:08:22 +00:00
|
|
|
gst_pad_push (this->src, GST_DATA (buf));
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-02-20 14:17:57 +00:00
|
|
|
/* this function is complicated now, but it will be unnecessary when we convert
|
|
|
|
* rate. */
|
2004-01-15 21:05:17 +00:00
|
|
|
static GstCaps *
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_convert_getcaps (GstPad * pad)
|
2004-01-15 21:05:17 +00:00
|
|
|
{
|
|
|
|
GstAudioConvert *this;
|
|
|
|
GstPad *otherpad;
|
2004-02-20 14:17:57 +00:00
|
|
|
GstStructure *structure;
|
|
|
|
GstCaps *othercaps, *caps;
|
|
|
|
const GstCaps *templcaps;
|
2004-02-25 13:25:44 +00:00
|
|
|
int i, size;
|
2004-01-15 21:05:17 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_return_val_if_fail (GST_IS_PAD (pad), NULL);
|
|
|
|
g_return_val_if_fail (GST_IS_AUDIO_CONVERT (GST_OBJECT_PARENT (pad)), NULL);
|
|
|
|
this = GST_AUDIO_CONVERT (GST_OBJECT_PARENT (pad));
|
2004-01-15 21:05:17 +00:00
|
|
|
|
|
|
|
otherpad = (pad == this->src) ? this->sink : this->src;
|
|
|
|
|
2004-02-20 14:17:57 +00:00
|
|
|
/* all we want to find out is the rate */
|
|
|
|
templcaps = gst_pad_get_pad_template_caps (pad);
|
2004-01-15 21:05:17 +00:00
|
|
|
othercaps = gst_pad_get_allowed_caps (otherpad);
|
|
|
|
|
2004-02-25 13:25:44 +00:00
|
|
|
size = gst_caps_get_size (othercaps);
|
|
|
|
|
2004-03-06 13:26:12 +00:00
|
|
|
for (i = size - 1; i >= 0; i--) {
|
2004-01-15 21:05:17 +00:00
|
|
|
structure = gst_caps_get_structure (othercaps, i);
|
|
|
|
gst_structure_remove_field (structure, "channels");
|
|
|
|
gst_structure_remove_field (structure, "endianness");
|
|
|
|
gst_structure_remove_field (structure, "width");
|
2004-03-06 13:26:12 +00:00
|
|
|
gst_structure_remove_field (structure, "depth");
|
|
|
|
gst_structure_remove_field (structure, "signed");
|
|
|
|
structure = gst_structure_copy (structure);
|
2004-02-20 14:17:57 +00:00
|
|
|
if (strcmp (gst_structure_get_name (structure), "audio/x-raw-int") == 0) {
|
2004-03-06 13:26:12 +00:00
|
|
|
gst_structure_set_name (structure, "audio/x-raw-float");
|
2004-04-05 13:18:56 +00:00
|
|
|
if (pad == this->sink) {
|
|
|
|
gst_structure_set (structure, "buffer-frames", GST_TYPE_INT_RANGE, 0,
|
|
|
|
G_MAXINT, NULL);
|
|
|
|
} else {
|
|
|
|
gst_structure_set (structure, "buffer-frames", G_TYPE_INT, 0, NULL);
|
|
|
|
}
|
2004-02-20 14:17:57 +00:00
|
|
|
} else {
|
2004-03-06 13:26:12 +00:00
|
|
|
gst_structure_set_name (structure, "audio/x-raw-int");
|
2004-02-20 14:17:57 +00:00
|
|
|
gst_structure_remove_field (structure, "buffer-frames");
|
|
|
|
}
|
2004-03-06 13:26:12 +00:00
|
|
|
gst_caps_append_structure (othercaps, structure);
|
2004-01-15 21:05:17 +00:00
|
|
|
}
|
2004-02-20 14:17:57 +00:00
|
|
|
caps = gst_caps_intersect (othercaps, templcaps);
|
2004-01-26 03:54:21 +00:00
|
|
|
gst_caps_free (othercaps);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-01-15 21:05:17 +00:00
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
static gboolean
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_convert_parse_caps (const GstCaps * gst_caps,
|
|
|
|
GstAudioConvertCaps * caps)
|
2004-01-26 03:54:21 +00:00
|
|
|
{
|
|
|
|
GstStructure *structure = gst_caps_get_structure (gst_caps, 0);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
g_return_val_if_fail (gst_caps_is_fixed (gst_caps), FALSE);
|
|
|
|
g_return_val_if_fail (caps != NULL, FALSE);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
caps->endianness = G_BYTE_ORDER;
|
2004-03-14 22:34:34 +00:00
|
|
|
caps->is_int =
|
|
|
|
(strcmp (gst_structure_get_name (structure), "audio/x-raw-int") == 0);
|
|
|
|
if (!gst_structure_get_int (structure, "channels", &caps->channels)
|
|
|
|
|| !gst_structure_get_int (structure, "width", &caps->width)
|
|
|
|
|| !gst_structure_get_int (structure, "rate", &caps->rate)
|
|
|
|
|| (caps->is_int
|
2004-03-15 19:32:28 +00:00
|
|
|
&& (!gst_structure_get_boolean (structure, "signed", &caps->sign)
|
|
|
|
|| !gst_structure_get_int (structure, "depth", &caps->depth)
|
|
|
|
|| (caps->width != 8
|
|
|
|
&& !gst_structure_get_int (structure, "endianness",
|
|
|
|
&caps->endianness)))) || (!caps->is_int
|
|
|
|
&& !gst_structure_get_int (structure, "buffer-frames",
|
|
|
|
&caps->buffer_frames))) {
|
2004-01-26 03:54:21 +00:00
|
|
|
GST_DEBUG ("could not get some values from structure");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-05-25 20:14:10 +00:00
|
|
|
if (caps->is_int && caps->depth > caps->width) {
|
|
|
|
GST_DEBUG ("width > depth, not allowed - make us advertise correct caps");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-01-26 03:54:21 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static GstPadLinkReturn
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_convert_link (GstPad * pad, const GstCaps * caps)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
|
|
|
GstAudioConvert *this;
|
2004-01-26 03:54:21 +00:00
|
|
|
GstPad *otherpad;
|
|
|
|
GstAudioConvertCaps ac_caps, other_ac_caps;
|
|
|
|
GstCaps *othercaps;
|
|
|
|
guint i;
|
|
|
|
GstPadLinkReturn ret;
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_LINK_REFUSED);
|
|
|
|
g_return_val_if_fail (GST_IS_AUDIO_CONVERT (GST_OBJECT_PARENT (pad)),
|
|
|
|
GST_PAD_LINK_REFUSED);
|
|
|
|
|
|
|
|
this = GST_AUDIO_CONVERT (GST_OBJECT_PARENT (pad));
|
2004-01-26 03:54:21 +00:00
|
|
|
otherpad = (pad == this->src ? this->sink : this->src);
|
|
|
|
|
|
|
|
/* negotiate sinkpad first */
|
2004-03-14 22:34:34 +00:00
|
|
|
if (pad == this->src && !gst_pad_is_negotiated (this->sink))
|
2004-01-26 03:54:21 +00:00
|
|
|
return GST_PAD_LINK_DELAYED;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
if (!gst_audio_convert_parse_caps (caps, &ac_caps))
|
2003-12-31 08:02:04 +00:00
|
|
|
return GST_PAD_LINK_REFUSED;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
/* ok, not those - try setting "any" caps */
|
|
|
|
othercaps = gst_pad_get_allowed_caps (otherpad);
|
|
|
|
for (i = 0; i < gst_caps_get_size (othercaps); i++) {
|
|
|
|
GstStructure *structure = gst_caps_get_structure (othercaps, i);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
gst_structure_set (structure, "rate", G_TYPE_INT, ac_caps.rate, NULL);
|
2004-03-06 13:26:12 +00:00
|
|
|
if (strcmp (gst_structure_get_name (structure), "audio/x-raw-float") == 0) {
|
|
|
|
if (!ac_caps.is_int) {
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_structure_set (structure, "buffer-frames", G_TYPE_INT,
|
|
|
|
ac_caps.buffer_frames, NULL);
|
2004-03-06 13:26:12 +00:00
|
|
|
} else {
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_structure_set (structure, "buffer-frames", G_TYPE_INT, 0, NULL);
|
2004-03-06 13:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-26 03:54:21 +00:00
|
|
|
}
|
2004-03-21 02:54:37 +00:00
|
|
|
if (this->sink == pad) {
|
|
|
|
this->sinkcaps = ac_caps;
|
|
|
|
} else {
|
|
|
|
this->srccaps = ac_caps;
|
|
|
|
}
|
|
|
|
GST_LOG_OBJECT (this, "trying to set caps to %" GST_PTR_FORMAT, othercaps);
|
2004-01-27 09:05:22 +00:00
|
|
|
ret = gst_pad_try_set_caps_nonfixed (otherpad, othercaps);
|
2004-01-26 03:54:21 +00:00
|
|
|
gst_caps_free (othercaps);
|
|
|
|
if (ret < GST_PAD_LINK_OK)
|
|
|
|
return ret;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
/* woohoo, got it */
|
2004-05-24 19:19:29 +00:00
|
|
|
othercaps = (GstCaps *) gst_pad_get_negotiated_caps (otherpad);
|
|
|
|
if (othercaps) {
|
|
|
|
if (!gst_audio_convert_parse_caps (othercaps, &other_ac_caps)) {
|
|
|
|
g_critical ("internal negotiation error");
|
|
|
|
return GST_PAD_LINK_REFUSED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
other_ac_caps = ac_caps;
|
2004-02-20 14:17:57 +00:00
|
|
|
}
|
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
if (this->sink == pad) {
|
|
|
|
this->srccaps = other_ac_caps;
|
|
|
|
this->sinkcaps = ac_caps;
|
|
|
|
} else {
|
|
|
|
this->srccaps = ac_caps;
|
|
|
|
this->sinkcaps = other_ac_caps;
|
2003-12-31 08:02:04 +00:00
|
|
|
}
|
2004-02-25 18:02:19 +00:00
|
|
|
|
2004-03-21 02:54:37 +00:00
|
|
|
GST_DEBUG_OBJECT (this, "negotiated pad to %" GST_PTR_FORMAT, caps);
|
2003-04-14 01:20:30 +00:00
|
|
|
return GST_PAD_LINK_OK;
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-05-03 13:25:22 +00:00
|
|
|
/* tries to fixate the given field of the given caps to the given int value */
|
2004-03-21 02:54:37 +00:00
|
|
|
gboolean
|
|
|
|
_fixate_caps_to_int (GstCaps ** caps, const gchar * field, gint value)
|
|
|
|
{
|
2004-05-03 13:25:22 +00:00
|
|
|
GstCaps *try, *isect_lower, *isect_higher;
|
2004-03-21 02:54:37 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
guint i;
|
|
|
|
|
2004-05-03 13:25:22 +00:00
|
|
|
/* First try to see if we can fixate by intersecting given caps with
|
|
|
|
* simple audio caps with ranges starting/ending with value */
|
2004-04-09 18:55:10 +00:00
|
|
|
try = gst_caps_new_simple ("audio/x-raw-int", field, GST_TYPE_INT_RANGE,
|
|
|
|
G_MININT, value - 1, NULL);
|
|
|
|
gst_caps_append (try, gst_caps_new_simple ("audio/x-raw-float", field,
|
|
|
|
GST_TYPE_INT_RANGE, G_MININT, value - 1, NULL));
|
2004-05-03 13:25:22 +00:00
|
|
|
isect_lower = gst_caps_intersect (*caps, try);
|
|
|
|
gst_caps_free (try);
|
|
|
|
|
|
|
|
if (!gst_caps_is_empty (isect_lower)) {
|
2004-04-09 18:55:10 +00:00
|
|
|
try = gst_caps_new_simple ("audio/x-raw-int", field, GST_TYPE_INT_RANGE,
|
|
|
|
value, G_MAXINT, NULL);
|
|
|
|
gst_caps_append (try, gst_caps_new_simple ("audio/x-raw-float", field,
|
|
|
|
GST_TYPE_INT_RANGE, value, G_MAXINT, NULL));
|
2004-05-03 13:25:22 +00:00
|
|
|
isect_higher = gst_caps_intersect (*caps, try);
|
|
|
|
/* FIXME: why choose to end up with the higher range, and not the fixed
|
|
|
|
* value ? */
|
|
|
|
if (!gst_caps_is_empty (isect_higher)) {
|
2004-03-21 02:54:37 +00:00
|
|
|
gst_caps_free (*caps);
|
2004-05-03 13:25:22 +00:00
|
|
|
*caps = isect_higher;
|
2004-03-21 02:54:37 +00:00
|
|
|
ret = TRUE;
|
|
|
|
} else {
|
2004-05-03 13:25:22 +00:00
|
|
|
gst_caps_free (isect_higher);
|
2004-03-21 02:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
2004-05-03 13:25:22 +00:00
|
|
|
gst_caps_free (isect_lower);
|
|
|
|
|
|
|
|
/* FIXME: why don't we already return here when ret == TRUE ? */
|
2004-03-21 02:54:37 +00:00
|
|
|
for (i = 0; i < gst_caps_get_size (*caps); i++) {
|
|
|
|
GstStructure *structure = gst_caps_get_structure (*caps, i);
|
|
|
|
|
|
|
|
if (gst_structure_has_field (structure, field))
|
|
|
|
ret |=
|
|
|
|
gst_caps_structure_fixate_field_nearest_int (structure, field, value);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
gst_audio_convert_fixate (GstPad * pad, const GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstAudioConvert *this =
|
|
|
|
GST_AUDIO_CONVERT (gst_object_get_parent (GST_OBJECT (pad)));
|
|
|
|
GstPad *otherpad = (pad == this->sink ? this->src : this->sink);
|
2004-05-26 14:47:23 +00:00
|
|
|
GstAudioConvertCaps try, ac_caps =
|
2004-03-21 02:54:37 +00:00
|
|
|
(pad == this->sink ? this->srccaps : this->sinkcaps);
|
2004-05-26 14:47:23 +00:00
|
|
|
GstCaps *copy = gst_caps_copy (caps);
|
2004-03-21 02:54:37 +00:00
|
|
|
|
2004-05-26 14:47:23 +00:00
|
|
|
if (!GST_PAD_IS_NEGOTIATING (otherpad)) {
|
|
|
|
try.channels = 2;
|
|
|
|
try.width = 16;
|
|
|
|
try.depth = 16;
|
|
|
|
try.endianness = G_BYTE_ORDER;
|
|
|
|
} else {
|
|
|
|
try.channels = ac_caps.channels;
|
|
|
|
try.width = ac_caps.is_int ? ac_caps.width : 16;
|
|
|
|
try.depth = ac_caps.is_int ? ac_caps.depth : 16;
|
|
|
|
try.endianness = ac_caps.is_int ? ac_caps.endianness : G_BYTE_ORDER;
|
|
|
|
}
|
2004-03-21 02:54:37 +00:00
|
|
|
|
2004-05-26 14:47:23 +00:00
|
|
|
if (_fixate_caps_to_int (©, "channels", try.channels))
|
2004-03-21 02:54:37 +00:00
|
|
|
return copy;
|
2004-05-26 14:47:23 +00:00
|
|
|
if (_fixate_caps_to_int (©, "width", try.width))
|
2004-03-21 02:54:37 +00:00
|
|
|
return copy;
|
2004-05-26 14:47:23 +00:00
|
|
|
if (_fixate_caps_to_int (©, "depth", try.depth))
|
2004-03-21 02:54:37 +00:00
|
|
|
return copy;
|
2004-05-26 14:47:23 +00:00
|
|
|
if (_fixate_caps_to_int (©, "endianness", try.endianness))
|
2004-05-25 20:14:10 +00:00
|
|
|
return copy;
|
|
|
|
|
2004-03-21 02:54:37 +00:00
|
|
|
|
|
|
|
gst_caps_free (copy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static GstElementStateReturn
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_convert_change_state (GstElement * element)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
|
|
|
GstAudioConvert *this = GST_AUDIO_CONVERT (element);
|
|
|
|
|
|
|
|
switch (GST_STATE_TRANSITION (element)) {
|
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
2003-07-19 22:58:41 +00:00
|
|
|
this->convert_internal = NULL;
|
2003-04-14 01:20:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent_class->change_state) {
|
|
|
|
return parent_class->change_state (element);
|
|
|
|
} else {
|
|
|
|
return GST_STATE_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-19 22:58:41 +00:00
|
|
|
/* return a writable buffer of size which ideally is the same as before
|
2003-04-14 01:20:30 +00:00
|
|
|
- You must unref the new buffer
|
|
|
|
- The size of the old buffer is undefined after this operation */
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstBuffer *
|
|
|
|
gst_audio_convert_get_buffer (GstBuffer * buf, guint size)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
|
|
|
GstBuffer *ret;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
|
|
|
GST_LOG
|
|
|
|
("new buffer of size %u requested. Current is: data: %p - size: %u - maxsize: %u",
|
2004-01-26 03:54:21 +00:00
|
|
|
size, buf->data, buf->size, buf->maxsize);
|
2003-04-16 18:36:29 +00:00
|
|
|
if (buf->maxsize >= size && gst_buffer_is_writable (buf)) {
|
2003-04-14 01:20:30 +00:00
|
|
|
gst_buffer_ref (buf);
|
|
|
|
buf->size = size;
|
2004-03-14 22:34:34 +00:00
|
|
|
GST_LOG
|
2004-03-15 19:32:28 +00:00
|
|
|
("returning same buffer with adjusted values. data: %p - size: %u - maxsize: %u",
|
|
|
|
buf->data, buf->size, buf->maxsize);
|
2003-04-14 01:20:30 +00:00
|
|
|
return buf;
|
|
|
|
} else {
|
2004-02-20 14:17:57 +00:00
|
|
|
ret = gst_buffer_new_and_alloc (size);
|
|
|
|
g_assert (ret);
|
2004-01-26 03:54:21 +00:00
|
|
|
gst_buffer_stamp (ret, buf);
|
2004-02-25 17:45:54 +00:00
|
|
|
GST_LOG ("returning new buffer. data: %p - size: %u - maxsize: %u",
|
2004-03-15 19:32:28 +00:00
|
|
|
ret->data, ret->size, ret->maxsize);
|
2003-04-14 01:20:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static inline guint8
|
|
|
|
GUINT8_IDENTITY (guint8 x)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
static inline guint8
|
|
|
|
GINT8_IDENTITY (gint8 x)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-03-05 21:05:26 +00:00
|
|
|
#define CONVERT_TO(to, from, type, sign, endianness, LE_FUNC, BE_FUNC) \
|
|
|
|
G_STMT_START{ \
|
|
|
|
type value; \
|
|
|
|
memcpy (&value, from, sizeof (type)); \
|
|
|
|
from -= sizeof (type); \
|
|
|
|
value = (endianness == G_LITTLE_ENDIAN) ? LE_FUNC (value) : BE_FUNC (value); \
|
|
|
|
if (sign) { \
|
|
|
|
to = value; \
|
|
|
|
} else { \
|
|
|
|
to = (gint64) value - (1 << (sizeof (type) * 8 - 1)); \
|
|
|
|
} \
|
2003-04-14 01:20:30 +00:00
|
|
|
}G_STMT_END;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
static GstBuffer *
|
|
|
|
gst_audio_convert_buffer_to_default_format (GstAudioConvert * this,
|
|
|
|
GstBuffer * buf)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
|
|
|
GstBuffer *ret;
|
|
|
|
gint i, count;
|
2003-04-15 03:19:08 +00:00
|
|
|
gint64 cur = 0;
|
2003-04-14 01:20:30 +00:00
|
|
|
gint32 write;
|
2003-04-15 19:10:14 +00:00
|
|
|
gint32 *dest;
|
|
|
|
guint8 *src;
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-02-20 14:17:57 +00:00
|
|
|
if (this->sinkcaps.is_int) {
|
|
|
|
if (this->sinkcaps.width == 32 && this->sinkcaps.depth == 32 &&
|
2004-03-15 19:32:28 +00:00
|
|
|
this->sinkcaps.endianness == G_BYTE_ORDER
|
|
|
|
&& this->sinkcaps.sign == TRUE)
|
2004-02-20 14:17:57 +00:00
|
|
|
return buf;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
ret =
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_audio_convert_get_buffer (buf,
|
|
|
|
buf->size * 32 / this->sinkcaps.width);
|
2004-02-20 14:17:57 +00:00
|
|
|
|
|
|
|
count = ret->size / 4;
|
|
|
|
src = buf->data + (count - 1) * (this->sinkcaps.width / 8);
|
|
|
|
dest = (gint32 *) ret->data;
|
|
|
|
for (i = count - 1; i >= 0; i--) {
|
|
|
|
switch (this->sinkcaps.width) {
|
2004-03-15 19:32:28 +00:00
|
|
|
case 8:
|
|
|
|
if (this->sinkcaps.sign) {
|
|
|
|
CONVERT_TO (cur, src, gint8, this->sinkcaps.sign,
|
|
|
|
this->sinkcaps.endianness, GINT8_IDENTITY, GINT8_IDENTITY);
|
|
|
|
} else {
|
|
|
|
CONVERT_TO (cur, src, guint8, this->sinkcaps.sign,
|
|
|
|
this->sinkcaps.endianness, GUINT8_IDENTITY, GUINT8_IDENTITY);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
if (this->sinkcaps.sign) {
|
|
|
|
CONVERT_TO (cur, src, gint16, this->sinkcaps.sign,
|
|
|
|
this->sinkcaps.endianness, GINT16_FROM_LE, GINT16_FROM_BE);
|
|
|
|
} else {
|
|
|
|
CONVERT_TO (cur, src, guint16, this->sinkcaps.sign,
|
|
|
|
this->sinkcaps.endianness, GUINT16_FROM_LE, GUINT16_FROM_BE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
if (this->sinkcaps.sign) {
|
|
|
|
CONVERT_TO (cur, src, gint32, this->sinkcaps.sign,
|
|
|
|
this->sinkcaps.endianness, GINT32_FROM_LE, GINT32_FROM_BE);
|
|
|
|
} else {
|
|
|
|
CONVERT_TO (cur, src, guint32, this->sinkcaps.sign,
|
|
|
|
this->sinkcaps.endianness, GUINT32_FROM_LE, GUINT32_FROM_BE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
2004-02-20 14:17:57 +00:00
|
|
|
}
|
|
|
|
cur = cur * ((gint64) 1 << (32 - this->sinkcaps.depth));
|
2004-03-14 22:34:34 +00:00
|
|
|
cur = CLAMP (cur, -((gint64) 1 << 32), (gint64) 0x7FFFFFFF);
|
2004-02-20 14:17:57 +00:00
|
|
|
write = cur;
|
|
|
|
memcpy (&dest[i], &write, 4);
|
2003-07-19 22:58:41 +00:00
|
|
|
}
|
2004-02-20 14:17:57 +00:00
|
|
|
} else {
|
|
|
|
/* float2int */
|
|
|
|
gfloat *in;
|
|
|
|
gint32 *out;
|
|
|
|
|
|
|
|
/* should just give the same buffer, unless it's not writable -- float is
|
|
|
|
* already 32 bits */
|
|
|
|
ret = gst_audio_convert_get_buffer (buf, buf->size);
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
in = (gfloat *) GST_BUFFER_DATA (buf);
|
|
|
|
out = (gint32 *) GST_BUFFER_DATA (ret);
|
|
|
|
for (i = buf->size / sizeof (float); i > 0; i--) {
|
2004-03-04 23:30:29 +00:00
|
|
|
*in *= 2147483647.0f + .5;
|
2004-03-14 22:34:34 +00:00
|
|
|
*out = (gint32) CLAMP ((gint64) * in, -2147483648ll, 2147483647ll);
|
2004-03-04 23:30:29 +00:00
|
|
|
out++;
|
|
|
|
in++;
|
|
|
|
}
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return ret;
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
#define POPULATE(format, be_func, le_func) G_STMT_START{ \
|
|
|
|
format val; \
|
|
|
|
format* p = (format *) dest; \
|
|
|
|
int_value >>= (32 - this->srccaps.depth); \
|
2004-03-05 21:05:26 +00:00
|
|
|
if (this->srccaps.sign) { \
|
|
|
|
val = (format) int_value; \
|
|
|
|
} else { \
|
|
|
|
val = (format) int_value + (1 << (this->srccaps.depth - 1)); \
|
|
|
|
} \
|
2004-01-26 03:54:21 +00:00
|
|
|
switch (this->srccaps.endianness) { \
|
2003-07-19 22:58:41 +00:00
|
|
|
case G_LITTLE_ENDIAN: \
|
|
|
|
val = le_func (val); \
|
|
|
|
break; \
|
|
|
|
case G_BIG_ENDIAN: \
|
|
|
|
val = be_func (val); \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
g_assert_not_reached (); \
|
|
|
|
}; \
|
|
|
|
*p = val; \
|
|
|
|
p ++; \
|
|
|
|
dest = (guint8 *) p; \
|
2003-04-14 01:20:30 +00:00
|
|
|
}G_STMT_END
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static GstBuffer *
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_convert_buffer_from_default_format (GstAudioConvert * this,
|
|
|
|
GstBuffer * buf)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
|
|
|
GstBuffer *ret;
|
|
|
|
guint count, i;
|
|
|
|
gint32 *src;
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
if (this->srccaps.is_int && this->srccaps.width == 32
|
|
|
|
&& this->srccaps.depth == 32 && this->srccaps.endianness == G_BYTE_ORDER
|
|
|
|
&& this->srccaps.sign == TRUE)
|
2003-04-14 01:20:30 +00:00
|
|
|
return buf;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-03-06 13:26:12 +00:00
|
|
|
if (this->srccaps.is_int) {
|
|
|
|
guint8 *dest;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-03-15 19:32:28 +00:00
|
|
|
count = buf->size / 4; /* size is undefined after gst_audio_convert_get_buffer! */
|
2004-03-14 22:34:34 +00:00
|
|
|
ret =
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_audio_convert_get_buffer (buf,
|
|
|
|
buf->size * this->srccaps.width / 32);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-03-06 13:26:12 +00:00
|
|
|
dest = ret->data;
|
|
|
|
src = (gint32 *) buf->data;
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-03-06 13:26:12 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
gint32 int_value = *src;
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-03-06 13:26:12 +00:00
|
|
|
src++;
|
|
|
|
switch (this->srccaps.width) {
|
2004-03-15 19:32:28 +00:00
|
|
|
case 8:
|
|
|
|
if (this->srccaps.sign) {
|
|
|
|
POPULATE (gint8, GINT8_IDENTITY, GINT8_IDENTITY);
|
|
|
|
} else {
|
|
|
|
POPULATE (guint8, GUINT8_IDENTITY, GUINT8_IDENTITY);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
if (this->srccaps.sign) {
|
|
|
|
POPULATE (gint16, GINT16_TO_BE, GINT16_TO_LE);
|
|
|
|
} else {
|
|
|
|
POPULATE (guint16, GUINT16_TO_BE, GUINT16_TO_LE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
if (this->srccaps.sign) {
|
|
|
|
POPULATE (gint32, GINT32_TO_BE, GINT32_TO_LE);
|
|
|
|
} else {
|
|
|
|
POPULATE (guint32, GUINT32_TO_BE, GUINT32_TO_LE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
2004-03-06 13:26:12 +00:00
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
}
|
2004-03-06 13:26:12 +00:00
|
|
|
} else {
|
|
|
|
gfloat *dest;
|
|
|
|
|
|
|
|
/* 1 / (2^31-1) * i */
|
2004-03-14 22:34:34 +00:00
|
|
|
#define INT2FLOAT(i) (4.6566128752457969e-10 * ((gfloat)i))
|
2004-03-15 19:32:28 +00:00
|
|
|
count = buf->size / 4; /* size is undefined after gst_audio_convert_get_buffer! */
|
2004-03-14 22:34:34 +00:00
|
|
|
ret =
|
2004-03-15 19:32:28 +00:00
|
|
|
gst_audio_convert_get_buffer (buf,
|
|
|
|
buf->size * this->srccaps.width / 32);
|
2004-03-14 22:34:34 +00:00
|
|
|
|
2004-03-06 13:26:12 +00:00
|
|
|
dest = (gfloat *) ret->data;
|
|
|
|
src = (gint32 *) buf->data;
|
|
|
|
for (i = 0; i < count; i++) {
|
2004-03-14 22:34:34 +00:00
|
|
|
*dest = (4.6566128752457969e-10 * ((gfloat) * src));
|
2004-03-06 13:26:12 +00:00
|
|
|
dest++;
|
|
|
|
src++;
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_buffer_unref (buf);
|
2003-04-14 01:20:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2003-04-14 01:20:30 +00:00
|
|
|
static GstBuffer *
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_audio_convert_channels (GstAudioConvert * this, GstBuffer * buf)
|
2003-04-14 01:20:30 +00:00
|
|
|
{
|
|
|
|
GstBuffer *ret;
|
2003-12-31 08:02:04 +00:00
|
|
|
gint i, count;
|
2004-03-05 21:05:26 +00:00
|
|
|
gint32 *src, *dest;
|
2003-04-14 01:20:30 +00:00
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
if (this->sinkcaps.channels == this->srccaps.channels)
|
2003-04-14 01:20:30 +00:00
|
|
|
return buf;
|
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
count = GST_BUFFER_SIZE (buf) / 4 / this->sinkcaps.channels;
|
|
|
|
ret = gst_audio_convert_get_buffer (buf, count * 4 * this->srccaps.channels);
|
2004-03-05 21:05:26 +00:00
|
|
|
src = (gint32 *) GST_BUFFER_DATA (buf);
|
|
|
|
dest = (gint32 *) GST_BUFFER_DATA (ret);
|
2003-07-19 22:58:41 +00:00
|
|
|
|
2004-01-26 03:54:21 +00:00
|
|
|
if (this->sinkcaps.channels > this->srccaps.channels) {
|
2003-04-14 01:20:30 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2003-04-15 19:10:14 +00:00
|
|
|
*dest = *src >> 1;
|
2003-04-14 01:20:30 +00:00
|
|
|
src++;
|
2004-03-05 21:05:26 +00:00
|
|
|
*dest += (*src >> 1) + (*src & 1);
|
2003-04-14 01:20:30 +00:00
|
|
|
src++;
|
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
} else {
|
2003-04-15 19:10:14 +00:00
|
|
|
for (i = count - 1; i >= 0; i--) {
|
|
|
|
dest[2 * i] = dest[2 * i + 1] = src[i];
|
2003-04-14 01:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:34 +00:00
|
|
|
gst_buffer_unref (buf);
|
2003-04-14 01:20:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|