dither_template.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 pos = s->dither.ns_pos;
  22. int i, j, ch;
  23. int taps = s->dither.ns_taps;
  24. float S = s->dither.ns_scale;
  25. float S_1 = s->dither.ns_scale_1;
  26. av_assert2((taps&3) != 2);
  27. av_assert2((taps&3) != 3 || s->dither.ns_coeffs[taps] == 0);
  28. for (ch=0; ch<srcs->ch_count; ch++) {
  29. const float *noise = ((const float *)noises->ch[ch]) + s->dither.noise_pos;
  30. const DELEM *src = (const DELEM*)srcs->ch[ch];
  31. DELEM *dst = (DELEM*)dsts->ch[ch];
  32. float *ns_errors = s->dither.ns_errors[ch];
  33. const float *ns_coeffs = s->dither.ns_coeffs;
  34. pos = s->dither.ns_pos;
  35. for (i=0; i<count; i++) {
  36. double d1, d = src[i]*S_1;
  37. for(j=0; j<taps-2; j+=4) {
  38. d -= ns_coeffs[j ] * ns_errors[pos + j ]
  39. +ns_coeffs[j + 1] * ns_errors[pos + j + 1]
  40. +ns_coeffs[j + 2] * ns_errors[pos + j + 2]
  41. +ns_coeffs[j + 3] * ns_errors[pos + j + 3];
  42. }
  43. if(j < taps)
  44. d -= ns_coeffs[j] * ns_errors[pos + j];
  45. pos = pos ? pos - 1 : taps - 1;
  46. d1 = rint(d + noise[i]);
  47. ns_errors[pos + taps] = ns_errors[pos] = d1 - d;
  48. d1 *= S;
  49. CLIP(d1);
  50. dst[i] = d1;
  51. }
  52. }
  53. s->dither.ns_pos = pos;
  54. }
  55. #undef RENAME
  56. #undef DELEM
  57. #undef CLIP