diff --git a/firmware/export/dsp-util.h b/firmware/export/dsp-util.h index 76962b527a..b86b2b776e 100644 --- a/firmware/export/dsp-util.h +++ b/firmware/export/dsp-util.h @@ -18,8 +18,8 @@ * KIND, either express or implied. * ****************************************************************************/ -#ifndef DSP_HELPER_H -#define DSP_HELPER_H +#ifndef DSP_UTIL_H +#define DSP_UTIL_H /** Clip sample to signed 16 bit range **/ @@ -48,4 +48,11 @@ static FORCE_INLINE int32_t clip_sample_16(int32_t sample) #undef CLIP_SAMPLE_16_DEFINED -#endif /* DSP_HELPER_H */ +/* Absolute difference of signed 32-bit numbers which must be dealt with + * in the unsigned 32-bit range */ +static FORCE_INLINE uint32_t ad_s32(int32_t a, int32_t b) +{ + return (a >= b) ? (a - b) : (b - a); +} + +#endif /* DSP_UTIL_H */ diff --git a/lib/rbcodec/dsp/tdspeed.c b/lib/rbcodec/dsp/tdspeed.c index 731be12621..c2f4a3f704 100644 --- a/lib/rbcodec/dsp/tdspeed.c +++ b/lib/rbcodec/dsp/tdspeed.c @@ -29,6 +29,7 @@ #include "system.h" #include "tdspeed.h" #include "settings.h" +#include "dsp-util.h" #define assert(cond) @@ -308,8 +309,7 @@ static int tdspeed_apply(int32_t *buf_out[2], int32_t *buf_in[2], for (int j = 0; j < st->dst_step; j += INC2, curr += INC2, prev += INC2) { - int32_t diff = *curr - *prev; - delta += abs(diff); + delta += ad_s32(*curr, *prev); if (delta >= min_delta) goto skip; @@ -322,8 +322,7 @@ static int tdspeed_apply(int32_t *buf_out[2], int32_t *buf_in[2], for (int j = 0; j < st->dst_step; j += INC2, curr += INC2, prev += INC2) { - int32_t diff = *curr - *prev; - delta += abs(diff); + delta += ad_s32(*curr, *prev); if (delta >= min_delta) goto skip;