2 * Copyright (C) 2012 Michael Niedermayer (michaelni@gmx.at)
4 * This file is part of libswresample
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "swresample_internal.h"
24 void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt) {
27 double *tmp = av_malloc((len + TMP_EXTRA) * sizeof(double));
30 if(in_fmt == AV_SAMPLE_FMT_FLT || in_fmt == AV_SAMPLE_FMT_DBL){
31 if(out_fmt == AV_SAMPLE_FMT_S32) scale = 1.0/(1L<<31);
32 if(out_fmt == AV_SAMPLE_FMT_S16) scale = 1.0/(1L<<15);
33 if(out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1.0/(1L<< 7);
35 if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S16) scale = 1L<<16;
36 if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<24;
37 if(in_fmt == AV_SAMPLE_FMT_S16 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<8;
39 scale *= s->dither_scale;
41 for(i=0; i<len + TMP_EXTRA; i++){
43 seed = seed* 1664525 + 1013904223;
45 switch(s->dither_method){
46 case SWR_DITHER_RECTANGULAR: v= ((double)seed) / UINT_MAX - 0.5; break;
47 case SWR_DITHER_TRIANGULAR :
48 case SWR_DITHER_TRIANGULAR_HIGHPASS :
49 v = ((double)seed) / UINT_MAX;
50 seed = seed*1664525 + 1013904223;
51 v-= ((double)seed) / UINT_MAX;
53 default: av_assert0(0);
61 switch(s->dither_method){
62 case SWR_DITHER_RECTANGULAR:
63 case SWR_DITHER_TRIANGULAR :
66 case SWR_DITHER_TRIANGULAR_HIGHPASS :
67 v = (- tmp[i] + 2*tmp[i+1] - tmp[i+2]) / sqrt(6);
69 default: av_assert0(0);
75 case AV_SAMPLE_FMT_S16: ((int16_t*)dst)[i] = v; break;
76 case AV_SAMPLE_FMT_S32: ((int32_t*)dst)[i] = v; break;
77 case AV_SAMPLE_FMT_FLT: ((float *)dst)[i] = v; break;
78 case AV_SAMPLE_FMT_DBL: ((double *)dst)[i] = v; break;
79 default: av_assert0(0);