summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Maxwell <greg@xiph.org>2013-11-22 11:32:32 -0800
committerGregory Maxwell <greg@xiph.org>2013-11-22 11:32:32 -0800
commitabf91fe9d019257b536264120e2da69dbc52c4f9 (patch)
treeea3a1062a4d2c60d232262e2587942e05d269b91
parentf6ec71925a3d1b7e783c509bc57b8dc66ca43785 (diff)
downloadopus-abf91fe9d019257b536264120e2da69dbc52c4f9.tar.gz
Minor opus_pcm_soft_clip API hardening and tests.
-rw-r--r--src/opus.c2
-rw-r--r--tests/test_opus_decode.c48
2 files changed, 49 insertions, 1 deletions
diff --git a/src/opus.c b/src/opus.c
index 87fb15ba..30890b9c 100644
--- a/src/opus.c
+++ b/src/opus.c
@@ -39,6 +39,8 @@ OPUS_EXPORT void opus_pcm_soft_clip(float *_x, int N, int C, float *declip_mem)
int i;
float *x;
+ if (C<1 || N<1 || !_x || !declip_mem) return;
+
/* First thing: saturate everything to +/- 2 which is the highest level our
non-linearity can handle. At the point where the signal reaches +/-2,
the derivative will be zero anyway, so this doesn't introduce any
diff --git a/tests/test_opus_decode.c b/tests/test_opus_decode.c
index 44a0ae52..88c6b21c 100644
--- a/tests/test_opus_decode.c
+++ b/tests/test_opus_decode.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011 Xiph.Org Foundation
+/* Copyright (c) 2011-2013 Xiph.Org Foundation
Written by Gregory Maxwell */
/*
Redistribution and use in source and binary forms, with or without
@@ -373,6 +373,49 @@ int test_decoder_code0(int no_fuzz)
return 0;
}
+#ifndef DISABLE_FLOAT_API
+void test_soft_clip(void)
+{
+ int i,j;
+ float x[1024];
+ float s[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+ fprintf(stdout," Testing opus_pcm_soft_clip... ");
+ for(i=0;i<1024;i++)
+ {
+ for (j=0;j<1024;j++)
+ {
+ x[j]=(i&511)*(1/128.f)-2.f;
+ }
+ opus_pcm_soft_clip(&x[i],1024-i,1,s);
+ for (j=i;j<1024;j++)
+ {
+ if(x[i]>1.f)test_failed();
+ if(x[i]<-1.f)test_failed();
+ }
+ }
+ for(i=1;i<9;i++)
+ {
+ for (j=0;j<1024;j++)
+ {
+ x[j]=(i&511)*(1/128.f)-2.f;
+ }
+ opus_pcm_soft_clip(x,1024/i,i,s);
+ for (j=0;j<(1024/i)*i;j++)
+ {
+ if(x[i]>1.f)test_failed();
+ if(x[i]<-1.f)test_failed();
+ }
+ }
+ opus_pcm_soft_clip(x,0,1,s);
+ opus_pcm_soft_clip(x,1,0,s);
+ opus_pcm_soft_clip(x,1,1,0);
+ opus_pcm_soft_clip(x,1,-1,s);
+ opus_pcm_soft_clip(x,-1,1,s);
+ opus_pcm_soft_clip(0,1,1,s);
+ printf("OK.\n");
+}
+#endif
+
int main(int _argc, char **_argv)
{
const char * oversion;
@@ -405,6 +448,9 @@ int main(int _argc, char **_argv)
into the decoders. This is helpful because garbage data
may cause the decoders to clip, which angers CLANG IOC.*/
test_decoder_code0(getenv("TEST_OPUS_NOFUZZ")!=NULL);
+#ifndef DISABLE_FLOAT_API
+ test_soft_clip();
+#endif
return 0;
}