aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2005-10-28 21:35:55 +0000
committerkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2005-10-28 21:35:55 +0000
commitdc1aa1ffdc495cfff0c483ac6056bc8b203ee3ff (patch)
tree516b6bc501ba5df920323c0dfd62cd355698de65
parent81880bfe3daa5f51a4083e3185cd72a30d2e6543 (diff)
ensure that SLINEAR volume adjustments don't wrap around short integer maximums
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@6882 f38db490-d61c-443f-a65b-d21fe96a405b
-rwxr-xr-xframe.c4
-rwxr-xr-xinclude/asterisk/utils.h30
2 files changed, 32 insertions, 2 deletions
diff --git a/frame.c b/frame.c
index 7b4fa4ddf..39bed2e01 100755
--- a/frame.c
+++ b/frame.c
@@ -1263,9 +1263,9 @@ int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
for (count = 0; count < f->samples; count++) {
if (adjustment > 0) {
- fdata[count] *= abs(adjustment);
+ ast_slinear_saturated_multiply(&fdata[count], abs(adjustment));
} else if (adjustment < 0) {
- fdata[count] /= abs(adjustment);
+ ast_slinear_saturated_divide(&fdata[count], abs(adjustment));
}
}
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index e0c32597d..9aa1a34c3 100755
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -168,7 +168,37 @@ char *ast_uri_encode(char *string, char *outbuf, int buflen, int doreserved);
\param s String to be decoded
*/
void ast_uri_decode(char *s);
+
+static inline void ast_slinear_saturated_add(short *input, short value)
+{
+ int res;
+
+ res = *input + value;
+ if (res > 32767)
+ *input = 32767;
+ else if (res < -32767)
+ *input = -32767;
+ else
+ *input = (short) res;
+}
+static inline void ast_slinear_saturated_multiply(short *input, short value)
+{
+ int res;
+
+ res = *input * value;
+ if (res > 32767)
+ *input = 32767;
+ else if (res < -32767)
+ *input = -32767;
+ else
+ *input = (short) res;
+}
+
+static inline void ast_slinear_saturated_divide(short *input, short value)
+{
+ *input /= value;
+}
extern int test_for_thread_safety(void);