You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.2KB

  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(void *dst, int len, unsigned seed, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, enum SwrDitherType method) {
  23. double scale = 0;
  24. int i;
  25. if(in_fmt == AV_SAMPLE_FMT_FLT || in_fmt == AV_SAMPLE_FMT_DBL){
  26. if(out_fmt == AV_SAMPLE_FMT_S32) scale = 1.0/(1L<<31);
  27. if(out_fmt == AV_SAMPLE_FMT_S16) scale = 1.0/(1L<<15);
  28. if(out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1.0/(1L<< 7);
  29. }
  30. if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_S16) scale = 1L<<16;
  31. if(in_fmt == AV_SAMPLE_FMT_S32 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<24;
  32. if(in_fmt == AV_SAMPLE_FMT_S16 && out_fmt == AV_SAMPLE_FMT_U8 ) scale = 1L<<8;
  33. for(i=0; i<len; i++){
  34. double v;
  35. seed = seed* 1664525 + 1013904223;
  36. switch(method){
  37. case SWR_DITHER_RECTANGULAR: v= ((double)seed) / UINT_MAX - 0.5; break;
  38. default: av_assert0(0);
  39. }
  40. v*= scale;
  41. switch(in_fmt){
  42. case AV_SAMPLE_FMT_S16: ((int16_t*)dst)[i] = v; break;
  43. case AV_SAMPLE_FMT_S32: ((int32_t*)dst)[i] = v; break;
  44. case AV_SAMPLE_FMT_FLT: ((float *)dst)[i] = v; break;
  45. case AV_SAMPLE_FMT_DBL: ((double *)dst)[i] = v; break;
  46. default: av_assert0(0);
  47. }
  48. }
  49. }