]> git.sesse.net Git - ffmpeg/blobdiff - libswresample/dither.c
avcodec_decode_audio: do not trust the channel layout, use the channel count.
[ffmpeg] / libswresample / dither.c
index d7cb8a134a0df84cfed41465fe8fe44eb1e831c5..d70505c27ef4b8498276543bbe0a697eee9cbe66 100644 (file)
@@ -24,7 +24,7 @@
 #include "noise_shaping_data.c"
 
 void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt) {
-    double scale = s->dither.ns_scale;
+    double scale = s->dither.noise_scale;
 #define TMP_EXTRA 2
     double *tmp = av_malloc((len + TMP_EXTRA) * sizeof(double));
     int i;
@@ -95,8 +95,9 @@ int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFo
     scale *= s->dither.scale;
 
     s->dither.ns_pos = 0;
+    s->dither.noise_scale=   scale;
     s->dither.ns_scale   =   scale;
-    s->dither.ns_scale_1 = 1/scale;
+    s->dither.ns_scale_1 = scale ? 1/scale : 0;
     memset(s->dither.ns_errors, 0, sizeof(s->dither.ns_errors));
     for (i=0; filters[i].coefs; i++) {
         const filter_t *f = &filters[i];
@@ -105,6 +106,7 @@ int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFo
             s->dither.ns_taps = f->len;
             for (j=0; j<f->len; j++)
                 s->dither.ns_coeffs[j] = f->coefs[j];
+            s->dither.ns_scale_1 *= 1 - exp(f->gain_cB * M_LN10 * 0.005) * 2 / (1<<(8*av_get_bytes_per_sample(out_fmt)));
             break;
         }
     }
@@ -113,10 +115,13 @@ int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFo
         s->dither.method = SWR_DITHER_TRIANGULAR_HIGHPASS;
     }
 
+    av_assert0(!s->preout.count);
     s->dither.noise = s->preout;
+    s->dither.temp  = s->preout;
     if (s->dither.method > SWR_DITHER_NS) {
         s->dither.noise.bps = 4;
         s->dither.noise.fmt = AV_SAMPLE_FMT_FLTP;
+        s->dither.noise_scale = 1;
     }
 
     return 0;