LCOV - code coverage report
Current view: top level - libswresample - dither.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 71 40.8 %
Date: 2017-10-24 00:14:43 Functions: 1 2 50.0 %

          Line data    Source code
       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           0 : int swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt) {
      27           0 :     double scale = s->dither.noise_scale;
      28             : #define TMP_EXTRA 2
      29           0 :     double *tmp = av_malloc_array(len + TMP_EXTRA, sizeof(double));
      30             :     int i;
      31             : 
      32           0 :     if (!tmp)
      33           0 :         return AVERROR(ENOMEM);
      34             : 
      35           0 :     for(i=0; i<len + TMP_EXTRA; i++){
      36             :         double v;
      37           0 :         seed = seed* 1664525 + 1013904223;
      38             : 
      39           0 :         switch(s->dither.method){
      40           0 :             case SWR_DITHER_RECTANGULAR: v= ((double)seed) / UINT_MAX - 0.5; break;
      41           0 :             default:
      42           0 :                 av_assert0(s->dither.method < SWR_DITHER_NB);
      43           0 :                 v = ((double)seed) / UINT_MAX;
      44           0 :                 seed = seed*1664525 + 1013904223;
      45           0 :                 v-= ((double)seed) / UINT_MAX;
      46           0 :                 break;
      47             :         }
      48           0 :         tmp[i] = v;
      49             :     }
      50             : 
      51           0 :     for(i=0; i<len; i++){
      52             :         double v;
      53             : 
      54           0 :         switch(s->dither.method){
      55           0 :             default:
      56           0 :                 av_assert0(s->dither.method < SWR_DITHER_NB);
      57           0 :                 v = tmp[i];
      58           0 :                 break;
      59           0 :             case SWR_DITHER_TRIANGULAR_HIGHPASS :
      60           0 :                 v = (- tmp[i] + 2*tmp[i+1] - tmp[i+2]) / sqrt(6);
      61           0 :                 break;
      62             :         }
      63             : 
      64           0 :         v*= scale;
      65             : 
      66           0 :         switch(noise_fmt){
      67           0 :             case AV_SAMPLE_FMT_S16P: ((int16_t*)dst)[i] = v; break;
      68           0 :             case AV_SAMPLE_FMT_S32P: ((int32_t*)dst)[i] = v; break;
      69           0 :             case AV_SAMPLE_FMT_FLTP: ((float  *)dst)[i] = v; break;
      70           0 :             case AV_SAMPLE_FMT_DBLP: ((double *)dst)[i] = v; break;
      71           0 :             default: av_assert0(0);
      72             :         }
      73             :     }
      74             : 
      75           0 :     av_free(tmp);
      76           0 :     return 0;
      77             : }
      78             : 
      79        1191 : av_cold int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt)
      80             : {
      81             :     int i;
      82        1191 :     double scale = 0;
      83             : 
      84        1191 :     if (s->dither.method > SWR_DITHER_TRIANGULAR_HIGHPASS && s->dither.method <= SWR_DITHER_NS)
      85           0 :         return AVERROR(EINVAL);
      86             : 
      87        1191 :     out_fmt = av_get_packed_sample_fmt(out_fmt);
      88        1191 :     in_fmt  = av_get_packed_sample_fmt( in_fmt);
      89             : 
      90        1191 :     if(in_fmt == AV_SAMPLE_FMT_FLT || in_fmt == AV_SAMPLE_FMT_DBL){
      91         611 :         if(out_fmt == AV_SAMPLE_FMT_S32) scale = 1.0/(1LL<<31);
      92         611 :         if(out_fmt == AV_SAMPLE_FMT_S16) scale = 1.0/(1LL<<15);
      93         611 :         if(out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1.0/(1LL<< 7);
      94             :     }
      95        1191 :     if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S32 && (s->dither.output_sample_bits&31)) scale = 1;
      96        1191 :     if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S16) scale = 1<<16;
      97        1191 :     if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1<<24;
      98        1191 :     if(in_fmt == AV_SAMPLE_FMT_S16 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1<<8;
      99             : 
     100        1191 :     scale *= s->dither.scale;
     101             : 
     102        1191 :     if (out_fmt == AV_SAMPLE_FMT_S32 && s->dither.output_sample_bits)
     103          36 :         scale *= 1<<(32-s->dither.output_sample_bits);
     104             : 
     105        1191 :     if (scale == 0) {
     106         686 :         s->dither.method = 0;
     107         686 :         return 0;
     108             :     }
     109             : 
     110         505 :     s->dither.ns_pos = 0;
     111         505 :     s->dither.noise_scale=   scale;
     112         505 :     s->dither.ns_scale   =   scale;
     113         505 :     s->dither.ns_scale_1 = scale ? 1/scale : 0;
     114         505 :     memset(s->dither.ns_errors, 0, sizeof(s->dither.ns_errors));
     115        8080 :     for (i=0; filters[i].coefs; i++) {
     116        7575 :         const filter_t *f = &filters[i];
     117        7575 :         if (llabs(s->out_sample_rate - f->rate)*20 <= f->rate && f->name == s->dither.method) {
     118             :             int j;
     119           0 :             s->dither.ns_taps = f->len;
     120           0 :             for (j=0; j<f->len; j++)
     121           0 :                 s->dither.ns_coeffs[j] = f->coefs[j];
     122           0 :             s->dither.ns_scale_1 *= 1 - exp(f->gain_cB * M_LN10 * 0.005) * 2 / (1<<(8*av_get_bytes_per_sample(out_fmt)));
     123           0 :             break;
     124             :         }
     125             :     }
     126         505 :     if (!filters[i].coefs && s->dither.method > SWR_DITHER_NS) {
     127           0 :         av_log(s, AV_LOG_WARNING, "Requested noise shaping dither not available at this sampling rate, using triangular hp dither\n");
     128           0 :         s->dither.method = SWR_DITHER_TRIANGULAR_HIGHPASS;
     129             :     }
     130             : 
     131         505 :     return 0;
     132             : }
     133             : 
     134             : #define TEMPLATE_DITHER_S16
     135             : #include "dither_template.c"
     136             : #undef TEMPLATE_DITHER_S16
     137             : 
     138             : #define TEMPLATE_DITHER_S32
     139             : #include "dither_template.c"
     140             : #undef TEMPLATE_DITHER_S32
     141             : 
     142             : #define TEMPLATE_DITHER_FLT
     143             : #include "dither_template.c"
     144             : #undef TEMPLATE_DITHER_FLT
     145             : 
     146             : #define TEMPLATE_DITHER_DBL
     147             : #include "dither_template.c"
     148             : #undef TEMPLATE_DITHER_DBL

Generated by: LCOV version 1.13