shithub: opus

Download patch

ref: abf91fe9d019257b536264120e2da69dbc52c4f9
parent: f6ec71925a3d1b7e783c509bc57b8dc66ca43785
author: Gregory Maxwell <[email protected]>
date: Fri Nov 22 06:32:32 EST 2013

Minor opus_pcm_soft_clip API hardening and tests.

--- a/src/opus.c
+++ b/src/opus.c
@@ -39,6 +39,8 @@
    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
--- 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 @@
    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 @@
      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;
 }