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.

67 lines
2.0KB

  1. #if defined(TEMPLATE_DITHER_DBL)
  2. # define RENAME(N) N ## _double
  3. # define DELEM double
  4. # define CLIP(v)
  5. #elif defined(TEMPLATE_DITHER_FLT)
  6. # define RENAME(N) N ## _float
  7. # define DELEM float
  8. # define CLIP(v)
  9. #elif defined(TEMPLATE_DITHER_S32)
  10. # define RENAME(N) N ## _int32
  11. # define DELEM int32_t
  12. # define CLIP(v) v = FFMAX(FFMIN(v, INT32_MAX), INT32_MIN)
  13. #elif defined(TEMPLATE_DITHER_S16)
  14. # define RENAME(N) N ## _int16
  15. # define DELEM int16_t
  16. # define CLIP(v) v = FFMAX(FFMIN(v, INT16_MAX), INT16_MIN)
  17. #else
  18. ERROR
  19. #endif
  20. void RENAME(swri_noise_shaping)(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count){
  21. int i, j, pos, ch;
  22. int taps = s->dither.ns_taps;
  23. float S = s->dither.ns_scale;
  24. float S_1 = s->dither.ns_scale_1;
  25. av_assert2((taps&3) != 2);
  26. av_assert2((taps&3) != 3 || s->dither.ns_coeffs[taps] == 0);
  27. for (ch=0; ch<srcs->ch_count; ch++) {
  28. const float *noise = ((const float *)noises->ch[ch]) + s->dither.noise_pos;
  29. const DELEM *src = (const DELEM*)srcs->ch[ch];
  30. DELEM *dst = (DELEM*)dsts->ch[ch];
  31. float *ns_errors = s->dither.ns_errors[ch];
  32. const float *ns_coeffs = s->dither.ns_coeffs;
  33. pos = s->dither.ns_pos;
  34. for (i=0; i<count; i++) {
  35. double d1, d = src[i]*S_1;
  36. for(j=0; j<taps-2; j+=4) {
  37. d -= ns_coeffs[j ] * ns_errors[pos + j ]
  38. +ns_coeffs[j + 1] * ns_errors[pos + j + 1]
  39. +ns_coeffs[j + 2] * ns_errors[pos + j + 2]
  40. +ns_coeffs[j + 3] * ns_errors[pos + j + 3];
  41. }
  42. if(j < taps)
  43. d -= ns_coeffs[j] * ns_errors[pos + j];
  44. pos = pos ? pos - 1 : taps - 1;
  45. d1 = rint(d + noise[i]);
  46. ns_errors[pos + taps] = ns_errors[pos] = d1 - d;
  47. d1 *= S;
  48. CLIP(d1);
  49. dst[i] = d1;
  50. }
  51. }
  52. s->dither.ns_pos = pos;
  53. }
  54. #undef RENAME
  55. #undef DELEM
  56. #undef CLIP