dither.c 5.0 KB

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