swr: split out DSP functions.

DSP bits of swri_resample go into their own mini-DSP functions; DSP
init goes from a per-call branch in multiple_resample to a proper
DSP init routine; x86 bits go into x86/; swri_resample() moves out of
resample_template.c into resample.c because it's independent of DSP
code or sample type; multiple_resample() is simplified.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
Ronald S. Bultje
2014-06-14 14:11:03 -04:00
committed by Michael Niedermayer
parent 9236f7b5a2
commit 7128a35f8c
7 changed files with 319 additions and 142 deletions

View File

@@ -25,32 +25,8 @@
* @author Michael Niedermayer <michaelni@gmx.at>
*/
#include "libavutil/log.h"
#include "libavutil/avassert.h"
#include "swresample_internal.h"
typedef struct ResampleContext {
const AVClass *av_class;
uint8_t *filter_bank;
int filter_length;
int filter_alloc;
int ideal_dst_incr;
int dst_incr;
int index;
int frac;
int src_incr;
int compensation_distance;
int phase_shift;
int phase_mask;
int linear;
enum SwrFilterType filter_type;
int kaiser_beta;
double factor;
enum AVSampleFormat format;
int felem_size;
int filter_shift;
} ResampleContext;
#include "resample.h"
/**
* 0th order modified bessel function of the first kind.
@@ -197,7 +173,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 precision, int cheby){
double precision, int cheby)
{
double cutoff = cutoff0? cutoff0 : 0.97;
double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
int phase_count= 1<<phase_shift;
@@ -259,6 +236,8 @@ static ResampleContext *resample_init(ResampleContext *c, int out_rate, int in_r
c->index= -phase_count*((c->filter_length-1)/2);
c->frac= 0;
swresample_dsp_init(c);
return c;
error:
av_freep(&c->filter_bank);
@@ -282,59 +261,53 @@ static int set_compensation(ResampleContext *c, int sample_delta, int compensati
return 0;
}
#define TEMPLATE_RESAMPLE_S16
#include "resample_template.c"
#undef TEMPLATE_RESAMPLE_S16
static int swri_resample(ResampleContext *c,
uint8_t *dst, const uint8_t *src, int *consumed,
int src_size, int dst_size, int update_ctx)
{
int fn_idx = c->format - AV_SAMPLE_FMT_S16P;
#define TEMPLATE_RESAMPLE_S32
#include "resample_template.c"
#undef TEMPLATE_RESAMPLE_S32
if (c->filter_length == 1 && c->phase_shift == 0) {
int index= c->index;
int frac= c->frac;
int dst_incr_frac= c->dst_incr % c->src_incr;
int dst_incr= c->dst_incr / c->src_incr;
int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*index;
int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
int new_size = (src_size * (int64_t)c->src_incr - frac + c->dst_incr - 1) / c->dst_incr;
#define TEMPLATE_RESAMPLE_FLT
#include "resample_template.c"
#undef TEMPLATE_RESAMPLE_FLT
dst_size= FFMIN(dst_size, new_size);
c->dsp.resample_one[fn_idx](dst, src, dst_size, index2, incr);
#define TEMPLATE_RESAMPLE_DBL
#include "resample_template.c"
#undef TEMPLATE_RESAMPLE_DBL
index += dst_size * dst_incr;
index += (frac + dst_size * (int64_t)dst_incr_frac) / c->src_incr;
av_assert2(index >= 0);
*consumed= index;
if (update_ctx) {
c->frac = (frac + dst_size * (int64_t)dst_incr_frac) % c->src_incr;
c->index = 0;
}
} else {
int64_t end_index = (1LL + src_size - c->filter_length) << c->phase_shift;
int64_t delta_frac = (end_index - c->index) * c->src_incr - c->frac;
int delta_n = (delta_frac + c->dst_incr - 1) / c->dst_incr;
// XXX FIXME the whole C loop should be written in asm so this x86 specific code here isnt needed
#if HAVE_MMXEXT_INLINE
dst_size = FFMIN(dst_size, delta_n);
if (!c->linear) {
*consumed = c->dsp.resample_common[fn_idx](c, dst, src, dst_size, update_ctx);
} else {
*consumed = c->dsp.resample_linear[fn_idx](c, dst, src, dst_size, update_ctx);
}
}
#include "x86/resample_mmx.h"
#define TEMPLATE_RESAMPLE_S16_MMX2
#include "resample_template.c"
#undef TEMPLATE_RESAMPLE_S16_MMX2
#if HAVE_SSE_INLINE
#define TEMPLATE_RESAMPLE_FLT_SSE
#include "resample_template.c"
#undef TEMPLATE_RESAMPLE_FLT_SSE
#endif
#if HAVE_SSE2_INLINE
#define TEMPLATE_RESAMPLE_S16_SSE2
#include "resample_template.c"
#undef TEMPLATE_RESAMPLE_S16_SSE2
#define TEMPLATE_RESAMPLE_DBL_SSE2
#include "resample_template.c"
#undef TEMPLATE_RESAMPLE_DBL_SSE2
#endif
#if HAVE_AVX_INLINE
#define TEMPLATE_RESAMPLE_FLT_AVX
#include "resample_template.c"
#undef TEMPLATE_RESAMPLE_FLT_AVX
#endif
#endif // HAVE_MMXEXT_INLINE
return dst_size;
}
static int multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){
int i, ret= -1;
int av_unused mm_flags = av_get_cpu_flags();
int need_emms= 0;
int need_emms = c->format == AV_SAMPLE_FMT_S16P && ARCH_X86_32 &&
(mm_flags & (AV_CPU_FLAG_MMX2 | AV_CPU_FLAG_SSE2)) == AV_CPU_FLAG_MMX2;
int64_t max_src_size = (INT64_MAX >> (c->phase_shift+1)) / c->src_incr;
if (c->compensation_distance)
@@ -342,32 +315,8 @@ static int multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, A
src_size = FFMIN(src_size, max_src_size);
for(i=0; i<dst->ch_count; i++){
#if HAVE_MMXEXT_INLINE
#if HAVE_SSE2_INLINE
if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_SSE2)) ret= swri_resample_int16_sse2 (c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
else
#endif
if(c->format == AV_SAMPLE_FMT_S16P && (mm_flags&AV_CPU_FLAG_MMX2 )){
ret= swri_resample_int16_mmx2 (c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
need_emms= 1;
} else
#endif
if(c->format == AV_SAMPLE_FMT_S16P) ret= swri_resample_int16(c, (int16_t*)dst->ch[i], (const int16_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
else if(c->format == AV_SAMPLE_FMT_S32P) ret= swri_resample_int32(c, (int32_t*)dst->ch[i], (const int32_t*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
#if HAVE_AVX_INLINE
else if(c->format == AV_SAMPLE_FMT_FLTP && (mm_flags&AV_CPU_FLAG_AVX))
ret= swri_resample_float_avx (c, (float*)dst->ch[i], (const float*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
#endif
#if HAVE_SSE_INLINE
else if(c->format == AV_SAMPLE_FMT_FLTP && (mm_flags&AV_CPU_FLAG_SSE))
ret= swri_resample_float_sse (c, (float*)dst->ch[i], (const float*)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
#endif
else if(c->format == AV_SAMPLE_FMT_FLTP) ret= swri_resample_float(c, (float *)dst->ch[i], (const float *)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
#if HAVE_SSE2_INLINE
else if(c->format == AV_SAMPLE_FMT_DBLP && (mm_flags&AV_CPU_FLAG_SSE2))
ret= swri_resample_double_sse2(c,(double *)dst->ch[i], (const double *)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
#endif
else if(c->format == AV_SAMPLE_FMT_DBLP) ret= swri_resample_double(c,(double *)dst->ch[i], (const double *)src->ch[i], consumed, src_size, dst_size, i+1==dst->ch_count);
ret= swri_resample(c, dst->ch[i], src->ch[i],
consumed, src_size, dst_size, i+1==dst->ch_count);
}
if(need_emms)
emms_c();