libswresample/dither.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2013 Michael Niedermayer (michaelni@gmx.at)
3  *
4  * This file is part of libswresample
5  *
6  * libswresample is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * libswresample is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with libswresample; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/avassert.h"
22 #include "swresample_internal.h"
23 
24 #include "noise_shaping_data.c"
25 
26 void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt) {
27  double scale = s->dither.noise_scale;
28 #define TMP_EXTRA 2
29  double *tmp = av_malloc((len + TMP_EXTRA) * sizeof(double));
30  int i;
31 
32  for(i=0; i<len + TMP_EXTRA; i++){
33  double v;
34  seed = seed* 1664525 + 1013904223;
35 
36  switch(s->dither.method){
37  case SWR_DITHER_RECTANGULAR: v= ((double)seed) / UINT_MAX - 0.5; break;
38  default:
40  v = ((double)seed) / UINT_MAX;
41  seed = seed*1664525 + 1013904223;
42  v-= ((double)seed) / UINT_MAX;
43  break;
44  }
45  tmp[i] = v;
46  }
47 
48  for(i=0; i<len; i++){
49  double v;
50 
51  switch(s->dither.method){
52  default:
54  v = tmp[i];
55  break;
57  v = (- tmp[i] + 2*tmp[i+1] - tmp[i+2]) / sqrt(6);
58  break;
59  }
60 
61  v*= scale;
62 
63  switch(noise_fmt){
64  case AV_SAMPLE_FMT_S16P: ((int16_t*)dst)[i] = v; break;
65  case AV_SAMPLE_FMT_S32P: ((int32_t*)dst)[i] = v; break;
66  case AV_SAMPLE_FMT_FLTP: ((float *)dst)[i] = v; break;
67  case AV_SAMPLE_FMT_DBLP: ((double *)dst)[i] = v; break;
68  default: av_assert0(0);
69  }
70  }
71 
72  av_free(tmp);
73 }
74 
75 int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt)
76 {
77  int i;
78  double scale = 0;
79 
81  return AVERROR(EINVAL);
82 
83  out_fmt = av_get_packed_sample_fmt(out_fmt);
84  in_fmt = av_get_packed_sample_fmt( in_fmt);
85 
86  if(in_fmt == AV_SAMPLE_FMT_FLT || in_fmt == AV_SAMPLE_FMT_DBL){
87  if(out_fmt == AV_SAMPLE_FMT_S32) scale = 1.0/(1L<<31);
88  if(out_fmt == AV_SAMPLE_FMT_S16) scale = 1.0/(1L<<15);
89  if(out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1.0/(1L<< 7);
90  }
91  if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S16) scale = 1L<<16;
92  if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<24;
93  if(in_fmt == AV_SAMPLE_FMT_S16 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<8;
94 
95  scale *= s->dither.scale;
96 
97  if (out_fmt == AV_SAMPLE_FMT_S32 && s->dither.output_sample_bits)
98  scale *= 1<<(32-s->dither.output_sample_bits);
99 
100  s->dither.ns_pos = 0;
102  s->dither.ns_scale = scale;
103  s->dither.ns_scale_1 = scale ? 1/scale : 0;
104  memset(s->dither.ns_errors, 0, sizeof(s->dither.ns_errors));
105  for (i=0; filters[i].coefs; i++) {
106  const filter_t *f = &filters[i];
107  if (fabs(s->out_sample_rate - f->rate) / f->rate <= .05 && f->name == s->dither.method) {
108  int j;
109  s->dither.ns_taps = f->len;
110  for (j=0; j<f->len; j++)
111  s->dither.ns_coeffs[j] = f->coefs[j];
112  s->dither.ns_scale_1 *= 1 - exp(f->gain_cB * M_LN10 * 0.005) * 2 / (1<<(8*av_get_bytes_per_sample(out_fmt)));
113  break;
114  }
115  }
116  if (!filters[i].coefs && s->dither.method > SWR_DITHER_NS) {
117  av_log(s, AV_LOG_WARNING, "Requested noise shaping dither not available at this sampling rate, using triangular hp dither\n");
119  }
120 
121  av_assert0(!s->preout.count);
122  s->dither.noise = s->preout;
123  s->dither.temp = s->preout;
124  if (s->dither.method > SWR_DITHER_NS) {
125  s->dither.noise.bps = 4;
127  s->dither.noise_scale = 1;
128  }
129 
130  return 0;
131 }
132 
133 #define TEMPLATE_DITHER_S16
134 #include "dither_template.c"
135 #undef TEMPLATE_DITHER_S16
136 
137 #define TEMPLATE_DITHER_S32
138 #include "dither_template.c"
139 #undef TEMPLATE_DITHER_S32
140 
141 #define TEMPLATE_DITHER_FLT
142 #include "dither_template.c"
143 #undef TEMPLATE_DITHER_FLT
144 
145 #define TEMPLATE_DITHER_DBL
146 #include "dither_template.c"
147 #undef TEMPLATE_DITHER_DBL
float v
const char * s
Definition: avisynth_c.h:668
AudioData temp
temporary storage when writing into the input buffer isnt possible
int out_sample_rate
output sample rate
enum AVResampleDitherMethod method
int count
number of samples
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
Sinusoidal phase f
signed 16 bits
Definition: samplefmt.h:52
float ns_scale
Noise shaping dither scale.
float ns_coeffs[NS_TAPS]
Noise shaping filter coefficients.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define TMP_EXTRA
float ns_errors[SWR_CH_MAX][2 *NS_TAPS]
AV_SAMPLE_FMT_U8
enum AVSampleFormat fmt
sample format
integer sqrt
Definition: avutil.txt:2
for audio filters
signed 32 bits, planar
Definition: samplefmt.h:59
float, planar
Definition: samplefmt.h:60
enum SwrDitherType name
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
int output_sample_bits
the number of used output bits, needed to scale dither correctly
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
AudioData preout
pre-output audio data: used for rematrix/resample
not part of API/ABI
Definition: swresample.h:123
signed 32 bits
Definition: samplefmt.h:53
void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt)
AudioData noise
noise used for dithering
int32_t
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
Definition: samplefmt.c:73
not part of API/ABI
Definition: swresample.h:115
int bps
bytes per sample
#define L(x)
float ns_scale_1
Noise shaping dither scale^-1.
float noise_scale
Noise scale.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:104
1i.*Xphase exp()
double const * coefs
static unsigned int seed
Definition: videogen.c:78
int ns_pos
Noise shaping dither position.
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
struct DitherContext dither
synthesis window for stochastic i
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define M_LN10
Definition: mathematics.h:37
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt)
Same thing on a dB scale
int len
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
signed 16 bits, planar
Definition: samplefmt.h:58
double, planar
Definition: samplefmt.h:61
int ns_taps
Noise shaping dither taps.