summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2012-04-26 15:21:43 -0400
committerMichael Sevakis <jethead71@rockbox.org>2012-04-26 16:04:43 -0400
commite5c3327cefb4e6f3a9089ad30fd12bb8f40a0658 (patch)
tree973027f2688c478bf0d30ce1ead0bfc24dae766b
parent2866063c3cff10814f0e2e6c5263c7ee6d4c7fa1 (diff)
downloadrockbox-e5c3327cefb4e6f3a9089ad30fd12bb8f40a0658.tar.gz
rockbox-e5c3327cefb4e6f3a9089ad30fd12bb8f40a0658.zip
Add a more correct absolute difference function to dsp-util.
Differences between signed samples cover the entire unsigned 32-bit range. "abs" will think any difference exceeding INT32_MAX is negative which is not corrent. Test which argument is greater and subtract the lesser from it, outputting unsigned difference. Change-Id: I73a8e5e418d49ff73d1a7c98eeb4731946dcfe84
-rw-r--r--firmware/export/dsp-util.h13
-rw-r--r--lib/rbcodec/dsp/tdspeed.c7
2 files changed, 13 insertions, 7 deletions
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 @@
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21#ifndef DSP_HELPER_H 21#ifndef DSP_UTIL_H
22#define DSP_HELPER_H 22#define DSP_UTIL_H
23 23
24/** Clip sample to signed 16 bit range **/ 24/** Clip sample to signed 16 bit range **/
25 25
@@ -48,4 +48,11 @@ static FORCE_INLINE int32_t clip_sample_16(int32_t sample)
48 48
49#undef CLIP_SAMPLE_16_DEFINED 49#undef CLIP_SAMPLE_16_DEFINED
50 50
51#endif /* DSP_HELPER_H */ 51/* Absolute difference of signed 32-bit numbers which must be dealt with
52 * in the unsigned 32-bit range */
53static FORCE_INLINE uint32_t ad_s32(int32_t a, int32_t b)
54{
55 return (a >= b) ? (a - b) : (b - a);
56}
57
58#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 @@
29#include "system.h" 29#include "system.h"
30#include "tdspeed.h" 30#include "tdspeed.h"
31#include "settings.h" 31#include "settings.h"
32#include "dsp-util.h"
32 33
33#define assert(cond) 34#define assert(cond)
34 35
@@ -308,8 +309,7 @@ static int tdspeed_apply(int32_t *buf_out[2], int32_t *buf_in[2],
308 309
309 for (int j = 0; j < st->dst_step; j += INC2, curr += INC2, prev += INC2) 310 for (int j = 0; j < st->dst_step; j += INC2, curr += INC2, prev += INC2)
310 { 311 {
311 int32_t diff = *curr - *prev; 312 delta += ad_s32(*curr, *prev);
312 delta += abs(diff);
313 313
314 if (delta >= min_delta) 314 if (delta >= min_delta)
315 goto skip; 315 goto skip;
@@ -322,8 +322,7 @@ static int tdspeed_apply(int32_t *buf_out[2], int32_t *buf_in[2],
322 322
323 for (int j = 0; j < st->dst_step; j += INC2, curr += INC2, prev += INC2) 323 for (int j = 0; j < st->dst_step; j += INC2, curr += INC2, prev += INC2)
324 { 324 {
325 int32_t diff = *curr - *prev; 325 delta += ad_s32(*curr, *prev);
326 delta += abs(diff);
327 326
328 if (delta >= min_delta) 327 if (delta >= min_delta)
329 goto skip; 328 goto skip;