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