dither.c 5.1 KB

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