diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2013-01-09 22:46:32 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2013-01-09 23:28:08 +0100 |
commit | 3ef06f3415a4693c435bd72a59fedaf4d325a5d9 (patch) | |
tree | 3f6cbb746dfded4f717a88d06ae35603c941e954 | |
parent | dc60ecdbeb11ef14a85a60ab06154b067e5e1f02 (diff) | |
download | ffmpeg-3ef06f3415a4693c435bd72a59fedaf4d325a5d9.tar.gz |
swr: split swri_dither_init() out
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
-rw-r--r-- | libswresample/dither.c | 78 | ||||
-rw-r--r-- | libswresample/swresample.c | 4 | ||||
-rw-r--r-- | libswresample/swresample_internal.h | 1 |
3 files changed, 51 insertions, 32 deletions
diff --git a/libswresample/dither.c b/libswresample/dither.c index c79d37213a..05c75fc0f1 100644 --- a/libswresample/dither.c +++ b/libswresample/dither.c @@ -24,7 +24,7 @@ #include "noise_shaping_data.c" void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt) { - double scale = 0; + double scale = s->dither.ns_scale; #define TMP_EXTRA 2 double *tmp = av_malloc((len + TMP_EXTRA) * sizeof(double)); int i; @@ -32,36 +32,6 @@ void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSa out_fmt = av_get_packed_sample_fmt(out_fmt); in_fmt = av_get_packed_sample_fmt( in_fmt); - if(in_fmt == AV_SAMPLE_FMT_FLT || in_fmt == AV_SAMPLE_FMT_DBL){ - if(out_fmt == AV_SAMPLE_FMT_S32) scale = 1.0/(1L<<31); - if(out_fmt == AV_SAMPLE_FMT_S16) scale = 1.0/(1L<<15); - if(out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1.0/(1L<< 7); - } - if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S16) scale = 1L<<16; - if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<24; - if(in_fmt == AV_SAMPLE_FMT_S16 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<8; - - scale *= s->dither.scale; - - s->dither.ns_pos = 0; - s->dither.ns_scale = scale; - s->dither.ns_scale_1 = 1/scale; - memset(s->dither.ns_errors, 0, sizeof(s->dither.ns_errors)); - for (i=0; filters[i].coefs; i++) { - const filter_t *f = &filters[i]; - if (fabs(s->out_sample_rate - f->rate) / f->rate <= .05 && f->name == s->dither.method) { - int j; - s->dither.ns_taps = f->len; - for (j=0; j<f->len; j++) - s->dither.ns_coeffs[j] = f->coefs[j]; - break; - } - } - if (!filters[i].coefs && s->dither.method > SWR_DITHER_NS) { - av_log(s, AV_LOG_WARNING, "Requested noise shaping dither not available at this sampling rate, using triangular hp dither\n"); - s->dither.method = SWR_DITHER_TRIANGULAR_HIGHPASS; - } - for(i=0; i<len + TMP_EXTRA; i++){ double v; seed = seed* 1664525 + 1013904223; @@ -105,6 +75,52 @@ void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSa av_free(tmp); } +int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt) +{ + int i; + double scale = 0; + + if (s->dither.method > SWR_DITHER_TRIANGULAR_HIGHPASS && s->dither.method <= SWR_DITHER_NS) + return AVERROR(EINVAL); + + out_fmt = av_get_packed_sample_fmt(out_fmt); + in_fmt = av_get_packed_sample_fmt( in_fmt); + + if(in_fmt == AV_SAMPLE_FMT_FLT || in_fmt == AV_SAMPLE_FMT_DBL){ + if(out_fmt == AV_SAMPLE_FMT_S32) scale = 1.0/(1L<<31); + if(out_fmt == AV_SAMPLE_FMT_S16) scale = 1.0/(1L<<15); + if(out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1.0/(1L<< 7); + } + if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S16) scale = 1L<<16; + if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<24; + if(in_fmt == AV_SAMPLE_FMT_S16 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<8; + + scale *= s->dither.scale; + + s->dither.ns_pos = 0; + s->dither.ns_scale = scale; + s->dither.ns_scale_1 = 1/scale; + memset(s->dither.ns_errors, 0, sizeof(s->dither.ns_errors)); + for (i=0; filters[i].coefs; i++) { + const filter_t *f = &filters[i]; + if (fabs(s->out_sample_rate - f->rate) / f->rate <= .05 && f->name == s->dither.method) { + int j; + s->dither.ns_taps = f->len; + for (j=0; j<f->len; j++) + s->dither.ns_coeffs[j] = f->coefs[j]; + break; + } + } + if (!filters[i].coefs && s->dither.method > SWR_DITHER_NS) { + av_log(s, AV_LOG_WARNING, "Requested noise shaping dither not available at this sampling rate, using triangular hp dither\n"); + s->dither.method = SWR_DITHER_TRIANGULAR_HIGHPASS; + } + + s->dither.noise = s->preout; + + return 0; +} + #define TEMPLATE_DITHER_S16 #include "dither_template.c" #undef TEMPLATE_DITHER_S16 diff --git a/libswresample/swresample.c b/libswresample/swresample.c index 7001f23201..5e0e4aa403 100644 --- a/libswresample/swresample.c +++ b/libswresample/swresample.c @@ -230,6 +230,7 @@ av_cold void swr_free(SwrContext **ss){ } av_cold int swr_init(struct SwrContext *s){ + int ret; s->in_buffer_index= 0; s->in_buffer_count= 0; s->resample_in_constraint= 0; @@ -391,7 +392,8 @@ av_assert0(s->out.ch_count); set_audiodata_fmt(&s->in_buffer, s->int_sample_fmt); } - s->dither.noise = s->preout; + if ((ret = swri_dither_init(s, s->out_sample_fmt, s->int_sample_fmt)) < 0) + return ret; if(s->rematrix || s->dither.method) return swri_rematrix_init(s); diff --git a/libswresample/swresample_internal.h b/libswresample/swresample_internal.h index 9276b5c8cb..1b0167cc08 100644 --- a/libswresample/swresample_internal.h +++ b/libswresample/swresample_internal.h @@ -177,6 +177,7 @@ int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mus void swri_rematrix_init_x86(struct SwrContext *s); void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt); +int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt); void swri_audio_convert_init_arm(struct AudioConvert *ac, enum AVSampleFormat out_fmt, |