dither.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2012 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. void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt) {
  23. double scale = 0;
  24. #define TMP_EXTRA 2
  25. double *tmp = av_malloc((len + TMP_EXTRA) * sizeof(double));
  26. int i;
  27. out_fmt = av_get_packed_sample_fmt(out_fmt);
  28. in_fmt = av_get_packed_sample_fmt( in_fmt);
  29. if(in_fmt == AV_SAMPLE_FMT_FLT || in_fmt == AV_SAMPLE_FMT_DBL){
  30. if(out_fmt == AV_SAMPLE_FMT_S32) scale = 1.0/(1L<<31);
  31. if(out_fmt == AV_SAMPLE_FMT_S16) scale = 1.0/(1L<<15);
  32. if(out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1.0/(1L<< 7);
  33. }
  34. if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S16) scale = 1L<<16;
  35. if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<24;
  36. if(in_fmt == AV_SAMPLE_FMT_S16 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<8;
  37. scale *= s->dither_scale;
  38. for(i=0; i<len + TMP_EXTRA; i++){
  39. double v;
  40. seed = seed* 1664525 + 1013904223;
  41. switch(s->dither_method){
  42. case SWR_DITHER_RECTANGULAR: v= ((double)seed) / UINT_MAX - 0.5; break;
  43. case SWR_DITHER_TRIANGULAR :
  44. case SWR_DITHER_TRIANGULAR_HIGHPASS :
  45. v = ((double)seed) / UINT_MAX;
  46. seed = seed*1664525 + 1013904223;
  47. v-= ((double)seed) / UINT_MAX;
  48. break;
  49. default: av_assert0(0);
  50. }
  51. tmp[i] = v;
  52. }
  53. for(i=0; i<len; i++){
  54. double v;
  55. switch(s->dither_method){
  56. case SWR_DITHER_RECTANGULAR:
  57. case SWR_DITHER_TRIANGULAR :
  58. v = tmp[i];
  59. break;
  60. case SWR_DITHER_TRIANGULAR_HIGHPASS :
  61. v = (- tmp[i] + 2*tmp[i+1] - tmp[i+2]) / sqrt(6);
  62. break;
  63. default: av_assert0(0);
  64. }
  65. v*= scale;
  66. switch(in_fmt){
  67. case AV_SAMPLE_FMT_S16: ((int16_t*)dst)[i] = v; break;
  68. case AV_SAMPLE_FMT_S32: ((int32_t*)dst)[i] = v; break;
  69. case AV_SAMPLE_FMT_FLT: ((float *)dst)[i] = v; break;
  70. case AV_SAMPLE_FMT_DBL: ((double *)dst)[i] = v; break;
  71. default: av_assert0(0);
  72. }
  73. }
  74. av_free(tmp);
  75. }