Commit bde8ec9b authored by David Schleef's avatar David Schleef
Browse files

gst/audioresample/Makefile.am: Leet audioresampling code

Original commit message from CVS:
* gst/audioresample/Makefile.am: Leet audioresampling code
* gst/audioresample/buffer.c:
* gst/audioresample/buffer.h:
* gst/audioresample/debug.c:
* gst/audioresample/debug.h:
* gst/audioresample/functable.c:
* gst/audioresample/functable.h:
* gst/audioresample/gstaudioresample.c:
* gst/audioresample/gstaudioresample.h:
* gst/audioresample/resample.c:
* gst/audioresample/resample.h:
* gst/audioresample/resample_chunk.c:
* gst/audioresample/resample_functable.c:
* gst/audioresample/resample_ref.c:
parent 3a9fc486
plugin_LTLIBRARIES = libgstaudioresample.la
resample_SOURCES = \
functable.c \
functable.h \
resample.c \
resample_functable.c \
resample_ref.c \
resample_chunk.c \
resample.h \
debug.c \
debug.h \
buffer.c \
buffer.h
libgstaudioresample_la_SOURCES = gstaudioresample.c $(resample_SOURCES)
libgstaudioresample_la_CFLAGS = $(GST_CFLAGS) $(LIBOIL_CFLAGS)
libgstaudioresample_la_LIBADD = $(LIBOIL_LIBS)
libgstaudioresample_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
#ifndef HAVE_CONFIG_H
#include "config.h"
#endif
#include <audioresample/buffer.h>
#include <glib.h>
#include <string.h>
#include <audioresample/debug.h>
static void audioresample_buffer_free_mem (AudioresampleBuffer * buffer,
void *);
static void audioresample_buffer_free_subbuffer (AudioresampleBuffer * buffer,
void *priv);
AudioresampleBuffer *
audioresample_buffer_new (void)
{
AudioresampleBuffer *buffer;
buffer = g_new0 (AudioresampleBuffer, 1);
buffer->ref_count = 1;
return buffer;
}
AudioresampleBuffer *
audioresample_buffer_new_and_alloc (int size)
{
AudioresampleBuffer *buffer = audioresample_buffer_new ();
buffer->data = g_malloc (size);
buffer->length = size;
buffer->free = audioresample_buffer_free_mem;
return buffer;
}
AudioresampleBuffer *
audioresample_buffer_new_with_data (void *data, int size)
{
AudioresampleBuffer *buffer = audioresample_buffer_new ();
buffer->data = data;
buffer->length = size;
buffer->free = audioresample_buffer_free_mem;
return buffer;
}
AudioresampleBuffer *
audioresample_buffer_new_subbuffer (AudioresampleBuffer * buffer, int offset,
int length)
{
AudioresampleBuffer *subbuffer = audioresample_buffer_new ();
if (buffer->parent) {
audioresample_buffer_ref (buffer->parent);
subbuffer->parent = buffer->parent;
} else {
audioresample_buffer_ref (buffer);
subbuffer->parent = buffer;
}
subbuffer->data = buffer->data + offset;
subbuffer->length = length;
subbuffer->free = audioresample_buffer_free_subbuffer;
return subbuffer;
}
void
audioresample_buffer_ref (AudioresampleBuffer * buffer)
{
buffer->ref_count++;
}
void
audioresample_buffer_unref (AudioresampleBuffer * buffer)
{
buffer->ref_count--;
if (buffer->ref_count == 0) {
if (buffer->free)
buffer->free (buffer, buffer->priv);
g_free (buffer);
}
}
static void
audioresample_buffer_free_mem (AudioresampleBuffer * buffer, void *priv)
{
g_free (buffer->data);
}
static void
audioresample_buffer_free_subbuffer (AudioresampleBuffer * buffer, void *priv)
{
audioresample_buffer_unref (buffer->parent);
}
AudioresampleBufferQueue *
audioresample_buffer_queue_new (void)
{
return g_new0 (AudioresampleBufferQueue, 1);
}
int
audioresample_buffer_queue_get_depth (AudioresampleBufferQueue * queue)
{
return queue->depth;
}
int
audioresample_buffer_queue_get_offset (AudioresampleBufferQueue * queue)
{
return queue->offset;
}
void
audioresample_buffer_queue_free (AudioresampleBufferQueue * queue)
{
GList *g;
for (g = g_list_first (queue->buffers); g; g = g_list_next (g)) {
audioresample_buffer_unref ((AudioresampleBuffer *) g->data);
}
g_list_free (queue->buffers);
g_free (queue);
}
void
audioresample_buffer_queue_push (AudioresampleBufferQueue * queue,
AudioresampleBuffer * buffer)
{
queue->buffers = g_list_append (queue->buffers, buffer);
queue->depth += buffer->length;
}
AudioresampleBuffer *
audioresample_buffer_queue_pull (AudioresampleBufferQueue * queue, int length)
{
GList *g;
AudioresampleBuffer *newbuffer;
AudioresampleBuffer *buffer;
AudioresampleBuffer *subbuffer;
g_return_val_if_fail (length > 0, NULL);
if (queue->depth < length) {
return NULL;
}
RESAMPLE_LOG ("pulling %d, %d available", length, queue->depth);
g = g_list_first (queue->buffers);
buffer = g->data;
if (buffer->length > length) {
newbuffer = audioresample_buffer_new_subbuffer (buffer, 0, length);
subbuffer = audioresample_buffer_new_subbuffer (buffer, length,
buffer->length - length);
g->data = subbuffer;
audioresample_buffer_unref (buffer);
} else {
int offset = 0;
newbuffer = audioresample_buffer_new_and_alloc (length);
while (offset < length) {
g = g_list_first (queue->buffers);
buffer = g->data;
if (buffer->length > length - offset) {
int n = length - offset;
memcpy (newbuffer->data + offset, buffer->data, n);
subbuffer =
audioresample_buffer_new_subbuffer (buffer, n, buffer->length - n);
g->data = subbuffer;
audioresample_buffer_unref (buffer);
offset += n;
} else {
memcpy (newbuffer->data + offset, buffer->data, buffer->length);
queue->buffers = g_list_delete_link (queue->buffers, g);
offset += buffer->length;
audioresample_buffer_unref (buffer);
}
}
}
queue->depth -= length;
queue->offset += length;
return newbuffer;
}
AudioresampleBuffer *
audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int length)
{
GList *g;
AudioresampleBuffer *newbuffer;
AudioresampleBuffer *buffer;
int offset = 0;
g_return_val_if_fail (length > 0, NULL);
if (queue->depth < length) {
return NULL;
}
RESAMPLE_LOG ("peeking %d, %d available", length, queue->depth);
g = g_list_first (queue->buffers);
buffer = g->data;
if (buffer->length > length) {
newbuffer = audioresample_buffer_new_subbuffer (buffer, 0, length);
} else {
newbuffer = audioresample_buffer_new_and_alloc (length);
while (offset < length) {
buffer = g->data;
if (buffer->length > length - offset) {
int n = length - offset;
memcpy (newbuffer->data + offset, buffer->data, n);
offset += n;
} else {
memcpy (newbuffer->data + offset, buffer->data, buffer->length);
offset += buffer->length;
}
g = g_list_next (g);
}
}
return newbuffer;
}
#ifndef __AUDIORESAMPLE_BUFFER_H__
#define __AUDIORESAMPLE_BUFFER_H__
#include <glib.h>
typedef struct _AudioresampleBuffer AudioresampleBuffer;
typedef struct _AudioresampleBufferQueue AudioresampleBufferQueue;
struct _AudioresampleBuffer
{
unsigned char *data;
int length;
int ref_count;
AudioresampleBuffer *parent;
void (*free) (AudioresampleBuffer *, void *);
void *priv;
void *priv2;
};
struct _AudioresampleBufferQueue
{
GList *buffers;
int depth;
int offset;
};
AudioresampleBuffer *audioresample_buffer_new (void);
AudioresampleBuffer *audioresample_buffer_new_and_alloc (int size);
AudioresampleBuffer *audioresample_buffer_new_with_data (void *data, int size);
AudioresampleBuffer *audioresample_buffer_new_subbuffer (AudioresampleBuffer * buffer, int offset,
int length);
void audioresample_buffer_ref (AudioresampleBuffer * buffer);
void audioresample_buffer_unref (AudioresampleBuffer * buffer);
AudioresampleBufferQueue *audioresample_buffer_queue_new (void);
void audioresample_buffer_queue_free (AudioresampleBufferQueue * queue);
int audioresample_buffer_queue_get_depth (AudioresampleBufferQueue * queue);
int audioresample_buffer_queue_get_offset (AudioresampleBufferQueue * queue);
void audioresample_buffer_queue_push (AudioresampleBufferQueue * queue,
AudioresampleBuffer * buffer);
AudioresampleBuffer *audioresample_buffer_queue_pull (AudioresampleBufferQueue * queue, int len);
AudioresampleBuffer *audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int len);
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include <stdio.h>
#include <debug.h>
static const char *resample_debug_level_names[] = {
"NONE",
"ERROR",
"WARNING",
"INFO",
"DEBUG",
"LOG"
};
static int resample_debug_level = RESAMPLE_LEVEL_ERROR;
void
resample_debug_log (int level, const char *file, const char *function,
int line, const char *format, ...)
{
#ifndef GLIB_COMPAT
va_list varargs;
char *s;
if (level > resample_debug_level)
return;
va_start (varargs, format);
s = g_strdup_vprintf (format, varargs);
va_end (varargs);
fprintf (stderr, "RESAMPLE: %s: %s(%d): %s: %s\n",
resample_debug_level_names[level], file, line, function, s);
g_free (s);
#else
va_list varargs;
char s[1000];
if (level > resample_debug_level)
return;
va_start (varargs, format);
vsnprintf (s, 999, format, varargs);
va_end (varargs);
fprintf (stderr, "RESAMPLE: %s: %s(%d): %s: %s\n",
resample_debug_level_names[level], file, line, function, s);
#endif
}
void
resample_debug_set_level (int level)
{
resample_debug_level = level;
}
int
resample_debug_get_level (void)
{
return resample_debug_level;
}
#ifndef __RESAMPLE_DEBUG_H__
#define __RESAMPLE_DEBUG_H__
enum
{
RESAMPLE_LEVEL_NONE = 0,
RESAMPLE_LEVEL_ERROR,
RESAMPLE_LEVEL_WARNING,
RESAMPLE_LEVEL_INFO,
RESAMPLE_LEVEL_DEBUG,
RESAMPLE_LEVEL_LOG
};
#define RESAMPLE_ERROR(...) \
RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_ERROR, __VA_ARGS__)
#define RESAMPLE_WARNING(...) \
RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_WARNING, __VA_ARGS__)
#define RESAMPLE_INFO(...) \
RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_INFO, __VA_ARGS__)
#define RESAMPLE_DEBUG(...) \
RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_DEBUG, __VA_ARGS__)
#define RESAMPLE_LOG(...) \
RESAMPLE_DEBUG_LEVEL(RESAMPLE_LEVEL_LOG, __VA_ARGS__)
#define RESAMPLE_DEBUG_LEVEL(level,...) \
resample_debug_log ((level), __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
void resample_debug_log (int level, const char *file, const char *function,
int line, const char *format, ...);
void resample_debug_set_level (int level);
int resample_debug_get_level (void);
#endif
/* Resampling library
* Copyright (C) <2001> David A. Schleef <ds@schleef.org>
*
* 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 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 <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <audioresample/functable.h>
#include <audioresample/debug.h>
void
functable_func_sinc (double *fx, double *dfx, double x, void *closure)
{
if (x == 0) {
*fx = 1;
*dfx = 0;
return;
}
*fx = sin (x) / x;
*dfx = (cos (x) - sin (x) / x) / x;
}
void
functable_func_boxcar (double *fx, double *dfx, double x, void *closure)
{
double width = *(double *) closure;
if (x < width && x > -width) {
*fx = 1;
} else {
*fx = 0;
}
*dfx = 0;
}
void
functable_func_hanning (double *fx, double *dfx, double x, void *closure)
{
double width = *(double *) closure;
if (x < width && x > -width) {
x /= width;
*fx = (1 - x * x) * (1 - x * x);
*dfx = -2 * 2 * x / width * (1 - x * x);
} else {
*fx = 0;
*dfx = 0;
}
}
Functable *
functable_new (void)
{
Functable *ft;
ft = malloc (sizeof (Functable));
memset (ft, 0, sizeof (Functable));
return ft;
}
void
functable_free (Functable * ft)
{
free (ft);
}
void
functable_set_length (Functable * t, int length)
{
t->length = length;
}
void
functable_set_offset (Functable * t, double offset)
{
t->offset = offset;
}
void
functable_set_multiplier (Functable * t, double multiplier)
{
t->multiplier = multiplier;
}
void
functable_calculate (Functable * t, FunctableFunc func, void *closure)
{
int i;
double x;
if (t->fx)
free (t->fx);
if (t->dfx)
free (t->dfx);
t->fx = malloc (sizeof (double) * (t->length + 1));
t->dfx = malloc (sizeof (double) * (t->length + 1));
t->inv_multiplier = 1.0 / t->multiplier;
for (i = 0; i < t->length + 1; i++) {
x = t->offset + t->multiplier * i;
func (&t->fx[i], &t->dfx[i], x, closure);
}
}
void
functable_calculate_multiply (Functable * t, FunctableFunc func, void *closure)
{
int i;
double x;
for (i = 0; i < t->length + 1; i++) {
double afx, adfx, bfx, bdfx;
afx = t->fx[i];
adfx = t->dfx[i];
x = t->offset + t->multiplier * i;
func (&bfx, &bdfx, x, closure);
t->fx[i] = afx * bfx;
t->dfx[i] = afx * bdfx + adfx * bfx;
}
}
double
functable_evaluate (Functable * t, double x)
{
int i;
double f0, f1, w0, w1;
double x2, x3;
double w;
if (x < t->offset || x > (t->offset + t->length * t->multiplier)) {
RESAMPLE_DEBUG ("x out of range %g", x);
}
x -= t->offset;
x *= t->inv_multiplier;
i = floor (x);
x -= i;
x2 = x * x;
x3 = x2 * x;
f1 = 3 * x2 - 2 * x3;
f0 = 1 - f1;
w0 = (x - 2 * x2 + x3) * t->multiplier;
w1 = (-x2 + x3) * t->multiplier;
w = t->fx[i] * f0 + t->fx[i + 1] * f1 + t->dfx[i] * w0 + t->dfx[i + 1] * w1;
/*w = t->fx[i] * (1-x) + t->fx[i+1] * x; */
return w;
}
double
functable_fir (Functable * t, double x, int n, double *data, int len)
{
int i, j;
double f0, f1, w0, w1;
double x2, x3;
double w;
double sum;
x -= t->offset;
x /= t->multiplier;
i = floor (x);
x -= i;
x2 = x * x;
x3 = x2 * x;
f1 = 3 * x2 - 2 * x3;
f0 = 1 - f1;