annotate ffmpeg/libavresample/dither.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
yading@11 3 *
yading@11 4 * Triangular with Noise Shaping is based on opusfile.
yading@11 5 * Copyright (c) 1994-2012 by the Xiph.Org Foundation and contributors
yading@11 6 *
yading@11 7 * This file is part of Libav.
yading@11 8 *
yading@11 9 * Libav is free software; you can redistribute it and/or
yading@11 10 * modify it under the terms of the GNU Lesser General Public
yading@11 11 * License as published by the Free Software Foundation; either
yading@11 12 * version 2.1 of the License, or (at your option) any later version.
yading@11 13 *
yading@11 14 * Libav is distributed in the hope that it will be useful,
yading@11 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 17 * Lesser General Public License for more details.
yading@11 18 *
yading@11 19 * You should have received a copy of the GNU Lesser General Public
yading@11 20 * License along with Libav; if not, write to the Free Software
yading@11 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 22 */
yading@11 23
yading@11 24 /**
yading@11 25 * @file
yading@11 26 * Dithered Audio Sample Quantization
yading@11 27 *
yading@11 28 * Converts from dbl, flt, or s32 to s16 using dithering.
yading@11 29 */
yading@11 30
yading@11 31 #include <math.h>
yading@11 32 #include <stdint.h>
yading@11 33
yading@11 34 #include "libavutil/common.h"
yading@11 35 #include "libavutil/lfg.h"
yading@11 36 #include "libavutil/mem.h"
yading@11 37 #include "libavutil/samplefmt.h"
yading@11 38 #include "audio_convert.h"
yading@11 39 #include "dither.h"
yading@11 40 #include "internal.h"
yading@11 41
yading@11 42 typedef struct DitherState {
yading@11 43 int mute;
yading@11 44 unsigned int seed;
yading@11 45 AVLFG lfg;
yading@11 46 float *noise_buf;
yading@11 47 int noise_buf_size;
yading@11 48 int noise_buf_ptr;
yading@11 49 float dither_a[4];
yading@11 50 float dither_b[4];
yading@11 51 } DitherState;
yading@11 52
yading@11 53 struct DitherContext {
yading@11 54 DitherDSPContext ddsp;
yading@11 55 enum AVResampleDitherMethod method;
yading@11 56 int apply_map;
yading@11 57 ChannelMapInfo *ch_map_info;
yading@11 58
yading@11 59 int mute_dither_threshold; // threshold for disabling dither
yading@11 60 int mute_reset_threshold; // threshold for resetting noise shaping
yading@11 61 const float *ns_coef_b; // noise shaping coeffs
yading@11 62 const float *ns_coef_a; // noise shaping coeffs
yading@11 63
yading@11 64 int channels;
yading@11 65 DitherState *state; // dither states for each channel
yading@11 66
yading@11 67 AudioData *flt_data; // input data in fltp
yading@11 68 AudioData *s16_data; // dithered output in s16p
yading@11 69 AudioConvert *ac_in; // converter for input to fltp
yading@11 70 AudioConvert *ac_out; // converter for s16p to s16 (if needed)
yading@11 71
yading@11 72 void (*quantize)(int16_t *dst, const float *src, float *dither, int len);
yading@11 73 int samples_align;
yading@11 74 };
yading@11 75
yading@11 76 /* mute threshold, in seconds */
yading@11 77 #define MUTE_THRESHOLD_SEC 0.000333
yading@11 78
yading@11 79 /* scale factor for 16-bit output.
yading@11 80 The signal is attenuated slightly to avoid clipping */
yading@11 81 #define S16_SCALE 32753.0f
yading@11 82
yading@11 83 /* scale to convert lfg from INT_MIN/INT_MAX to -0.5/0.5 */
yading@11 84 #define LFG_SCALE (1.0f / (2.0f * INT32_MAX))
yading@11 85
yading@11 86 /* noise shaping coefficients */
yading@11 87
yading@11 88 static const float ns_48_coef_b[4] = {
yading@11 89 2.2374f, -0.7339f, -0.1251f, -0.6033f
yading@11 90 };
yading@11 91
yading@11 92 static const float ns_48_coef_a[4] = {
yading@11 93 0.9030f, 0.0116f, -0.5853f, -0.2571f
yading@11 94 };
yading@11 95
yading@11 96 static const float ns_44_coef_b[4] = {
yading@11 97 2.2061f, -0.4707f, -0.2534f, -0.6213f
yading@11 98 };
yading@11 99
yading@11 100 static const float ns_44_coef_a[4] = {
yading@11 101 1.0587f, 0.0676f, -0.6054f, -0.2738f
yading@11 102 };
yading@11 103
yading@11 104 static void dither_int_to_float_rectangular_c(float *dst, int *src, int len)
yading@11 105 {
yading@11 106 int i;
yading@11 107 for (i = 0; i < len; i++)
yading@11 108 dst[i] = src[i] * LFG_SCALE;
yading@11 109 }
yading@11 110
yading@11 111 static void dither_int_to_float_triangular_c(float *dst, int *src0, int len)
yading@11 112 {
yading@11 113 int i;
yading@11 114 int *src1 = src0 + len;
yading@11 115
yading@11 116 for (i = 0; i < len; i++) {
yading@11 117 float r = src0[i] * LFG_SCALE;
yading@11 118 r += src1[i] * LFG_SCALE;
yading@11 119 dst[i] = r;
yading@11 120 }
yading@11 121 }
yading@11 122
yading@11 123 static void quantize_c(int16_t *dst, const float *src, float *dither, int len)
yading@11 124 {
yading@11 125 int i;
yading@11 126 for (i = 0; i < len; i++)
yading@11 127 dst[i] = av_clip_int16(lrintf(src[i] * S16_SCALE + dither[i]));
yading@11 128 }
yading@11 129
yading@11 130 #define SQRT_1_6 0.40824829046386301723f
yading@11 131
yading@11 132 static void dither_highpass_filter(float *src, int len)
yading@11 133 {
yading@11 134 int i;
yading@11 135
yading@11 136 /* filter is from libswresample in FFmpeg */
yading@11 137 for (i = 0; i < len - 2; i++)
yading@11 138 src[i] = (-src[i] + 2 * src[i + 1] - src[i + 2]) * SQRT_1_6;
yading@11 139 }
yading@11 140
yading@11 141 static int generate_dither_noise(DitherContext *c, DitherState *state,
yading@11 142 int min_samples)
yading@11 143 {
yading@11 144 int i;
yading@11 145 int nb_samples = FFALIGN(min_samples, 16) + 16;
yading@11 146 int buf_samples = nb_samples *
yading@11 147 (c->method == AV_RESAMPLE_DITHER_RECTANGULAR ? 1 : 2);
yading@11 148 unsigned int *noise_buf_ui;
yading@11 149
yading@11 150 av_freep(&state->noise_buf);
yading@11 151 state->noise_buf_size = state->noise_buf_ptr = 0;
yading@11 152
yading@11 153 state->noise_buf = av_malloc(buf_samples * sizeof(*state->noise_buf));
yading@11 154 if (!state->noise_buf)
yading@11 155 return AVERROR(ENOMEM);
yading@11 156 state->noise_buf_size = FFALIGN(min_samples, 16);
yading@11 157 noise_buf_ui = (unsigned int *)state->noise_buf;
yading@11 158
yading@11 159 av_lfg_init(&state->lfg, state->seed);
yading@11 160 for (i = 0; i < buf_samples; i++)
yading@11 161 noise_buf_ui[i] = av_lfg_get(&state->lfg);
yading@11 162
yading@11 163 c->ddsp.dither_int_to_float(state->noise_buf, noise_buf_ui, nb_samples);
yading@11 164
yading@11 165 if (c->method == AV_RESAMPLE_DITHER_TRIANGULAR_HP)
yading@11 166 dither_highpass_filter(state->noise_buf, nb_samples);
yading@11 167
yading@11 168 return 0;
yading@11 169 }
yading@11 170
yading@11 171 static void quantize_triangular_ns(DitherContext *c, DitherState *state,
yading@11 172 int16_t *dst, const float *src,
yading@11 173 int nb_samples)
yading@11 174 {
yading@11 175 int i, j;
yading@11 176 float *dither = &state->noise_buf[state->noise_buf_ptr];
yading@11 177
yading@11 178 if (state->mute > c->mute_reset_threshold)
yading@11 179 memset(state->dither_a, 0, sizeof(state->dither_a));
yading@11 180
yading@11 181 for (i = 0; i < nb_samples; i++) {
yading@11 182 float err = 0;
yading@11 183 float sample = src[i] * S16_SCALE;
yading@11 184
yading@11 185 for (j = 0; j < 4; j++) {
yading@11 186 err += c->ns_coef_b[j] * state->dither_b[j] -
yading@11 187 c->ns_coef_a[j] * state->dither_a[j];
yading@11 188 }
yading@11 189 for (j = 3; j > 0; j--) {
yading@11 190 state->dither_a[j] = state->dither_a[j - 1];
yading@11 191 state->dither_b[j] = state->dither_b[j - 1];
yading@11 192 }
yading@11 193 state->dither_a[0] = err;
yading@11 194 sample -= err;
yading@11 195
yading@11 196 if (state->mute > c->mute_dither_threshold) {
yading@11 197 dst[i] = av_clip_int16(lrintf(sample));
yading@11 198 state->dither_b[0] = 0;
yading@11 199 } else {
yading@11 200 dst[i] = av_clip_int16(lrintf(sample + dither[i]));
yading@11 201 state->dither_b[0] = av_clipf(dst[i] - sample, -1.5f, 1.5f);
yading@11 202 }
yading@11 203
yading@11 204 state->mute++;
yading@11 205 if (src[i])
yading@11 206 state->mute = 0;
yading@11 207 }
yading@11 208 }
yading@11 209
yading@11 210 static int convert_samples(DitherContext *c, int16_t **dst, float * const *src,
yading@11 211 int channels, int nb_samples)
yading@11 212 {
yading@11 213 int ch, ret;
yading@11 214 int aligned_samples = FFALIGN(nb_samples, 16);
yading@11 215
yading@11 216 for (ch = 0; ch < channels; ch++) {
yading@11 217 DitherState *state = &c->state[ch];
yading@11 218
yading@11 219 if (state->noise_buf_size < aligned_samples) {
yading@11 220 ret = generate_dither_noise(c, state, nb_samples);
yading@11 221 if (ret < 0)
yading@11 222 return ret;
yading@11 223 } else if (state->noise_buf_size - state->noise_buf_ptr < aligned_samples) {
yading@11 224 state->noise_buf_ptr = 0;
yading@11 225 }
yading@11 226
yading@11 227 if (c->method == AV_RESAMPLE_DITHER_TRIANGULAR_NS) {
yading@11 228 quantize_triangular_ns(c, state, dst[ch], src[ch], nb_samples);
yading@11 229 } else {
yading@11 230 c->quantize(dst[ch], src[ch],
yading@11 231 &state->noise_buf[state->noise_buf_ptr],
yading@11 232 FFALIGN(nb_samples, c->samples_align));
yading@11 233 }
yading@11 234
yading@11 235 state->noise_buf_ptr += aligned_samples;
yading@11 236 }
yading@11 237
yading@11 238 return 0;
yading@11 239 }
yading@11 240
yading@11 241 int ff_convert_dither(DitherContext *c, AudioData *dst, AudioData *src)
yading@11 242 {
yading@11 243 int ret;
yading@11 244 AudioData *flt_data;
yading@11 245
yading@11 246 /* output directly to dst if it is planar */
yading@11 247 if (dst->sample_fmt == AV_SAMPLE_FMT_S16P)
yading@11 248 c->s16_data = dst;
yading@11 249 else {
yading@11 250 /* make sure s16_data is large enough for the output */
yading@11 251 ret = ff_audio_data_realloc(c->s16_data, src->nb_samples);
yading@11 252 if (ret < 0)
yading@11 253 return ret;
yading@11 254 }
yading@11 255
yading@11 256 if (src->sample_fmt != AV_SAMPLE_FMT_FLTP || c->apply_map) {
yading@11 257 /* make sure flt_data is large enough for the input */
yading@11 258 ret = ff_audio_data_realloc(c->flt_data, src->nb_samples);
yading@11 259 if (ret < 0)
yading@11 260 return ret;
yading@11 261 flt_data = c->flt_data;
yading@11 262 }
yading@11 263
yading@11 264 if (src->sample_fmt != AV_SAMPLE_FMT_FLTP) {
yading@11 265 /* convert input samples to fltp and scale to s16 range */
yading@11 266 ret = ff_audio_convert(c->ac_in, flt_data, src);
yading@11 267 if (ret < 0)
yading@11 268 return ret;
yading@11 269 } else if (c->apply_map) {
yading@11 270 ret = ff_audio_data_copy(flt_data, src, c->ch_map_info);
yading@11 271 if (ret < 0)
yading@11 272 return ret;
yading@11 273 } else {
yading@11 274 flt_data = src;
yading@11 275 }
yading@11 276
yading@11 277 /* check alignment and padding constraints */
yading@11 278 if (c->method != AV_RESAMPLE_DITHER_TRIANGULAR_NS) {
yading@11 279 int ptr_align = FFMIN(flt_data->ptr_align, c->s16_data->ptr_align);
yading@11 280 int samples_align = FFMIN(flt_data->samples_align, c->s16_data->samples_align);
yading@11 281 int aligned_len = FFALIGN(src->nb_samples, c->ddsp.samples_align);
yading@11 282
yading@11 283 if (!(ptr_align % c->ddsp.ptr_align) && samples_align >= aligned_len) {
yading@11 284 c->quantize = c->ddsp.quantize;
yading@11 285 c->samples_align = c->ddsp.samples_align;
yading@11 286 } else {
yading@11 287 c->quantize = quantize_c;
yading@11 288 c->samples_align = 1;
yading@11 289 }
yading@11 290 }
yading@11 291
yading@11 292 ret = convert_samples(c, (int16_t **)c->s16_data->data,
yading@11 293 (float * const *)flt_data->data, src->channels,
yading@11 294 src->nb_samples);
yading@11 295 if (ret < 0)
yading@11 296 return ret;
yading@11 297
yading@11 298 c->s16_data->nb_samples = src->nb_samples;
yading@11 299
yading@11 300 /* interleave output to dst if needed */
yading@11 301 if (dst->sample_fmt == AV_SAMPLE_FMT_S16) {
yading@11 302 ret = ff_audio_convert(c->ac_out, dst, c->s16_data);
yading@11 303 if (ret < 0)
yading@11 304 return ret;
yading@11 305 } else
yading@11 306 c->s16_data = NULL;
yading@11 307
yading@11 308 return 0;
yading@11 309 }
yading@11 310
yading@11 311 void ff_dither_free(DitherContext **cp)
yading@11 312 {
yading@11 313 DitherContext *c = *cp;
yading@11 314 int ch;
yading@11 315
yading@11 316 if (!c)
yading@11 317 return;
yading@11 318 ff_audio_data_free(&c->flt_data);
yading@11 319 ff_audio_data_free(&c->s16_data);
yading@11 320 ff_audio_convert_free(&c->ac_in);
yading@11 321 ff_audio_convert_free(&c->ac_out);
yading@11 322 for (ch = 0; ch < c->channels; ch++)
yading@11 323 av_free(c->state[ch].noise_buf);
yading@11 324 av_free(c->state);
yading@11 325 av_freep(cp);
yading@11 326 }
yading@11 327
yading@11 328 static void dither_init(DitherDSPContext *ddsp,
yading@11 329 enum AVResampleDitherMethod method)
yading@11 330 {
yading@11 331 ddsp->quantize = quantize_c;
yading@11 332 ddsp->ptr_align = 1;
yading@11 333 ddsp->samples_align = 1;
yading@11 334
yading@11 335 if (method == AV_RESAMPLE_DITHER_RECTANGULAR)
yading@11 336 ddsp->dither_int_to_float = dither_int_to_float_rectangular_c;
yading@11 337 else
yading@11 338 ddsp->dither_int_to_float = dither_int_to_float_triangular_c;
yading@11 339
yading@11 340 if (ARCH_X86)
yading@11 341 ff_dither_init_x86(ddsp, method);
yading@11 342 }
yading@11 343
yading@11 344 DitherContext *ff_dither_alloc(AVAudioResampleContext *avr,
yading@11 345 enum AVSampleFormat out_fmt,
yading@11 346 enum AVSampleFormat in_fmt,
yading@11 347 int channels, int sample_rate, int apply_map)
yading@11 348 {
yading@11 349 AVLFG seed_gen;
yading@11 350 DitherContext *c;
yading@11 351 int ch;
yading@11 352
yading@11 353 if (av_get_packed_sample_fmt(out_fmt) != AV_SAMPLE_FMT_S16 ||
yading@11 354 av_get_bytes_per_sample(in_fmt) <= 2) {
yading@11 355 av_log(avr, AV_LOG_ERROR, "dithering %s to %s is not supported\n",
yading@11 356 av_get_sample_fmt_name(in_fmt), av_get_sample_fmt_name(out_fmt));
yading@11 357 return NULL;
yading@11 358 }
yading@11 359
yading@11 360 c = av_mallocz(sizeof(*c));
yading@11 361 if (!c)
yading@11 362 return NULL;
yading@11 363
yading@11 364 c->apply_map = apply_map;
yading@11 365 if (apply_map)
yading@11 366 c->ch_map_info = &avr->ch_map_info;
yading@11 367
yading@11 368 if (avr->dither_method == AV_RESAMPLE_DITHER_TRIANGULAR_NS &&
yading@11 369 sample_rate != 48000 && sample_rate != 44100) {
yading@11 370 av_log(avr, AV_LOG_WARNING, "sample rate must be 48000 or 44100 Hz "
yading@11 371 "for triangular_ns dither. using triangular_hp instead.\n");
yading@11 372 avr->dither_method = AV_RESAMPLE_DITHER_TRIANGULAR_HP;
yading@11 373 }
yading@11 374 c->method = avr->dither_method;
yading@11 375 dither_init(&c->ddsp, c->method);
yading@11 376
yading@11 377 if (c->method == AV_RESAMPLE_DITHER_TRIANGULAR_NS) {
yading@11 378 if (sample_rate == 48000) {
yading@11 379 c->ns_coef_b = ns_48_coef_b;
yading@11 380 c->ns_coef_a = ns_48_coef_a;
yading@11 381 } else {
yading@11 382 c->ns_coef_b = ns_44_coef_b;
yading@11 383 c->ns_coef_a = ns_44_coef_a;
yading@11 384 }
yading@11 385 }
yading@11 386
yading@11 387 /* Either s16 or s16p output format is allowed, but s16p is used
yading@11 388 internally, so we need to use a temp buffer and interleave if the output
yading@11 389 format is s16 */
yading@11 390 if (out_fmt != AV_SAMPLE_FMT_S16P) {
yading@11 391 c->s16_data = ff_audio_data_alloc(channels, 1024, AV_SAMPLE_FMT_S16P,
yading@11 392 "dither s16 buffer");
yading@11 393 if (!c->s16_data)
yading@11 394 goto fail;
yading@11 395
yading@11 396 c->ac_out = ff_audio_convert_alloc(avr, out_fmt, AV_SAMPLE_FMT_S16P,
yading@11 397 channels, sample_rate, 0);
yading@11 398 if (!c->ac_out)
yading@11 399 goto fail;
yading@11 400 }
yading@11 401
yading@11 402 if (in_fmt != AV_SAMPLE_FMT_FLTP || c->apply_map) {
yading@11 403 c->flt_data = ff_audio_data_alloc(channels, 1024, AV_SAMPLE_FMT_FLTP,
yading@11 404 "dither flt buffer");
yading@11 405 if (!c->flt_data)
yading@11 406 goto fail;
yading@11 407 }
yading@11 408 if (in_fmt != AV_SAMPLE_FMT_FLTP) {
yading@11 409 c->ac_in = ff_audio_convert_alloc(avr, AV_SAMPLE_FMT_FLTP, in_fmt,
yading@11 410 channels, sample_rate, c->apply_map);
yading@11 411 if (!c->ac_in)
yading@11 412 goto fail;
yading@11 413 }
yading@11 414
yading@11 415 c->state = av_mallocz(channels * sizeof(*c->state));
yading@11 416 if (!c->state)
yading@11 417 goto fail;
yading@11 418 c->channels = channels;
yading@11 419
yading@11 420 /* calculate thresholds for turning off dithering during periods of
yading@11 421 silence to avoid replacing digital silence with quiet dither noise */
yading@11 422 c->mute_dither_threshold = lrintf(sample_rate * MUTE_THRESHOLD_SEC);
yading@11 423 c->mute_reset_threshold = c->mute_dither_threshold * 4;
yading@11 424
yading@11 425 /* initialize dither states */
yading@11 426 av_lfg_init(&seed_gen, 0xC0FFEE);
yading@11 427 for (ch = 0; ch < channels; ch++) {
yading@11 428 DitherState *state = &c->state[ch];
yading@11 429 state->mute = c->mute_reset_threshold + 1;
yading@11 430 state->seed = av_lfg_get(&seed_gen);
yading@11 431 generate_dither_noise(c, state, FFMAX(32768, sample_rate / 2));
yading@11 432 }
yading@11 433
yading@11 434 return c;
yading@11 435
yading@11 436 fail:
yading@11 437 ff_dither_free(&c);
yading@11 438 return NULL;
yading@11 439 }