From abf91fe9d019257b536264120e2da69dbc52c4f9 Mon Sep 17 00:00:00 2001 From: Gregory Maxwell <greg@xiph.org> Date: Fri, 22 Nov 2013 11:32:32 -0800 Subject: [PATCH] Minor opus_pcm_soft_clip API hardening and tests. --- src/opus.c | 2 ++ tests/test_opus_decode.c | 48 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/opus.c b/src/opus.c index 87fb15ba2..30890b9cb 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 44a0ae524..88c6b21c9 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; } -- GitLab