Commit 801b3157 authored by Rob Sykes's avatar Rob Sykes Committed by Michael Niedermayer

swr: Add SOX resampler support

Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent 41049d07
......@@ -213,6 +213,7 @@ External library support:
--enable-libpulse enable Pulseaudio input via libpulse [no]
--enable-librtmp enable RTMP[E] support via librtmp [no]
--enable-libschroedinger enable Dirac de/encoding via libschroedinger [no]
--enable-libsoxr enable Include libsoxr resampling [no]
--enable-libspeex enable Speex de/encoding via libspeex [no]
--enable-libstagefright-h264 enable H.264 decoding via libstagefright [no]
--enable-libtheora enable Theora encoding via libtheora [no]
......@@ -1173,6 +1174,7 @@ CONFIG_LIST="
libpulse
librtmp
libschroedinger
libsoxr
libspeex
libstagefright_h264
libtheora
......@@ -3839,6 +3841,7 @@ enabled libopus && require_pkg_config opus opus_multistream.h opus_multistrea
enabled libpulse && require_pkg_config libpulse-simple pulse/simple.h pa_simple_new
enabled librtmp && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
enabled libschroedinger && require_pkg_config schroedinger-1.0 schroedinger/schro.h schro_init
enabled libsoxr && require libsoxr soxr.h soxr_create -lsoxr
enabled libspeex && require libspeex speex/speex.h speex_decoder_init -lspeex
enabled libstagefright_h264 && require_cpp libstagefright_h264 "binder/ProcessState.h media/stagefright/MetaData.h
media/stagefright/MediaBufferGroup.h media/stagefright/MediaDebug.h media/stagefright/MediaDefs.h
......@@ -4254,6 +4257,7 @@ echo "libopus enabled ${libopus-no}"
echo "libpulse enabled ${libpulse-no}"
echo "librtmp enabled ${librtmp-no}"
echo "libschroedinger enabled ${libschroedinger-no}"
echo "libsoxr enabled ${libsoxr-no}"
echo "libspeex enabled ${libspeex-no}"
echo "libstagefright-h264 enabled ${libstagefright_h264-no}"
echo "libtheora enabled ${libtheora-no}"
......
......@@ -13,4 +13,6 @@ OBJS = audioconvert.o \
resample.o \
swresample.o \
OBJS-$(CONFIG_LIBSOXR) += soxr_resample.o
TESTPROGS = swresample
......@@ -196,7 +196,8 @@ static int build_filter(ResampleContext *c, void *filter, double factor, int tap
}
static ResampleContext *resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
double cutoff0, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta){
double cutoff0, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta,
double precision, int cheby){
double cutoff = cutoff0? cutoff0 : 0.8;
double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
int phase_count= 1<<phase_shift;
......
/*
* audio resampling with soxr
* Copyright (c) 2012 Rob Sykes <aquegg@yahoo.co.uk>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* audio resampling with soxr
*/
#include "libavutil/log.h"
#include "swresample_internal.h"
#include <soxr.h>
static struct ResampleContext *create(struct ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
double cutoff, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta, double precision, int cheby){
soxr_error_t error;
soxr_datatype_t type =
format == AV_SAMPLE_FMT_S16P? SOXR_INT16_S :
format == AV_SAMPLE_FMT_S16 ? SOXR_INT16_I :
format == AV_SAMPLE_FMT_S32P? SOXR_INT32_S :
format == AV_SAMPLE_FMT_S32 ? SOXR_INT32_I :
format == AV_SAMPLE_FMT_FLTP? SOXR_FLOAT32_S :
format == AV_SAMPLE_FMT_FLT ? SOXR_FLOAT32_I :
format == AV_SAMPLE_FMT_DBLP? SOXR_FLOAT64_S :
format == AV_SAMPLE_FMT_DBL ? SOXR_FLOAT64_I : (soxr_datatype_t)-1;
soxr_io_spec_t io_spec = soxr_io_spec(type, type);
soxr_quality_spec_t q_spec = soxr_quality_spec((int)((precision-2)/4), (SOXR_HI_PREC_CLOCK|SOXR_ROLLOFF_NONE)*!!cheby);
q_spec.bits = linear? 0 : precision;
q_spec.bw_pc = cutoff? FFMAX(FFMIN(cutoff,.995),.8)*100 : q_spec.bw_pc;
soxr_delete((soxr_t)c);
c = (struct ResampleContext *)
soxr_create(in_rate, out_rate, 0, &error, &io_spec, &q_spec, 0);
if (!c)
av_log(NULL, AV_LOG_ERROR, "soxr_create: %s\n", error);
return c;
}
static void destroy(struct ResampleContext * *c){
soxr_delete((soxr_t)*c);
*c = NULL;
}
static int flush(struct SwrContext *s){
soxr_process((soxr_t)s->resample, NULL, 0, NULL, NULL, 0, NULL);
return 0;
}
static int process(
struct ResampleContext * c, AudioData *dst, int dst_size,
AudioData *src, int src_size, int *consumed){
size_t idone, odone;
soxr_error_t error = soxr_set_error((soxr_t)c, soxr_set_num_channels((soxr_t)c, src->ch_count));
error = soxr_process((soxr_t)c, src->ch, (size_t)src_size,
&idone, dst->ch, (size_t)dst_size, &odone);
*consumed = (int)idone;
return error? -1 : odone;
}
static int64_t get_delay(struct SwrContext *s, int64_t base){
double delay_s = soxr_delay((soxr_t)s->resample) / s->out_sample_rate;
return (int64_t)(delay_s * base + .5);
}
struct Resampler const soxr_resampler={
create, destroy, process, flush, NULL /* set_compensation */, get_delay,
};
......@@ -86,6 +86,9 @@ static const AVOption options[]={
{"cutoff" , "set cutoff frequency ratio" , OFFSET(cutoff) , AV_OPT_TYPE_DOUBLE,{.dbl=0. }, 0 , 1 , PARAM },
{"resampler" , "set resampling Engine" , OFFSET(engine) , AV_OPT_TYPE_INT , {.i64=0 }, 0 , SWR_ENGINE_NB-1, PARAM, "resampler"},
{"swr" , "select SW Resampler" , 0 , AV_OPT_TYPE_CONST, {.i64=SWR_ENGINE_SWR }, INT_MIN, INT_MAX , PARAM, "resampler"},
{"soxr" , "select SoX Resampler" , 0 , AV_OPT_TYPE_CONST, {.i64=SWR_ENGINE_SOXR }, INT_MIN, INT_MAX , PARAM, "resampler"},
{"precision" , "set resampling precision" , OFFSET(precision) , AV_OPT_TYPE_DOUBLE,{.dbl=20.0 }, 15.0 , 33.0 , PARAM },
{"cheby" , "enable Chebyshev passband" , OFFSET(cheby) , AV_OPT_TYPE_INT , {.i64=0 }, 0 , 1 , PARAM },
{"min_comp" , "set minimum difference between timestamps and audio data (in seconds) below which no timestamp compensation of either kind is applied"
, OFFSET(min_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=FLT_MAX }, 0 , FLT_MAX , PARAM },
{"min_hard_comp" , "set minimum difference between timestamps and audio data (in seconds) to trigger padding/trimming the data."
......@@ -262,6 +265,10 @@ av_cold int swr_init(struct SwrContext *s){
}
switch(s->engine){
#if CONFIG_LIBSOXR
extern struct Resampler const soxr_resampler;
case SWR_ENGINE_SOXR: s->resampler = &soxr_resampler; break;
#endif
case SWR_ENGINE_SWR : s->resampler = &swri_resampler; break;
default:
av_log(s, AV_LOG_ERROR, "Requested resampling engine is unavailable\n");
......@@ -272,7 +279,7 @@ av_cold int swr_init(struct SwrContext *s){
set_audiodata_fmt(&s->out, s->out_sample_fmt);
if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
s->resample = s->resampler->init(s->resample, s->out_sample_rate, s->in_sample_rate, s->filter_size, s->phase_shift, s->linear_interp, s->cutoff, s->int_sample_fmt, s->filter_type, s->kaiser_beta);
s->resample = s->resampler->init(s->resample, s->out_sample_rate, s->in_sample_rate, s->filter_size, s->phase_shift, s->linear_interp, s->cutoff, s->int_sample_fmt, s->filter_type, s->kaiser_beta, s->precision, s->cheby);
}else
s->resampler->free(&s->resample);
if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
......@@ -491,7 +498,7 @@ static int resample(SwrContext *s, AudioData *out_param, int out_count,
}
}
if(in_count && !s->in_buffer_count){
if((s->flushed || in_count) && !s->in_buffer_count){
s->in_buffer_index=0;
ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
out_count -= ret;
......
......@@ -117,6 +117,7 @@ enum SwrDitherType {
/** Resampling Engines */
enum SwrEngine {
SWR_ENGINE_SWR, /**< SW Resampler */
SWR_ENGINE_SOXR, /**< SoX Resampler */
SWR_ENGINE_NB, ///< not part of API/ABI
};
......
......@@ -77,6 +77,8 @@ struct SwrContext {
double cutoff; /**< resampling cutoff frequency. 1.0 corresponds to half the output sample rate */
enum SwrFilterType filter_type; /**< resampling filter type */
int kaiser_beta; /**< beta value for Kaiser window (only applicable if filter_type == AV_FILTER_TYPE_KAISER) */
double precision; /**< resampling precision (in bits) */
int cheby; /**< if 1 then the resampling FIR filter will be configured for maximal passband flatness */
float min_compensation; ///< minimum below which no compensation will happen
float min_hard_compensation; ///< minimum below which no silence inject / sample drop will happen
......@@ -125,7 +127,7 @@ struct SwrContext {
};
typedef struct ResampleContext * (* resample_init_func)(struct ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
double cutoff, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta);
double cutoff, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta, double precision, int cheby);
typedef void (* resample_free_func)(struct ResampleContext **c);
typedef int (* multiple_resample_func)(struct ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed);
typedef int (* resample_flush_func)(struct SwrContext *c);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment