2012-04-10 20:52:42 +03:00
|
|
|
/*
|
2013-01-09 20:30:01 +03:00
|
|
|
* Copyright (C) 2012-2013 Michael Niedermayer (michaelni@gmx.at)
|
2012-04-10 20:52:42 +03:00
|
|
|
*
|
|
|
|
* This file is part of libswresample
|
|
|
|
*
|
|
|
|
* libswresample 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.
|
|
|
|
*
|
|
|
|
* libswresample 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 libswresample; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "swresample_internal.h"
|
|
|
|
|
2013-01-09 20:03:49 +03:00
|
|
|
#include "noise_shaping_data.c"
|
|
|
|
|
2015-06-03 05:17:48 +02:00
|
|
|
int swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt) {
|
2013-01-10 02:25:09 +03:00
|
|
|
double scale = s->dither.noise_scale;
|
2012-04-11 14:27:22 +03:00
|
|
|
#define TMP_EXTRA 2
|
2014-04-08 01:29:06 +03:00
|
|
|
double *tmp = av_malloc_array(len + TMP_EXTRA, sizeof(double));
|
2012-04-10 20:52:42 +03:00
|
|
|
int i;
|
|
|
|
|
2015-06-03 05:17:48 +02:00
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2012-04-11 14:27:22 +03:00
|
|
|
for(i=0; i<len + TMP_EXTRA; i++){
|
2012-04-10 20:52:42 +03:00
|
|
|
double v;
|
|
|
|
seed = seed* 1664525 + 1013904223;
|
|
|
|
|
2013-01-09 20:41:12 +03:00
|
|
|
switch(s->dither.method){
|
2012-04-10 20:52:42 +03:00
|
|
|
case SWR_DITHER_RECTANGULAR: v= ((double)seed) / UINT_MAX - 0.5; break;
|
2013-01-09 20:03:49 +03:00
|
|
|
default:
|
2013-01-09 20:41:12 +03:00
|
|
|
av_assert0(s->dither.method < SWR_DITHER_NB);
|
2012-04-10 22:10:14 +03:00
|
|
|
v = ((double)seed) / UINT_MAX;
|
|
|
|
seed = seed*1664525 + 1013904223;
|
|
|
|
v-= ((double)seed) / UINT_MAX;
|
|
|
|
break;
|
2012-04-10 20:52:42 +03:00
|
|
|
}
|
2012-04-11 14:27:22 +03:00
|
|
|
tmp[i] = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<len; i++){
|
|
|
|
double v;
|
|
|
|
|
2013-01-09 20:41:12 +03:00
|
|
|
switch(s->dither.method){
|
2013-01-09 20:03:49 +03:00
|
|
|
default:
|
2013-01-09 20:41:12 +03:00
|
|
|
av_assert0(s->dither.method < SWR_DITHER_NB);
|
2012-04-11 14:27:22 +03:00
|
|
|
v = tmp[i];
|
|
|
|
break;
|
|
|
|
case SWR_DITHER_TRIANGULAR_HIGHPASS :
|
|
|
|
v = (- tmp[i] + 2*tmp[i+1] - tmp[i+2]) / sqrt(6);
|
|
|
|
break;
|
|
|
|
}
|
2012-04-10 20:52:42 +03:00
|
|
|
|
|
|
|
v*= scale;
|
|
|
|
|
2013-01-10 01:20:24 +03:00
|
|
|
switch(noise_fmt){
|
|
|
|
case AV_SAMPLE_FMT_S16P: ((int16_t*)dst)[i] = v; break;
|
|
|
|
case AV_SAMPLE_FMT_S32P: ((int32_t*)dst)[i] = v; break;
|
|
|
|
case AV_SAMPLE_FMT_FLTP: ((float *)dst)[i] = v; break;
|
|
|
|
case AV_SAMPLE_FMT_DBLP: ((double *)dst)[i] = v; break;
|
2012-04-10 20:52:42 +03:00
|
|
|
default: av_assert0(0);
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 14:27:22 +03:00
|
|
|
|
|
|
|
av_free(tmp);
|
2015-06-03 05:17:48 +02:00
|
|
|
return 0;
|
2012-04-10 20:52:42 +03:00
|
|
|
}
|
2013-01-09 20:03:49 +03:00
|
|
|
|
2015-02-21 00:55:52 +02:00
|
|
|
av_cold int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt)
|
2013-01-10 00:46:32 +03:00
|
|
|
{
|
|
|
|
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){
|
2015-02-01 20:27:00 +02:00
|
|
|
if(out_fmt == AV_SAMPLE_FMT_S32) scale = 1.0/(1LL<<31);
|
|
|
|
if(out_fmt == AV_SAMPLE_FMT_S16) scale = 1.0/(1LL<<15);
|
|
|
|
if(out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1.0/(1LL<< 7);
|
2013-01-10 00:46:32 +03:00
|
|
|
}
|
2013-06-05 03:47:19 +03:00
|
|
|
if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S32 && (s->dither.output_sample_bits&31)) scale = 1;
|
2015-02-01 20:27:00 +02:00
|
|
|
if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S16) scale = 1<<16;
|
|
|
|
if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1<<24;
|
|
|
|
if(in_fmt == AV_SAMPLE_FMT_S16 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1<<8;
|
2013-01-10 00:46:32 +03:00
|
|
|
|
|
|
|
scale *= s->dither.scale;
|
|
|
|
|
2013-02-24 21:05:54 +03:00
|
|
|
if (out_fmt == AV_SAMPLE_FMT_S32 && s->dither.output_sample_bits)
|
|
|
|
scale *= 1<<(32-s->dither.output_sample_bits);
|
|
|
|
|
2013-01-10 00:46:32 +03:00
|
|
|
s->dither.ns_pos = 0;
|
2013-01-10 02:25:09 +03:00
|
|
|
s->dither.noise_scale= scale;
|
2013-01-10 00:46:32 +03:00
|
|
|
s->dither.ns_scale = scale;
|
2013-01-14 03:06:48 +03:00
|
|
|
s->dither.ns_scale_1 = scale ? 1/scale : 0;
|
2013-01-10 00:46:32 +03:00
|
|
|
memset(s->dither.ns_errors, 0, sizeof(s->dither.ns_errors));
|
|
|
|
for (i=0; filters[i].coefs; i++) {
|
|
|
|
const filter_t *f = &filters[i];
|
2015-08-23 22:31:12 +02:00
|
|
|
if (llabs(s->out_sample_rate - f->rate)*20 <= f->rate && f->name == s->dither.method) {
|
2013-01-10 00:46:32 +03:00
|
|
|
int j;
|
|
|
|
s->dither.ns_taps = f->len;
|
|
|
|
for (j=0; j<f->len; j++)
|
|
|
|
s->dither.ns_coeffs[j] = f->coefs[j];
|
2013-01-10 03:28:24 +03:00
|
|
|
s->dither.ns_scale_1 *= 1 - exp(f->gain_cB * M_LN10 * 0.005) * 2 / (1<<(8*av_get_bytes_per_sample(out_fmt)));
|
2013-01-10 00:46:32 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-10 20:51:30 +03:00
|
|
|
av_assert0(!s->preout.count);
|
2013-01-10 00:46:32 +03:00
|
|
|
s->dither.noise = s->preout;
|
2013-01-10 20:51:30 +03:00
|
|
|
s->dither.temp = s->preout;
|
2013-01-10 01:20:24 +03:00
|
|
|
if (s->dither.method > SWR_DITHER_NS) {
|
|
|
|
s->dither.noise.bps = 4;
|
|
|
|
s->dither.noise.fmt = AV_SAMPLE_FMT_FLTP;
|
2013-01-10 02:27:04 +03:00
|
|
|
s->dither.noise_scale = 1;
|
2013-01-10 01:20:24 +03:00
|
|
|
}
|
2013-01-10 00:46:32 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-09 20:03:49 +03:00
|
|
|
#define TEMPLATE_DITHER_S16
|
|
|
|
#include "dither_template.c"
|
|
|
|
#undef TEMPLATE_DITHER_S16
|
|
|
|
|
|
|
|
#define TEMPLATE_DITHER_S32
|
|
|
|
#include "dither_template.c"
|
|
|
|
#undef TEMPLATE_DITHER_S32
|
|
|
|
|
|
|
|
#define TEMPLATE_DITHER_FLT
|
|
|
|
#include "dither_template.c"
|
|
|
|
#undef TEMPLATE_DITHER_FLT
|
|
|
|
|
|
|
|
#define TEMPLATE_DITHER_DBL
|
|
|
|
#include "dither_template.c"
|
|
|
|
#undef TEMPLATE_DITHER_DBL
|