ref: 9652f81eb9b03f744f4cbfeead696613ae973ead
parent: da025d5632be80dad0af54af70d2cb0a286802d8
author: Gregory Maxwell <[email protected]>
date: Wed Oct 26 19:55:33 EDT 2011
Test cleanups and renaming. Build tests in the tests/ directory instead of top, cleanup, rename, and eliminate recursive make for the celt/ unit tests.
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,7 +2,7 @@
lib_LTLIBRARIES = libopus.la
-SUBDIRS = . celt/tests doc
+SUBDIRS = . doc
INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/celt -I$(top_srcdir)/silk -I$(top_srcdir)/silk/float -I$(top_srcdir)/silk/fixed
@@ -26,9 +26,9 @@
noinst_HEADERS = $(OPUS_HEAD) $(SILK_HEAD) $(CELT_HEAD)
-noinst_PROGRAMS = opus_demo repacketizer_demo opus_compare test_opus_api test_opus_encode test_opus_decode
+noinst_PROGRAMS = opus_demo repacketizer_demo opus_compare tests/test_opus_api tests/test_opus_encode tests/test_opus_decode celt/tests/test_unit_cwrs32 celt/tests/test_unit_dft celt/tests/test_unit_entropy celt/tests/test_unit_laplace celt/tests/test_unit_mathops celt/tests/test_unit_mdct celt/tests/test_unit_rotation celt/tests/test_unit_types
-TESTS = test_opus_api test_opus_decode test_opus_encode
+TESTS = celt/tests/test_unit_types celt/tests/test_unit_mathops celt/tests/test_unit_entropy celt/tests/test_unit_laplace celt/tests/test_unit_dft celt/tests/test_unit_mdct celt/tests/test_unit_rotation celt/tests/test_unit_cwrs32 tests/test_opus_api tests/test_opus_decode tests/test_opus_encode
opus_demo_SOURCES = src/opus_demo.c
@@ -41,14 +41,38 @@
opus_compare_SOURCES = src/opus_compare.c
opus_compare_LDADD = -lm
-test_opus_api_SOURCES = tests/test_opus_api.c tests/test_opus_common.h
-test_opus_api_LDADD = libopus.la -lm
+tests_test_opus_api_SOURCES = tests/test_opus_api.c tests/test_opus_common.h
+tests_test_opus_api_LDADD = libopus.la -lm
-test_opus_encode_SOURCES = tests/test_opus_encode.c tests/test_opus_common.h
-test_opus_encode_LDADD = libopus.la -lm
+tests_test_opus_encode_SOURCES = tests/test_opus_encode.c tests/test_opus_common.h
+tests_test_opus_encode_LDADD = libopus.la -lm
-test_opus_decode_SOURCES = tests/test_opus_decode.c tests/test_opus_common.h
-test_opus_decode_LDADD = libopus.la -lm
+tests_test_opus_decode_SOURCES = tests/test_opus_decode.c tests/test_opus_common.h
+tests_test_opus_decode_LDADD = libopus.la -lm
+
+celt_tests_test_unit_cwrs32_SOURCES = celt/tests/test_unit_cwrs32.c
+celt_tests_test_unit_cwrs32_LDADD = -lm
+
+celt_tests_test_unit_dft_SOURCES = celt/tests/test_unit_dft.c
+celt_tests_test_unit_dft_LDADD = -lm
+
+celt_tests_test_unit_entropy_SOURCES = celt/tests/test_unit_entropy.c
+celt_tests_test_unit_entropy_LDADD = -lm
+
+celt_tests_test_unit_laplace_SOURCES = celt/tests/test_unit_laplace.c
+celt_tests_test_unit_laplace_LDADD = -lm
+
+celt_tests_test_unit_mathops_SOURCES = celt/tests/test_unit_mathops.c
+celt_tests_test_unit_mathops_LDADD = -lm
+
+celt_tests_test_unit_mdct_SOURCES = celt/tests/test_unit_mdct.c
+celt_tests_test_unit_mdct_LDADD = -lm
+
+celt_tests_test_unit_rotation_SOURCES = celt/tests/test_unit_rotation.c
+celt_tests_test_unit_rotation_LDADD = -lm
+
+celt_tests_test_unit_types_SOURCES = celt/tests/test_unit_types.c
+celt_tests_test_unit_types_LDADD = -lm
if CUSTOM_MODES
pkginclude_HEADERS += include/opus_custom.h
--- a/celt/tests/Makefile.am
+++ /dev/null
@@ -1,15 +1,0 @@
-INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/celt
-METASOURCES = AUTO
-
-TESTS = type-test ectest cwrs32-test dft-test laplace-test mdct-test mathops-test rotation-test
-
-noinst_PROGRAMS = type-test ectest cwrs32-test dft-test laplace-test mdct-test mathops-test rotation-test
-
-type_test_SOURCES = type-test.c
-ectest_SOURCES = ectest.c
-cwrs32_test_SOURCES = cwrs32-test.c
-dft_test_SOURCES = dft-test.c
-laplace_test_SOURCES = laplace-test.c
-mdct_test_SOURCES = mdct-test.c
-mathops_test_SOURCES = mathops-test.c
-rotation_test_SOURCES = rotation-test.c
--- a/celt/tests/cwrs32-test.c
+++ /dev/null
@@ -1,195 +1,0 @@
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <stdio.h>
-#include <string.h>
-
-#ifndef CUSTOM_MODES
-#define CUSTOM_MODES
-#else
-#define TEST_CUSTOM_MODES
-#endif
-
-#define CELT_C
-#include "../celt/stack_alloc.h"
-#include "../celt/entenc.c"
-#include "../celt/entdec.c"
-#include "../celt/entcode.c"
-#include "../celt/cwrs.c"
-#include "../celt/mathops.c"
-#include "../celt/rate.h"
-
-#define NMAX (240)
-#define KMAX (128)
-
-#ifdef TEST_CUSTOM_MODES
-
-#define NDIMS (46)
-static const int pn[NDIMS]={
- 2, 3, 4, 5, 6, 7, 8, 9, 10,
- 11, 12, 13, 14, 15, 16, 18, 20, 22,
- 24, 26, 28, 30, 32, 36, 40, 44, 48,
- 52, 56, 60, 64, 72, 80, 88, 96, 104,
- 112, 120, 128, 144, 160, 176, 192, 208, 224,
- 240
-};
-static const int pkmax[NDIMS]={
- 128, 128, 128, 128, 88, 52, 36, 26, 22,
- 18, 16, 15, 13, 12, 12, 11, 10, 9,
- 9, 8, 8, 7, 7, 7, 7, 6, 6,
- 6, 6, 6, 5, 5, 5, 5, 5, 5,
- 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4
-};
-
-#else /* TEST_CUSTOM_MODES */
-
-#define NDIMS (22)
-static const int pn[NDIMS]={
- 2, 3, 4, 6, 8, 9, 11, 12, 16,
- 18, 22, 24, 32, 36, 44, 48, 64, 72,
- 88, 96, 144, 176
-};
-static const int pkmax[NDIMS]={
- 128, 128, 128, 88, 36, 26, 18, 16, 12,
- 11, 9, 9, 7, 7, 6, 6, 5, 5,
- 5, 5, 4, 4
-};
-
-#endif
-
-int main(void){
- int t;
- int n;
- ALLOC_STACK;
- for(t=0;t<NDIMS;t++){
- int pseudo;
- n=pn[t];
- for(pseudo=1;pseudo<41;pseudo++)
- {
- int k;
- opus_uint32 uu[KMAX+2U];
- opus_uint32 inc;
- opus_uint32 nc;
- opus_uint32 i;
- k=get_pulses(pseudo);
- if (k>pkmax[t])break;
- printf("Testing CWRS with N=%i, K=%i...\n",n,k);
- nc=ncwrs_urow(n,k,uu);
- inc=nc/20000;
- if(inc<1)inc=1;
- for(i=0;i<nc;i+=inc){
- opus_uint32 u[KMAX+2U];
- int y[NMAX];
- int sy;
- int yy[5];
- opus_uint32 v;
- opus_uint32 ii;
- int kk;
- int j;
- memcpy(u,uu,(k+2U)*sizeof(*u));
- cwrsi(n,k,i,y,u);
- sy=0;
- for(j=0;j<n;j++)sy+=ABS(y[j]);
- if(sy!=k){
- fprintf(stderr,"N=%d Pulse count mismatch in cwrsi (%d!=%d).\n",
- n,sy,k);
- return 99;
- }
- /*printf("%6u of %u:",i,nc);
- for(j=0;j<n;j++)printf(" %+3i",y[j]);
- printf(" ->");*/
- ii=icwrs(n,k,&v,y,u);
- if(ii!=i){
- fprintf(stderr,"Combination-index mismatch (%lu!=%lu).\n",
- (long)ii,(long)i);
- return 1;
- }
- if(v!=nc){
- fprintf(stderr,"Combination count mismatch (%lu!=%lu).\n",
- (long)v,(long)nc);
- return 2;
- }
-#ifndef SMALL_FOOTPRINT
- if(n==2){
- cwrsi2(k,i,yy);
- for(j=0;j<2;j++)if(yy[j]!=y[j]){
- fprintf(stderr,"N=2 pulse vector mismatch ({%i,%i}!={%i,%i}).\n",
- yy[0],yy[1],y[0],y[1]);
- return 3;
- }
- ii=icwrs2(yy,&kk);
- if(ii!=i){
- fprintf(stderr,"N=2 combination-index mismatch (%lu!=%lu).\n",
- (long)ii,(long)i);
- return 4;
- }
- if(kk!=k){
- fprintf(stderr,"N=2 pulse count mismatch (%i,%i).\n",kk,k);
- return 5;
- }
- v=ncwrs2(k);
- if(v!=nc){
- fprintf(stderr,"N=2 combination count mismatch (%lu,%lu).\n",
- (long)v,(long)nc);
- return 6;
- }
- }
- else if(n==3){
- cwrsi3(k,i,yy);
- for(j=0;j<3;j++)if(yy[j]!=y[j]){
- fprintf(stderr,"N=3 pulse vector mismatch "
- "({%i,%i,%i}!={%i,%i,%i}).\n",yy[0],yy[1],yy[2],y[0],y[1],y[2]);
- return 7;
- }
- ii=icwrs3(yy,&kk);
- if(ii!=i){
- fprintf(stderr,"N=3 combination-index mismatch (%lu!=%lu).\n",
- (long)ii,(long)i);
- return 8;
- }
- if(kk!=k){
- fprintf(stderr,"N=3 pulse count mismatch (%i!=%i).\n",kk,k);
- return 9;
- }
- v=ncwrs3(k);
- if(v!=nc){
- fprintf(stderr,"N=3 combination count mismatch (%lu!=%lu).\n",
- (long)v,(long)nc);
- return 10;
- }
- }
- else if(n==4){
- cwrsi4(k,i,yy);
- for(j=0;j<4;j++)if(yy[j]!=y[j]){
- fprintf(stderr,"N=4 pulse vector mismatch "
- "({%i,%i,%i,%i}!={%i,%i,%i,%i}.\n",
- yy[0],yy[1],yy[2],yy[3],y[0],y[1],y[2],y[3]);
- return 11;
- }
- ii=icwrs4(yy,&kk);
- if(ii!=i){
- fprintf(stderr,"N=4 combination-index mismatch (%lu!=%lu).\n",
- (long)ii,(long)i);
- return 12;
- }
- if(kk!=k){
- fprintf(stderr,"N=4 pulse count mismatch (%i!=%i).\n",kk,k);
- return 13;
- }
- v=ncwrs4(k);
- if(v!=nc){
- fprintf(stderr,"N=4 combination count mismatch (%lu!=%lu).\n",
- (long)v,(long)nc);
- return 14;
- }
- }
-#endif /* SMALL_FOOTPRINT */
- /*printf(" %6u\n",i);*/
- }
- /*printf("\n");*/
- }
- }
- return 0;
-}
--- a/celt/tests/dft-test.c
+++ /dev/null
@@ -1,140 +1,0 @@
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#define SKIP_CONFIG_H
-
-#ifndef CUSTOM_MODES
-#define CUSTOM_MODES
-#endif
-
-#include <stdio.h>
-#include "kiss_fft.h"
-
-#define CELT_C
-#include "../celt/stack_alloc.h"
-#include "../celt/kiss_fft.c"
-#include "../celt/mathops.c"
-#include "../celt/entcode.c"
-
-
-#ifndef M_PI
-#define M_PI 3.141592653
-#endif
-
-#ifdef FIXED_DEBUG
-long long celt_mips=0;
-#endif
-int ret = 0;
-
-void check(kiss_fft_cpx * in,kiss_fft_cpx * out,int nfft,int isinverse)
-{
- int bin,k;
- double errpow=0,sigpow=0, snr;
-
- for (bin=0;bin<nfft;++bin) {
- double ansr = 0;
- double ansi = 0;
- double difr;
- double difi;
-
- for (k=0;k<nfft;++k) {
- double phase = -2*M_PI*bin*k/nfft;
- double re = cos(phase);
- double im = sin(phase);
- if (isinverse)
- im = -im;
-
- if (!isinverse)
- {
- re /= nfft;
- im /= nfft;
- }
-
- ansr += in[k].r * re - in[k].i * im;
- ansi += in[k].r * im + in[k].i * re;
- }
- /*printf ("%d %d ", (int)ansr, (int)ansi);*/
- difr = ansr - out[bin].r;
- difi = ansi - out[bin].i;
- errpow += difr*difr + difi*difi;
- sigpow += ansr*ansr+ansi*ansi;
- }
- snr = 10*log10(sigpow/errpow);
- printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
- if (snr<60) {
- printf( "** poor snr: %f ** \n", snr);
- ret = 1;
- }
-}
-
-void test1d(int nfft,int isinverse)
-{
- size_t buflen = sizeof(kiss_fft_cpx)*nfft;
-
- kiss_fft_cpx * in = (kiss_fft_cpx*)malloc(buflen);
- kiss_fft_cpx * out= (kiss_fft_cpx*)malloc(buflen);
- kiss_fft_state *cfg = opus_fft_alloc(nfft,0,0);
- int k;
-
- for (k=0;k<nfft;++k) {
- in[k].r = (rand() % 32767) - 16384;
- in[k].i = (rand() % 32767) - 16384;
- }
-
- for (k=0;k<nfft;++k) {
- in[k].r *= 32768;
- in[k].i *= 32768;
- }
-
- if (isinverse)
- {
- for (k=0;k<nfft;++k) {
- in[k].r /= nfft;
- in[k].i /= nfft;
- }
- }
-
- /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/
-
- if (isinverse)
- opus_ifft(cfg,in,out);
- else
- opus_fft(cfg,in,out);
-
- /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/
-
- check(in,out,nfft,isinverse);
-
- free(in);
- free(out);
- free(cfg);
-}
-
-int main(int argc,char ** argv)
-{
- ALLOC_STACK;
- if (argc>1) {
- int k;
- for (k=1;k<argc;++k) {
- test1d(atoi(argv[k]),0);
- test1d(atoi(argv[k]),1);
- }
- }else{
- test1d(32,0);
- test1d(32,1);
- test1d(128,0);
- test1d(128,1);
- test1d(256,0);
- test1d(256,1);
-#ifndef RADIX_TWO_ONLY
- test1d(36,0);
- test1d(36,1);
- test1d(50,0);
- test1d(50,1);
- test1d(120,0);
- test1d(120,1);
-#endif
- }
- return ret;
-}
--- a/celt/tests/ectest.c
+++ /dev/null
@@ -1,295 +1,0 @@
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <math.h>
-#include <time.h>
-#include "entcode.h"
-#include "entenc.h"
-#include "entdec.h"
-#include <string.h>
-
-#include "../celt/entenc.c"
-#include "../celt/entdec.c"
-#include "../celt/entcode.c"
-
-#ifndef M_LOG2E
-# define M_LOG2E 1.4426950408889634074
-#endif
-#define DATA_SIZE 10000000
-#define DATA_SIZE2 10000
-
-int main(int _argc,char **_argv){
- ec_enc enc;
- ec_dec dec;
- long nbits;
- long nbits2;
- double entropy;
- int ft;
- int ftb;
- int sz;
- int i;
- int ret;
- unsigned int sym;
- unsigned int seed;
- unsigned char *ptr;
- ret=0;
- entropy=0;
- if (_argc > 2) {
- fprintf(stderr, "Usage: %s [<seed>]\n", _argv[0]);
- return 1;
- }
- if (_argc > 1)
- seed = atoi(_argv[1]);
- else
- seed = time(NULL);
- /*Testing encoding of raw bit values.*/
- ptr = (unsigned char *)malloc(DATA_SIZE);
- ec_enc_init(&enc,ptr, DATA_SIZE);
- for(ft=2;ft<1024;ft++){
- for(i=0;i<ft;i++){
- entropy+=log(ft)*M_LOG2E;
- ec_enc_uint(&enc,i,ft);
- }
- }
- /*Testing encoding of raw bit values.*/
- for(ftb=1;ftb<16;ftb++){
- for(i=0;i<(1<<ftb);i++){
- entropy+=ftb;
- nbits=ec_tell(&enc);
- ec_enc_bits(&enc,i,ftb);
- nbits2=ec_tell(&enc);
- if(nbits2-nbits!=ftb){
- fprintf(stderr,"Used %li bits to encode %i bits directly.\n",
- nbits2-nbits,ftb);
- ret=-1;
- }
- }
- }
- nbits=ec_tell_frac(&enc);
- ec_enc_done(&enc);
- fprintf(stderr,
- "Encoded %0.2lf bits of entropy to %0.2lf bits (%0.3lf%% wasted).\n",
- entropy,ldexp(nbits,-3),100*(nbits-ldexp(entropy,3))/nbits);
- fprintf(stderr,"Packed to %li bytes.\n",(long)ec_range_bytes(&enc));
- ec_dec_init(&dec,ptr,DATA_SIZE);
- for(ft=2;ft<1024;ft++){
- for(i=0;i<ft;i++){
- sym=ec_dec_uint(&dec,ft);
- if(sym!=(unsigned)i){
- fprintf(stderr,"Decoded %i instead of %i with ft of %i.\n",sym,i,ft);
- ret=-1;
- }
- }
- }
- for(ftb=1;ftb<16;ftb++){
- for(i=0;i<(1<<ftb);i++){
- sym=ec_dec_bits(&dec,ftb);
- if(sym!=(unsigned)i){
- fprintf(stderr,"Decoded %i instead of %i with ftb of %i.\n",sym,i,ftb);
- ret=-1;
- }
- }
- }
- nbits2=ec_tell_frac(&dec);
- if(nbits!=nbits2){
- fprintf(stderr,
- "Reported number of bits used was %0.2lf, should be %0.2lf.\n",
- ldexp(nbits2,-3),ldexp(nbits,-3));
- ret=-1;
- }
- /*Testing an encoder bust prefers range coder data over raw bits.
- This isn't a general guarantee, will only work for data that is buffered in
- the encoder state and not yet stored in the user buffer, and should never
- get used in practice.
- It's mostly here for code coverage completeness.*/
- /*Start with a 16-bit buffer.*/
- ec_enc_init(&enc,ptr,2);
- /*Write 7 raw bits.*/
- ec_enc_bits(&enc,0x55,7);
- /*Write 12.3 bits of range coder data.*/
- ec_enc_uint(&enc,1,2);
- ec_enc_uint(&enc,1,3);
- ec_enc_uint(&enc,1,4);
- ec_enc_uint(&enc,1,5);
- ec_enc_uint(&enc,2,6);
- ec_enc_uint(&enc,6,7);
- ec_enc_done(&enc);
- ec_dec_init(&dec,ptr,2);
- if(!enc.error
- /*The raw bits should have been overwritten by the range coder data.*/
- ||ec_dec_bits(&dec,7)!=0x05
- /*And all the range coder data should have been encoded correctly.*/
- ||ec_dec_uint(&dec,2)!=1
- ||ec_dec_uint(&dec,3)!=1
- ||ec_dec_uint(&dec,4)!=1
- ||ec_dec_uint(&dec,5)!=1
- ||ec_dec_uint(&dec,6)!=2
- ||ec_dec_uint(&dec,7)!=6){
- fprintf(stderr,"Encoder bust overwrote range coder data with raw bits.\n");
- ret=-1;
- }
- srand(seed);
- fprintf(stderr,"Testing random streams... Random seed: %u (%.4X)\n", seed, rand() % 65536);
- for(i=0;i<409600;i++){
- unsigned *data;
- unsigned *tell;
- unsigned tell_bits;
- int j;
- int zeros;
- ft=rand()/((RAND_MAX>>(rand()%11U))+1U)+10;
- sz=rand()/((RAND_MAX>>(rand()%9U))+1U);
- data=(unsigned *)malloc(sz*sizeof(*data));
- tell=(unsigned *)malloc((sz+1)*sizeof(*tell));
- ec_enc_init(&enc,ptr,DATA_SIZE2);
- zeros = rand()%13==0;
- tell[0]=ec_tell_frac(&enc);
- for(j=0;j<sz;j++){
- if (zeros)
- data[j]=0;
- else
- data[j]=rand()%ft;
- ec_enc_uint(&enc,data[j],ft);
- tell[j+1]=ec_tell_frac(&enc);
- }
- if (rand()%2==0)
- while(ec_tell(&enc)%8 != 0)
- ec_enc_uint(&enc, rand()%2, 2);
- tell_bits = ec_tell(&enc);
- ec_enc_done(&enc);
- if(tell_bits!=(unsigned)ec_tell(&enc)){
- fprintf(stderr,"ec_tell() changed after ec_enc_done(): %i instead of %i (Random seed: %u)\n",
- ec_tell(&enc),tell_bits,seed);
- ret=-1;
- }
- if ((tell_bits+7)/8 < ec_range_bytes(&enc))
- {
- fprintf (stderr, "ec_tell() lied, there's %i bytes instead of %d (Random seed: %u)\n",
- ec_range_bytes(&enc), (tell_bits+7)/8,seed);
- ret=-1;
- }
- ec_dec_init(&dec,ptr,DATA_SIZE2);
- if(ec_tell_frac(&dec)!=tell[0]){
- fprintf(stderr,
- "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
- 0,ec_tell_frac(&dec),tell[0],seed);
- }
- for(j=0;j<sz;j++){
- sym=ec_dec_uint(&dec,ft);
- if(sym!=data[j]){
- fprintf(stderr,
- "Decoded %i instead of %i with ft of %i at position %i of %i (Random seed: %u).\n",
- sym,data[j],ft,j,sz,seed);
- ret=-1;
- }
- if(ec_tell_frac(&dec)!=tell[j+1]){
- fprintf(stderr,
- "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
- j+1,ec_tell_frac(&dec),tell[j+1],seed);
- }
- }
- free(tell);
- free(data);
- }
- /*Test compatibility between multiple different encode/decode routines.*/
- for(i=0;i<409600;i++){
- unsigned *logp1;
- unsigned *data;
- unsigned *tell;
- unsigned *enc_method;
- int j;
- sz=rand()/((RAND_MAX>>(rand()%9U))+1U);
- logp1=(unsigned *)malloc(sz*sizeof(*logp1));
- data=(unsigned *)malloc(sz*sizeof(*data));
- tell=(unsigned *)malloc((sz+1)*sizeof(*tell));
- enc_method=(unsigned *)malloc(sz*sizeof(*enc_method));
- ec_enc_init(&enc,ptr,DATA_SIZE2);
- tell[0]=ec_tell_frac(&enc);
- for(j=0;j<sz;j++){
- data[j]=rand()/((RAND_MAX>>1)+1);
- logp1[j]=(rand()%15)+1;
- enc_method[j]=rand()/((RAND_MAX>>2)+1);
- switch(enc_method[j]){
- case 0:{
- ec_encode(&enc,data[j]?(1<<logp1[j])-1:0,
- (1<<logp1[j])-(data[j]?0:1),1<<logp1[j]);
- }break;
- case 1:{
- ec_encode_bin(&enc,data[j]?(1<<logp1[j])-1:0,
- (1<<logp1[j])-(data[j]?0:1),logp1[j]);
- }break;
- case 2:{
- ec_enc_bit_logp(&enc,data[j],logp1[j]);
- }break;
- case 3:{
- unsigned char icdf[2];
- icdf[0]=1;
- icdf[1]=0;
- ec_enc_icdf(&enc,data[j],icdf,logp1[j]);
- }break;
- }
- tell[j+1]=ec_tell_frac(&enc);
- }
- ec_enc_done(&enc);
- if((ec_tell(&enc)+7U)/8U<ec_range_bytes(&enc)){
- fprintf(stderr,"tell() lied, there's %i bytes instead of %d (Random seed: %u)\n",
- ec_range_bytes(&enc),(ec_tell(&enc)+7)/8,seed);
- ret=-1;
- }
- ec_dec_init(&dec,ptr,DATA_SIZE2);
- if(ec_tell_frac(&dec)!=tell[0]){
- fprintf(stderr,
- "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
- 0,ec_tell_frac(&dec),tell[0],seed);
- }
- for(j=0;j<sz;j++){
- int fs;
- int dec_method;
- dec_method=rand()/((RAND_MAX>>2)+1);
- switch(dec_method){
- case 0:{
- fs=ec_decode(&dec,1<<logp1[j]);
- sym=fs>=(1<<logp1[j])-1;
- ec_dec_update(&dec,sym?(1<<logp1[j])-1:0,
- (1<<logp1[j])-(sym?0:1),1<<logp1[j]);
- }break;
- case 1:{
- fs=ec_decode_bin(&dec,logp1[j]);
- sym=fs>=(1<<logp1[j])-1;
- ec_dec_update(&dec,sym?(1<<logp1[j])-1:0,
- (1<<logp1[j])-(sym?0:1),1<<logp1[j]);
- }break;
- case 2:{
- sym=ec_dec_bit_logp(&dec,logp1[j]);
- }break;
- case 3:{
- unsigned char icdf[2];
- icdf[0]=1;
- icdf[1]=0;
- sym=ec_dec_icdf(&dec,icdf,logp1[j]);
- }break;
- }
- if(sym!=data[j]){
- fprintf(stderr,
- "Decoded %i instead of %i with logp1 of %i at position %i of %i (Random seed: %u).\n",
- sym,data[j],logp1[j],j,sz,seed);
- fprintf(stderr,"Encoding method: %i, decoding method: %i\n",
- enc_method[j],dec_method);
- ret=-1;
- }
- if(ec_tell_frac(&dec)!=tell[j+1]){
- fprintf(stderr,
- "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
- j+1,ec_tell_frac(&dec),tell[j+1],seed);
- }
- }
- free(enc_method);
- free(tell);
- free(data);
- free(logp1);
- }
- free(ptr);
- return ret;
-}
--- a/celt/tests/laplace-test.c
+++ /dev/null
@@ -1,65 +1,0 @@
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include "laplace.h"
-#define CELT_C
-#include "../celt/stack_alloc.h"
-
-#include "../celt/entenc.c"
-#include "../celt/entdec.c"
-#include "../celt/entcode.c"
-#include "../celt/laplace.c"
-
-#define DATA_SIZE 40000
-
-int ec_laplace_get_start_freq(int decay)
-{
- opus_uint32 ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN+1);
- int fs = (ft*(16384-decay))/(16384+decay);
- return fs+LAPLACE_MINP;
-}
-
-int main(void)
-{
- int i;
- int ret = 0;
- ec_enc enc;
- ec_dec dec;
- unsigned char *ptr;
- int val[10000], decay[10000];
- ALLOC_STACK;
- ptr = (unsigned char *)malloc(DATA_SIZE);
- ec_enc_init(&enc,ptr,DATA_SIZE);
-
- val[0] = 3; decay[0] = 6000;
- val[1] = 0; decay[1] = 5800;
- val[2] = -1; decay[2] = 5600;
- for (i=3;i<10000;i++)
- {
- val[i] = rand()%15-7;
- decay[i] = rand()%11000+5000;
- }
- for (i=0;i<10000;i++)
- ec_laplace_encode(&enc, &val[i],
- ec_laplace_get_start_freq(decay[i]), decay[i]);
-
- ec_enc_done(&enc);
-
- ec_dec_init(&dec,ec_get_buffer(&enc),ec_range_bytes(&enc));
-
- for (i=0;i<10000;i++)
- {
- int d = ec_laplace_decode(&dec,
- ec_laplace_get_start_freq(decay[i]), decay[i]);
- if (d != val[i])
- {
- fprintf (stderr, "Got %d instead of %d\n", d, val[i]);
- ret = 1;
- }
- }
-
- return ret;
-}
--- a/celt/tests/mathops-test.c
+++ /dev/null
@@ -1,238 +1,0 @@
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#ifndef CUSTOM_MODES
-#define CUSTOM_MODES
-#endif
-
-#define CELT_C
-
-#include "mathops.c"
-#include "entenc.c"
-#include "entdec.c"
-#include "entcode.c"
-#include "bands.c"
-#include "vq.c"
-#include "cwrs.c"
-#include <stdio.h>
-#include <math.h>
-
-#ifdef FIXED_POINT
-#define WORD "%d"
-#else
-#define WORD "%f"
-#endif
-
-#ifdef FIXED_DEBUG
-long long celt_mips=0;
-#endif
-int ret = 0;
-
-void testdiv(void)
-{
- opus_int32 i;
- for (i=1;i<=327670;i++)
- {
- double prod;
- opus_val32 val;
- val = celt_rcp(i);
-#ifdef FIXED_POINT
- prod = (1./32768./65526.)*val*i;
-#else
- prod = val*i;
-#endif
- if (fabs(prod-1) > .00025)
- {
- fprintf (stderr, "div failed: 1/%d="WORD" (product = %f)\n", i, val, prod);
- ret = 1;
- }
- }
-}
-
-void testsqrt(void)
-{
- opus_int32 i;
- for (i=1;i<=1000000000;i++)
- {
- double ratio;
- opus_val16 val;
- val = celt_sqrt(i);
- ratio = val/sqrt(i);
- if (fabs(ratio - 1) > .0005 && fabs(val-sqrt(i)) > 2)
- {
- fprintf (stderr, "sqrt failed: sqrt(%d)="WORD" (ratio = %f)\n", i, val, ratio);
- ret = 1;
- }
- i+= i>>10;
- }
-}
-
-void testbitexactcos(void)
-{
- int i;
- opus_int32 min_d,max_d,last,chk;
- chk=max_d=0;
- last=min_d=32767;
- for(i=64;i<=16320;i++)
- {
- opus_int32 d;
- opus_int32 q=bitexact_cos(i);
- chk ^= q*i;
- d = last - q;
- if (d>max_d)max_d=d;
- if (d<min_d)min_d=d;
- last = q;
- }
- if ((chk!=89408644)||(max_d!=5)||(min_d!=0)||(bitexact_cos(64)!=32767)||
- (bitexact_cos(16320)!=200)||(bitexact_cos(8192)!=23171))
- {
- fprintf (stderr, "bitexact_cos failed\n");
- ret = 1;
- }
-}
-
-void testbitexactlog2tan(void)
-{
- int i,fail;
- opus_int32 min_d,max_d,last,chk;
- fail=chk=max_d=0;
- last=min_d=15059;
- for(i=64;i<8193;i++)
- {
- opus_int32 d;
- opus_int32 mid=bitexact_cos(i);
- opus_int32 side=bitexact_cos(16384-i);
- opus_int32 q=bitexact_log2tan(mid,side);
- chk ^= q*i;
- d = last - q;
- if (q!=-1*bitexact_log2tan(side,mid))
- fail = 1;
- if (d>max_d)max_d=d;
- if (d<min_d)min_d=d;
- last = q;
- }
- if ((chk!=15821257)||(max_d!=61)||(min_d!=-2)||fail||
- (bitexact_log2tan(32767,200)!=15059)||(bitexact_log2tan(30274,12540)!=2611)||
- (bitexact_log2tan(23171,23171)!=0))
- {
- fprintf (stderr, "bitexact_log2tan failed\n");
- ret = 1;
- }
-}
-
-#ifndef FIXED_POINT
-void testlog2(void)
-{
- float x;
- for (x=0.001;x<1677700.0;x+=(x/8.0))
- {
- float error = fabs((1.442695040888963387*log(x))-celt_log2(x));
- if (error>0.0009)
- {
- fprintf (stderr, "celt_log2 failed: fabs((1.442695040888963387*log(x))-celt_log2(x))>0.001 (x = %f, error = %f)\n", x,error);
- ret = 1;
- }
- }
-}
-
-void testexp2(void)
-{
- float x;
- for (x=-11.0;x<24.0;x+=0.0007)
- {
- float error = fabs(x-(1.442695040888963387*log(celt_exp2(x))));
- if (error>0.0002)
- {
- fprintf (stderr, "celt_exp2 failed: fabs(x-(1.442695040888963387*log(celt_exp2(x))))>0.0005 (x = %f, error = %f)\n", x,error);
- ret = 1;
- }
- }
-}
-
-void testexp2log2(void)
-{
- float x;
- for (x=-11.0;x<24.0;x+=0.0007)
- {
- float error = fabs(x-(celt_log2(celt_exp2(x))));
- if (error>0.001)
- {
- fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_log2(celt_exp2(x))))>0.001 (x = %f, error = %f)\n", x,error);
- ret = 1;
- }
- }
-}
-#else
-void testlog2(void)
-{
- opus_val32 x;
- for (x=8;x<1073741824;x+=(x>>3))
- {
- float error = fabs((1.442695040888963387*log(x/16384.0))-celt_log2(x)/1024.0);
- if (error>0.003)
- {
- fprintf (stderr, "celt_log2 failed: x = %ld, error = %f\n", (long)x,error);
- ret = 1;
- }
- }
-}
-
-void testexp2(void)
-{
- opus_val16 x;
- for (x=-32768;x<15360;x++)
- {
- float error1 = fabs(x/1024.0-(1.442695040888963387*log(celt_exp2(x)/65536.0)));
- float error2 = fabs(exp(0.6931471805599453094*x/1024.0)-celt_exp2(x)/65536.0);
- if (error1>0.0002&&error2>0.00004)
- {
- fprintf (stderr, "celt_exp2 failed: x = "WORD", error1 = %f, error2 = %f\n", x,error1,error2);
- ret = 1;
- }
- }
-}
-
-void testexp2log2(void)
-{
- opus_val32 x;
- for (x=8;x<65536;x+=(x>>3))
- {
- float error = fabs(x-0.25*celt_exp2(celt_log2(x)))/16384;
- if (error>0.004)
- {
- fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_exp2(celt_log2(x))))>0.001 (x = %ld, error = %f)\n", (long)x,error);
- ret = 1;
- }
- }
-}
-
-void testilog2(void)
-{
- opus_val32 x;
- for (x=1;x<=268435455;x+=127)
- {
- opus_val32 error = abs(celt_ilog2(x)-(int)floor(log2(x)));
- if (error!=0)
- {
- printf("celt_ilog2 failed: celt_ilog2(x)!=floor(log2(x)) (x = %d, error = %d)\n",x,error);
- ret = 1;
- }
- }
-}
-#endif
-
-int main(void)
-{
- testbitexactcos();
- testbitexactlog2tan();
- testdiv();
- testsqrt();
- testlog2();
- testexp2();
- testexp2log2();
-#ifdef FIXED_POINT
- testilog2();
-#endif
- return ret;
-}
--- a/celt/tests/mdct-test.c
+++ /dev/null
@@ -1,170 +1,0 @@
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#define SKIP_CONFIG_H
-
-#ifndef CUSTOM_MODES
-#define CUSTOM_MODES
-#endif
-
-#include <stdio.h>
-#include "mdct.h"
-#define CELT_C
-#include "../celt/stack_alloc.h"
-
-#include "../celt/kiss_fft.c"
-#include "../celt/mdct.c"
-#include "../celt/mathops.c"
-#include "../celt/entcode.c"
-
-#ifndef M_PI
-#define M_PI 3.141592653
-#endif
-
-#ifdef FIXED_DEBUG
-long long celt_mips=0;
-#endif
-int ret = 0;
-void check(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
-{
- int bin,k;
- double errpow=0,sigpow=0;
- double snr;
- for (bin=0;bin<nfft/2;++bin) {
- double ansr = 0;
- double difr;
-
- for (k=0;k<nfft;++k) {
- double phase = 2*M_PI*(k+.5+.25*nfft)*(bin+.5)/nfft;
- double re = cos(phase);
-
- re /= nfft/4;
-
- ansr += in[k] * re;
- }
- /*printf ("%f %f\n", ansr, out[bin]);*/
- difr = ansr - out[bin];
- errpow += difr*difr;
- sigpow += ansr*ansr;
- }
- snr = 10*log10(sigpow/errpow);
- printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
- if (snr<60) {
- printf( "** poor snr: %f **\n", snr);
- ret = 1;
- }
-}
-
-void check_inv(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
-{
- int bin,k;
- double errpow=0,sigpow=0;
- double snr;
- for (bin=0;bin<nfft;++bin) {
- double ansr = 0;
- double difr;
-
- for (k=0;k<nfft/2;++k) {
- double phase = 2*M_PI*(bin+.5+.25*nfft)*(k+.5)/nfft;
- double re = cos(phase);
-
- /*re *= 2;*/
-
- ansr += in[k] * re;
- }
- /*printf ("%f %f\n", ansr, out[bin]);*/
- difr = ansr - out[bin];
- errpow += difr*difr;
- sigpow += ansr*ansr;
- }
- snr = 10*log10(sigpow/errpow);
- printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
- if (snr<60) {
- printf( "** poor snr: %f **\n", snr);
- ret = 1;
- }
-}
-
-
-void test1d(int nfft,int isinverse)
-{
- mdct_lookup cfg;
- size_t buflen = sizeof(kiss_fft_scalar)*nfft;
-
- kiss_fft_scalar * in = (kiss_fft_scalar*)malloc(buflen);
- kiss_fft_scalar * in_copy = (kiss_fft_scalar*)malloc(buflen);
- kiss_fft_scalar * out= (kiss_fft_scalar*)malloc(buflen);
- opus_val16 * window= (opus_val16*)malloc(sizeof(opus_val16)*nfft/2);
- int k;
-
- clt_mdct_init(&cfg, nfft, 0);
- for (k=0;k<nfft;++k) {
- in[k] = (rand() % 32768) - 16384;
- }
-
- for (k=0;k<nfft/2;++k) {
- window[k] = Q15ONE;
- }
- for (k=0;k<nfft;++k) {
- in[k] *= 32768;
- }
-
- if (isinverse)
- {
- for (k=0;k<nfft;++k) {
- in[k] /= nfft;
- }
- }
-
- for (k=0;k<nfft;++k)
- in_copy[k] = in[k];
- /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/
-
- if (isinverse)
- {
- for (k=0;k<nfft;++k)
- out[k] = 0;
- clt_mdct_backward(&cfg,in,out, window, nfft/2, 0, 1);
- check_inv(in,out,nfft,isinverse);
- } else {
- clt_mdct_forward(&cfg,in,out,window, nfft/2, 0, 1);
- check(in_copy,out,nfft,isinverse);
- }
- /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/
-
-
- free(in);
- free(out);
- clt_mdct_clear(&cfg);
-}
-
-int main(int argc,char ** argv)
-{
- ALLOC_STACK;
- if (argc>1) {
- int k;
- for (k=1;k<argc;++k) {
- test1d(atoi(argv[k]),0);
- test1d(atoi(argv[k]),1);
- }
- }else{
- test1d(32,0);
- test1d(32,1);
- test1d(256,0);
- test1d(256,1);
- test1d(512,0);
- test1d(512,1);
-#ifndef RADIX_TWO_ONLY
- test1d(40,0);
- test1d(40,1);
- test1d(120,0);
- test1d(120,1);
- test1d(240,0);
- test1d(240,1);
- test1d(480,0);
- test1d(480,1);
-#endif
- }
- return ret;
-}
--- a/celt/tests/real-fft-test.c
+++ /dev/null
@@ -1,171 +1,0 @@
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include "kiss_fftr.h"
-#include "_kiss_fft_guts.h"
-#include <stdio.h>
-#include <string.h>
-
-#define CELT_C
-#include "../celt/stack_alloc.h"
-#include "../celt/kiss_fft.c"
-#include "../celt/kiss_fftr.c"
-
-#ifdef FIXED_DEBUG
-long long celt_mips=0;
-#endif
-int ret=0;
-
-static
-kiss_fft_scalar rand_scalar(void)
-{
- return (rand()%32767)-16384;
-}
-
-static
-double snr_compare( kiss_fft_cpx * vec1,kiss_fft_scalar * vec2, int n)
-{
- int k;
- double sigpow=1e-10, noisepow=1e-10, err,snr;
-
- vec1[0].i = vec1[n].r;
- for (k=0;k<n;++k) {
- sigpow += (double)vec1[k].r * (double)vec1[k].r +
- (double)vec1[k].i * (double)vec1[k].i;
- err = (double)vec1[k].r - (double)vec2[2*k];
- /*printf ("%f %f\n", (double)vec1[k].r, (double)vec2[2*k]);*/
- noisepow += err * err;
- err = (double)vec1[k].i - (double)vec2[2*k+1];
- /*printf ("%f %f\n", (double)vec1[k].i, (double)vec2[2*k+1]);*/
- noisepow += err * err;
-
- }
- snr = 10*log10( sigpow / noisepow );
- if (snr<60) {
- printf( "** poor snr: %f **\n", snr);
- ret = 1;
- }
- return snr;
-}
-
-static
-double snr_compare_scal( kiss_fft_scalar * vec1,kiss_fft_scalar * vec2, int n)
-{
- int k;
- double sigpow=1e-10, noisepow=1e-10, err,snr;
-
- for (k=0;k<n;++k) {
- sigpow += (double)vec1[k] * (double)vec1[k];
- err = (double)vec1[k] - (double)vec2[k];
- noisepow += err * err;
- }
- snr = 10*log10( sigpow / noisepow );
- if (snr<60) {
- printf( "\npoor snr: %f\n", snr);
- ret = 1;
- }
- return snr;
-}
-#ifdef RADIX_TWO_ONLY
-#define NFFT 1024
-#else
-#define NFFT 8*3*5
-#endif
-
-#ifndef NUMFFTS
-#define NUMFFTS 10000
-#endif
-
-
-int main(void)
-{
- int i;
- kiss_fft_cpx cin[NFFT];
- kiss_fft_cpx cout[NFFT];
- kiss_fft_scalar fin[NFFT];
- kiss_fft_scalar sout[NFFT];
- kiss_fft_cfg kiss_fft_state;
- kiss_fftr_cfg kiss_fftr_state;
-
- kiss_fft_scalar rin[NFFT+2];
- kiss_fft_scalar rout[NFFT+2];
- kiss_fft_scalar zero;
- ALLOC_STACK;
- memset(&zero,0,sizeof(zero) ); // ugly way of setting short,int,float,double, or __m128 to zero
-
- for (i=0;i<NFFT;++i) {
- rin[i] = rand_scalar();
-#if defined(FIXED_POINT) && defined(DOUBLE_PRECISION)
- rin[i] *= 32768;
-#endif
- cin[i].r = rin[i];
- cin[i].i = zero;
- }
-
- kiss_fft_state = opus_fft_alloc(NFFT,0,0);
- kiss_fftr_state = kiss_fftr_alloc(NFFT,0,0);
- opus_fft(kiss_fft_state,cin,cout);
- kiss_fftr(kiss_fftr_state,rin,sout);
-
- printf( "nfft=%d, inverse=%d, snr=%g\n",
- NFFT,0, snr_compare(cout,sout,(NFFT/2)) );
-
- memset(cin,0,sizeof(cin));
- cin[0].r = rand_scalar();
- cin[NFFT/2].r = rand_scalar();
- for (i=1;i< NFFT/2;++i) {
- //cin[i].r = (kiss_fft_scalar)(rand()-RAND_MAX/2);
- cin[i].r = rand_scalar();
- cin[i].i = rand_scalar();
- }
-
- // conjugate symmetry of real signal
- for (i=1;i< NFFT/2;++i) {
- cin[NFFT-i].r = cin[i].r;
- cin[NFFT-i].i = - cin[i].i;
- }
-
-
-#ifdef FIXED_POINT
-#ifdef DOUBLE_PRECISION
- for (i=0;i< NFFT;++i) {
- cin[i].r *= 32768;
- cin[i].i *= 32768;
- }
-#endif
- for (i=0;i< NFFT;++i) {
- cin[i].r /= NFFT;
- cin[i].i /= NFFT;
- }
-#endif
-
- fin[0] = cin[0].r;
- fin[1] = cin[NFFT/2].r;
- for (i=1;i< NFFT/2;++i)
- {
- fin[2*i] = cin[i].r;
- fin[2*i+1] = cin[i].i;
- }
-
- opus_ifft(kiss_fft_state,cin,cout);
- kiss_fftri(kiss_fftr_state,fin,rout);
- /*
- printf(" results from inverse opus_fft : (%f,%f), (%f,%f), (%f,%f), (%f,%f), (%f,%f) ...\n "
- , (float)cout[0].r , (float)cout[0].i , (float)cout[1].r , (float)cout[1].i , (float)cout[2].r , (float)cout[2].i , (float)cout[3].r , (float)cout[3].i , (float)cout[4].r , (float)cout[4].i
- );
-
- printf(" results from inverse kiss_fftr: %f,%f,%f,%f,%f ... \n"
- ,(float)rout[0] ,(float)rout[1] ,(float)rout[2] ,(float)rout[3] ,(float)rout[4]);
-*/
- for (i=0;i<NFFT;++i) {
- sout[i] = cout[i].r;
- }
-
- printf( "nfft=%d, inverse=%d, snr=%g\n",
- NFFT,1, snr_compare_scal(rout,sout,NFFT) );
- free(kiss_fft_state);
- free(kiss_fftr_state);
-
- return ret;
-}
--- a/celt/tests/rotation-test.c
+++ /dev/null
@@ -1,63 +1,0 @@
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#ifndef CUSTOM_MODES
-#define CUSTOM_MODES
-#endif
-
-#define CELT_C
-
-#include <stdio.h>
-#include <stdlib.h>
-#include "vq.c"
-#include "cwrs.c"
-#include "entcode.c"
-#include "entenc.c"
-#include "entdec.c"
-#include "mathops.c"
-#include "bands.h"
-#include <math.h>
-#define MAX_SIZE 100
-
-int ret=0;
-void test_rotation(int N, int K)
-{
- int i;
- double err = 0, ener = 0, snr, snr0;
- opus_val16 x0[MAX_SIZE];
- opus_val16 x1[MAX_SIZE];
- for (i=0;i<N;i++)
- x1[i] = x0[i] = rand()%32767-16384;
- exp_rotation(x1, N, 1, 1, K, SPREAD_NORMAL);
- for (i=0;i<N;i++)
- {
- err += (x0[i]-(double)x1[i])*(x0[i]-(double)x1[i]);
- ener += x0[i]*(double)x0[i];
- }
- snr0 = 20*log10(ener/err);
- err = ener = 0;
- exp_rotation(x1, N, -1, 1, K, SPREAD_NORMAL);
- for (i=0;i<N;i++)
- {
- err += (x0[i]-(double)x1[i])*(x0[i]-(double)x1[i]);
- ener += x0[i]*(double)x0[i];
- }
- snr = 20*log10(ener/err);
- printf ("SNR for size %d (%d pulses) is %f (was %f without inverse)\n", N, K, snr, snr0);
- if (snr < 60 || snr0 > 20)
- {
- fprintf(stderr, "FAIL!\n");
- ret = 1;
- }
-}
-
-int main(void)
-{
- ALLOC_STACK;
- test_rotation(15, 3);
- test_rotation(23, 5);
- test_rotation(50, 3);
- test_rotation(80, 1);
- return ret;
-}
--- /dev/null
+++ b/celt/tests/test_unit_cwrs32.c
@@ -1,0 +1,195 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <string.h>
+
+#ifndef CUSTOM_MODES
+#define CUSTOM_MODES
+#else
+#define TEST_CUSTOM_MODES
+#endif
+
+#define CELT_C
+#include "stack_alloc.h"
+#include "entenc.c"
+#include "entdec.c"
+#include "entcode.c"
+#include "cwrs.c"
+#include "mathops.c"
+#include "rate.h"
+
+#define NMAX (240)
+#define KMAX (128)
+
+#ifdef TEST_CUSTOM_MODES
+
+#define NDIMS (46)
+static const int pn[NDIMS]={
+ 2, 3, 4, 5, 6, 7, 8, 9, 10,
+ 11, 12, 13, 14, 15, 16, 18, 20, 22,
+ 24, 26, 28, 30, 32, 36, 40, 44, 48,
+ 52, 56, 60, 64, 72, 80, 88, 96, 104,
+ 112, 120, 128, 144, 160, 176, 192, 208, 224,
+ 240
+};
+static const int pkmax[NDIMS]={
+ 128, 128, 128, 128, 88, 52, 36, 26, 22,
+ 18, 16, 15, 13, 12, 12, 11, 10, 9,
+ 9, 8, 8, 7, 7, 7, 7, 6, 6,
+ 6, 6, 6, 5, 5, 5, 5, 5, 5,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 4
+};
+
+#else /* TEST_CUSTOM_MODES */
+
+#define NDIMS (22)
+static const int pn[NDIMS]={
+ 2, 3, 4, 6, 8, 9, 11, 12, 16,
+ 18, 22, 24, 32, 36, 44, 48, 64, 72,
+ 88, 96, 144, 176
+};
+static const int pkmax[NDIMS]={
+ 128, 128, 128, 88, 36, 26, 18, 16, 12,
+ 11, 9, 9, 7, 7, 6, 6, 5, 5,
+ 5, 5, 4, 4
+};
+
+#endif
+
+int main(void){
+ int t;
+ int n;
+ ALLOC_STACK;
+ for(t=0;t<NDIMS;t++){
+ int pseudo;
+ n=pn[t];
+ for(pseudo=1;pseudo<41;pseudo++)
+ {
+ int k;
+ opus_uint32 uu[KMAX+2U];
+ opus_uint32 inc;
+ opus_uint32 nc;
+ opus_uint32 i;
+ k=get_pulses(pseudo);
+ if (k>pkmax[t])break;
+ printf("Testing CWRS with N=%i, K=%i...\n",n,k);
+ nc=ncwrs_urow(n,k,uu);
+ inc=nc/20000;
+ if(inc<1)inc=1;
+ for(i=0;i<nc;i+=inc){
+ opus_uint32 u[KMAX+2U];
+ int y[NMAX];
+ int sy;
+ int yy[5];
+ opus_uint32 v;
+ opus_uint32 ii;
+ int kk;
+ int j;
+ memcpy(u,uu,(k+2U)*sizeof(*u));
+ cwrsi(n,k,i,y,u);
+ sy=0;
+ for(j=0;j<n;j++)sy+=ABS(y[j]);
+ if(sy!=k){
+ fprintf(stderr,"N=%d Pulse count mismatch in cwrsi (%d!=%d).\n",
+ n,sy,k);
+ return 99;
+ }
+ /*printf("%6u of %u:",i,nc);
+ for(j=0;j<n;j++)printf(" %+3i",y[j]);
+ printf(" ->");*/
+ ii=icwrs(n,k,&v,y,u);
+ if(ii!=i){
+ fprintf(stderr,"Combination-index mismatch (%lu!=%lu).\n",
+ (long)ii,(long)i);
+ return 1;
+ }
+ if(v!=nc){
+ fprintf(stderr,"Combination count mismatch (%lu!=%lu).\n",
+ (long)v,(long)nc);
+ return 2;
+ }
+#ifndef SMALL_FOOTPRINT
+ if(n==2){
+ cwrsi2(k,i,yy);
+ for(j=0;j<2;j++)if(yy[j]!=y[j]){
+ fprintf(stderr,"N=2 pulse vector mismatch ({%i,%i}!={%i,%i}).\n",
+ yy[0],yy[1],y[0],y[1]);
+ return 3;
+ }
+ ii=icwrs2(yy,&kk);
+ if(ii!=i){
+ fprintf(stderr,"N=2 combination-index mismatch (%lu!=%lu).\n",
+ (long)ii,(long)i);
+ return 4;
+ }
+ if(kk!=k){
+ fprintf(stderr,"N=2 pulse count mismatch (%i,%i).\n",kk,k);
+ return 5;
+ }
+ v=ncwrs2(k);
+ if(v!=nc){
+ fprintf(stderr,"N=2 combination count mismatch (%lu,%lu).\n",
+ (long)v,(long)nc);
+ return 6;
+ }
+ }
+ else if(n==3){
+ cwrsi3(k,i,yy);
+ for(j=0;j<3;j++)if(yy[j]!=y[j]){
+ fprintf(stderr,"N=3 pulse vector mismatch "
+ "({%i,%i,%i}!={%i,%i,%i}).\n",yy[0],yy[1],yy[2],y[0],y[1],y[2]);
+ return 7;
+ }
+ ii=icwrs3(yy,&kk);
+ if(ii!=i){
+ fprintf(stderr,"N=3 combination-index mismatch (%lu!=%lu).\n",
+ (long)ii,(long)i);
+ return 8;
+ }
+ if(kk!=k){
+ fprintf(stderr,"N=3 pulse count mismatch (%i!=%i).\n",kk,k);
+ return 9;
+ }
+ v=ncwrs3(k);
+ if(v!=nc){
+ fprintf(stderr,"N=3 combination count mismatch (%lu!=%lu).\n",
+ (long)v,(long)nc);
+ return 10;
+ }
+ }
+ else if(n==4){
+ cwrsi4(k,i,yy);
+ for(j=0;j<4;j++)if(yy[j]!=y[j]){
+ fprintf(stderr,"N=4 pulse vector mismatch "
+ "({%i,%i,%i,%i}!={%i,%i,%i,%i}.\n",
+ yy[0],yy[1],yy[2],yy[3],y[0],y[1],y[2],y[3]);
+ return 11;
+ }
+ ii=icwrs4(yy,&kk);
+ if(ii!=i){
+ fprintf(stderr,"N=4 combination-index mismatch (%lu!=%lu).\n",
+ (long)ii,(long)i);
+ return 12;
+ }
+ if(kk!=k){
+ fprintf(stderr,"N=4 pulse count mismatch (%i!=%i).\n",kk,k);
+ return 13;
+ }
+ v=ncwrs4(k);
+ if(v!=nc){
+ fprintf(stderr,"N=4 combination count mismatch (%lu!=%lu).\n",
+ (long)v,(long)nc);
+ return 14;
+ }
+ }
+#endif /* SMALL_FOOTPRINT */
+ /*printf(" %6u\n",i);*/
+ }
+ /*printf("\n");*/
+ }
+ }
+ return 0;
+}
--- /dev/null
+++ b/celt/tests/test_unit_dft.c
@@ -1,0 +1,140 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#define SKIP_CONFIG_H
+
+#ifndef CUSTOM_MODES
+#define CUSTOM_MODES
+#endif
+
+#include <stdio.h>
+#include "kiss_fft.h"
+
+#define CELT_C
+#include "stack_alloc.h"
+#include "kiss_fft.c"
+#include "mathops.c"
+#include "entcode.c"
+
+
+#ifndef M_PI
+#define M_PI 3.141592653
+#endif
+
+#ifdef FIXED_DEBUG
+long long celt_mips=0;
+#endif
+int ret = 0;
+
+void check(kiss_fft_cpx * in,kiss_fft_cpx * out,int nfft,int isinverse)
+{
+ int bin,k;
+ double errpow=0,sigpow=0, snr;
+
+ for (bin=0;bin<nfft;++bin) {
+ double ansr = 0;
+ double ansi = 0;
+ double difr;
+ double difi;
+
+ for (k=0;k<nfft;++k) {
+ double phase = -2*M_PI*bin*k/nfft;
+ double re = cos(phase);
+ double im = sin(phase);
+ if (isinverse)
+ im = -im;
+
+ if (!isinverse)
+ {
+ re /= nfft;
+ im /= nfft;
+ }
+
+ ansr += in[k].r * re - in[k].i * im;
+ ansi += in[k].r * im + in[k].i * re;
+ }
+ /*printf ("%d %d ", (int)ansr, (int)ansi);*/
+ difr = ansr - out[bin].r;
+ difi = ansi - out[bin].i;
+ errpow += difr*difr + difi*difi;
+ sigpow += ansr*ansr+ansi*ansi;
+ }
+ snr = 10*log10(sigpow/errpow);
+ printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
+ if (snr<60) {
+ printf( "** poor snr: %f ** \n", snr);
+ ret = 1;
+ }
+}
+
+void test1d(int nfft,int isinverse)
+{
+ size_t buflen = sizeof(kiss_fft_cpx)*nfft;
+
+ kiss_fft_cpx * in = (kiss_fft_cpx*)malloc(buflen);
+ kiss_fft_cpx * out= (kiss_fft_cpx*)malloc(buflen);
+ kiss_fft_state *cfg = opus_fft_alloc(nfft,0,0);
+ int k;
+
+ for (k=0;k<nfft;++k) {
+ in[k].r = (rand() % 32767) - 16384;
+ in[k].i = (rand() % 32767) - 16384;
+ }
+
+ for (k=0;k<nfft;++k) {
+ in[k].r *= 32768;
+ in[k].i *= 32768;
+ }
+
+ if (isinverse)
+ {
+ for (k=0;k<nfft;++k) {
+ in[k].r /= nfft;
+ in[k].i /= nfft;
+ }
+ }
+
+ /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/
+
+ if (isinverse)
+ opus_ifft(cfg,in,out);
+ else
+ opus_fft(cfg,in,out);
+
+ /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/
+
+ check(in,out,nfft,isinverse);
+
+ free(in);
+ free(out);
+ free(cfg);
+}
+
+int main(int argc,char ** argv)
+{
+ ALLOC_STACK;
+ if (argc>1) {
+ int k;
+ for (k=1;k<argc;++k) {
+ test1d(atoi(argv[k]),0);
+ test1d(atoi(argv[k]),1);
+ }
+ }else{
+ test1d(32,0);
+ test1d(32,1);
+ test1d(128,0);
+ test1d(128,1);
+ test1d(256,0);
+ test1d(256,1);
+#ifndef RADIX_TWO_ONLY
+ test1d(36,0);
+ test1d(36,1);
+ test1d(50,0);
+ test1d(50,1);
+ test1d(120,0);
+ test1d(120,1);
+#endif
+ }
+ return ret;
+}
--- /dev/null
+++ b/celt/tests/test_unit_entropy.c
@@ -1,0 +1,295 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+#include <time.h>
+#include "entcode.h"
+#include "entenc.h"
+#include "entdec.h"
+#include <string.h>
+
+#include "entenc.c"
+#include "entdec.c"
+#include "entcode.c"
+
+#ifndef M_LOG2E
+# define M_LOG2E 1.4426950408889634074
+#endif
+#define DATA_SIZE 10000000
+#define DATA_SIZE2 10000
+
+int main(int _argc,char **_argv){
+ ec_enc enc;
+ ec_dec dec;
+ long nbits;
+ long nbits2;
+ double entropy;
+ int ft;
+ int ftb;
+ int sz;
+ int i;
+ int ret;
+ unsigned int sym;
+ unsigned int seed;
+ unsigned char *ptr;
+ ret=0;
+ entropy=0;
+ if (_argc > 2) {
+ fprintf(stderr, "Usage: %s [<seed>]\n", _argv[0]);
+ return 1;
+ }
+ if (_argc > 1)
+ seed = atoi(_argv[1]);
+ else
+ seed = time(NULL);
+ /*Testing encoding of raw bit values.*/
+ ptr = (unsigned char *)malloc(DATA_SIZE);
+ ec_enc_init(&enc,ptr, DATA_SIZE);
+ for(ft=2;ft<1024;ft++){
+ for(i=0;i<ft;i++){
+ entropy+=log(ft)*M_LOG2E;
+ ec_enc_uint(&enc,i,ft);
+ }
+ }
+ /*Testing encoding of raw bit values.*/
+ for(ftb=1;ftb<16;ftb++){
+ for(i=0;i<(1<<ftb);i++){
+ entropy+=ftb;
+ nbits=ec_tell(&enc);
+ ec_enc_bits(&enc,i,ftb);
+ nbits2=ec_tell(&enc);
+ if(nbits2-nbits!=ftb){
+ fprintf(stderr,"Used %li bits to encode %i bits directly.\n",
+ nbits2-nbits,ftb);
+ ret=-1;
+ }
+ }
+ }
+ nbits=ec_tell_frac(&enc);
+ ec_enc_done(&enc);
+ fprintf(stderr,
+ "Encoded %0.2lf bits of entropy to %0.2lf bits (%0.3lf%% wasted).\n",
+ entropy,ldexp(nbits,-3),100*(nbits-ldexp(entropy,3))/nbits);
+ fprintf(stderr,"Packed to %li bytes.\n",(long)ec_range_bytes(&enc));
+ ec_dec_init(&dec,ptr,DATA_SIZE);
+ for(ft=2;ft<1024;ft++){
+ for(i=0;i<ft;i++){
+ sym=ec_dec_uint(&dec,ft);
+ if(sym!=(unsigned)i){
+ fprintf(stderr,"Decoded %i instead of %i with ft of %i.\n",sym,i,ft);
+ ret=-1;
+ }
+ }
+ }
+ for(ftb=1;ftb<16;ftb++){
+ for(i=0;i<(1<<ftb);i++){
+ sym=ec_dec_bits(&dec,ftb);
+ if(sym!=(unsigned)i){
+ fprintf(stderr,"Decoded %i instead of %i with ftb of %i.\n",sym,i,ftb);
+ ret=-1;
+ }
+ }
+ }
+ nbits2=ec_tell_frac(&dec);
+ if(nbits!=nbits2){
+ fprintf(stderr,
+ "Reported number of bits used was %0.2lf, should be %0.2lf.\n",
+ ldexp(nbits2,-3),ldexp(nbits,-3));
+ ret=-1;
+ }
+ /*Testing an encoder bust prefers range coder data over raw bits.
+ This isn't a general guarantee, will only work for data that is buffered in
+ the encoder state and not yet stored in the user buffer, and should never
+ get used in practice.
+ It's mostly here for code coverage completeness.*/
+ /*Start with a 16-bit buffer.*/
+ ec_enc_init(&enc,ptr,2);
+ /*Write 7 raw bits.*/
+ ec_enc_bits(&enc,0x55,7);
+ /*Write 12.3 bits of range coder data.*/
+ ec_enc_uint(&enc,1,2);
+ ec_enc_uint(&enc,1,3);
+ ec_enc_uint(&enc,1,4);
+ ec_enc_uint(&enc,1,5);
+ ec_enc_uint(&enc,2,6);
+ ec_enc_uint(&enc,6,7);
+ ec_enc_done(&enc);
+ ec_dec_init(&dec,ptr,2);
+ if(!enc.error
+ /*The raw bits should have been overwritten by the range coder data.*/
+ ||ec_dec_bits(&dec,7)!=0x05
+ /*And all the range coder data should have been encoded correctly.*/
+ ||ec_dec_uint(&dec,2)!=1
+ ||ec_dec_uint(&dec,3)!=1
+ ||ec_dec_uint(&dec,4)!=1
+ ||ec_dec_uint(&dec,5)!=1
+ ||ec_dec_uint(&dec,6)!=2
+ ||ec_dec_uint(&dec,7)!=6){
+ fprintf(stderr,"Encoder bust overwrote range coder data with raw bits.\n");
+ ret=-1;
+ }
+ srand(seed);
+ fprintf(stderr,"Testing random streams... Random seed: %u (%.4X)\n", seed, rand() % 65536);
+ for(i=0;i<409600;i++){
+ unsigned *data;
+ unsigned *tell;
+ unsigned tell_bits;
+ int j;
+ int zeros;
+ ft=rand()/((RAND_MAX>>(rand()%11U))+1U)+10;
+ sz=rand()/((RAND_MAX>>(rand()%9U))+1U);
+ data=(unsigned *)malloc(sz*sizeof(*data));
+ tell=(unsigned *)malloc((sz+1)*sizeof(*tell));
+ ec_enc_init(&enc,ptr,DATA_SIZE2);
+ zeros = rand()%13==0;
+ tell[0]=ec_tell_frac(&enc);
+ for(j=0;j<sz;j++){
+ if (zeros)
+ data[j]=0;
+ else
+ data[j]=rand()%ft;
+ ec_enc_uint(&enc,data[j],ft);
+ tell[j+1]=ec_tell_frac(&enc);
+ }
+ if (rand()%2==0)
+ while(ec_tell(&enc)%8 != 0)
+ ec_enc_uint(&enc, rand()%2, 2);
+ tell_bits = ec_tell(&enc);
+ ec_enc_done(&enc);
+ if(tell_bits!=(unsigned)ec_tell(&enc)){
+ fprintf(stderr,"ec_tell() changed after ec_enc_done(): %i instead of %i (Random seed: %u)\n",
+ ec_tell(&enc),tell_bits,seed);
+ ret=-1;
+ }
+ if ((tell_bits+7)/8 < ec_range_bytes(&enc))
+ {
+ fprintf (stderr, "ec_tell() lied, there's %i bytes instead of %d (Random seed: %u)\n",
+ ec_range_bytes(&enc), (tell_bits+7)/8,seed);
+ ret=-1;
+ }
+ ec_dec_init(&dec,ptr,DATA_SIZE2);
+ if(ec_tell_frac(&dec)!=tell[0]){
+ fprintf(stderr,
+ "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
+ 0,ec_tell_frac(&dec),tell[0],seed);
+ }
+ for(j=0;j<sz;j++){
+ sym=ec_dec_uint(&dec,ft);
+ if(sym!=data[j]){
+ fprintf(stderr,
+ "Decoded %i instead of %i with ft of %i at position %i of %i (Random seed: %u).\n",
+ sym,data[j],ft,j,sz,seed);
+ ret=-1;
+ }
+ if(ec_tell_frac(&dec)!=tell[j+1]){
+ fprintf(stderr,
+ "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
+ j+1,ec_tell_frac(&dec),tell[j+1],seed);
+ }
+ }
+ free(tell);
+ free(data);
+ }
+ /*Test compatibility between multiple different encode/decode routines.*/
+ for(i=0;i<409600;i++){
+ unsigned *logp1;
+ unsigned *data;
+ unsigned *tell;
+ unsigned *enc_method;
+ int j;
+ sz=rand()/((RAND_MAX>>(rand()%9U))+1U);
+ logp1=(unsigned *)malloc(sz*sizeof(*logp1));
+ data=(unsigned *)malloc(sz*sizeof(*data));
+ tell=(unsigned *)malloc((sz+1)*sizeof(*tell));
+ enc_method=(unsigned *)malloc(sz*sizeof(*enc_method));
+ ec_enc_init(&enc,ptr,DATA_SIZE2);
+ tell[0]=ec_tell_frac(&enc);
+ for(j=0;j<sz;j++){
+ data[j]=rand()/((RAND_MAX>>1)+1);
+ logp1[j]=(rand()%15)+1;
+ enc_method[j]=rand()/((RAND_MAX>>2)+1);
+ switch(enc_method[j]){
+ case 0:{
+ ec_encode(&enc,data[j]?(1<<logp1[j])-1:0,
+ (1<<logp1[j])-(data[j]?0:1),1<<logp1[j]);
+ }break;
+ case 1:{
+ ec_encode_bin(&enc,data[j]?(1<<logp1[j])-1:0,
+ (1<<logp1[j])-(data[j]?0:1),logp1[j]);
+ }break;
+ case 2:{
+ ec_enc_bit_logp(&enc,data[j],logp1[j]);
+ }break;
+ case 3:{
+ unsigned char icdf[2];
+ icdf[0]=1;
+ icdf[1]=0;
+ ec_enc_icdf(&enc,data[j],icdf,logp1[j]);
+ }break;
+ }
+ tell[j+1]=ec_tell_frac(&enc);
+ }
+ ec_enc_done(&enc);
+ if((ec_tell(&enc)+7U)/8U<ec_range_bytes(&enc)){
+ fprintf(stderr,"tell() lied, there's %i bytes instead of %d (Random seed: %u)\n",
+ ec_range_bytes(&enc),(ec_tell(&enc)+7)/8,seed);
+ ret=-1;
+ }
+ ec_dec_init(&dec,ptr,DATA_SIZE2);
+ if(ec_tell_frac(&dec)!=tell[0]){
+ fprintf(stderr,
+ "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
+ 0,ec_tell_frac(&dec),tell[0],seed);
+ }
+ for(j=0;j<sz;j++){
+ int fs;
+ int dec_method;
+ dec_method=rand()/((RAND_MAX>>2)+1);
+ switch(dec_method){
+ case 0:{
+ fs=ec_decode(&dec,1<<logp1[j]);
+ sym=fs>=(1<<logp1[j])-1;
+ ec_dec_update(&dec,sym?(1<<logp1[j])-1:0,
+ (1<<logp1[j])-(sym?0:1),1<<logp1[j]);
+ }break;
+ case 1:{
+ fs=ec_decode_bin(&dec,logp1[j]);
+ sym=fs>=(1<<logp1[j])-1;
+ ec_dec_update(&dec,sym?(1<<logp1[j])-1:0,
+ (1<<logp1[j])-(sym?0:1),1<<logp1[j]);
+ }break;
+ case 2:{
+ sym=ec_dec_bit_logp(&dec,logp1[j]);
+ }break;
+ case 3:{
+ unsigned char icdf[2];
+ icdf[0]=1;
+ icdf[1]=0;
+ sym=ec_dec_icdf(&dec,icdf,logp1[j]);
+ }break;
+ }
+ if(sym!=data[j]){
+ fprintf(stderr,
+ "Decoded %i instead of %i with logp1 of %i at position %i of %i (Random seed: %u).\n",
+ sym,data[j],logp1[j],j,sz,seed);
+ fprintf(stderr,"Encoding method: %i, decoding method: %i\n",
+ enc_method[j],dec_method);
+ ret=-1;
+ }
+ if(ec_tell_frac(&dec)!=tell[j+1]){
+ fprintf(stderr,
+ "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
+ j+1,ec_tell_frac(&dec),tell[j+1],seed);
+ }
+ }
+ free(enc_method);
+ free(tell);
+ free(data);
+ free(logp1);
+ }
+ free(ptr);
+ return ret;
+}
--- /dev/null
+++ b/celt/tests/test_unit_laplace.c
@@ -1,0 +1,65 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "laplace.h"
+#define CELT_C
+#include "stack_alloc.h"
+
+#include "entenc.c"
+#include "entdec.c"
+#include "entcode.c"
+#include "laplace.c"
+
+#define DATA_SIZE 40000
+
+int ec_laplace_get_start_freq(int decay)
+{
+ opus_uint32 ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN+1);
+ int fs = (ft*(16384-decay))/(16384+decay);
+ return fs+LAPLACE_MINP;
+}
+
+int main(void)
+{
+ int i;
+ int ret = 0;
+ ec_enc enc;
+ ec_dec dec;
+ unsigned char *ptr;
+ int val[10000], decay[10000];
+ ALLOC_STACK;
+ ptr = (unsigned char *)malloc(DATA_SIZE);
+ ec_enc_init(&enc,ptr,DATA_SIZE);
+
+ val[0] = 3; decay[0] = 6000;
+ val[1] = 0; decay[1] = 5800;
+ val[2] = -1; decay[2] = 5600;
+ for (i=3;i<10000;i++)
+ {
+ val[i] = rand()%15-7;
+ decay[i] = rand()%11000+5000;
+ }
+ for (i=0;i<10000;i++)
+ ec_laplace_encode(&enc, &val[i],
+ ec_laplace_get_start_freq(decay[i]), decay[i]);
+
+ ec_enc_done(&enc);
+
+ ec_dec_init(&dec,ec_get_buffer(&enc),ec_range_bytes(&enc));
+
+ for (i=0;i<10000;i++)
+ {
+ int d = ec_laplace_decode(&dec,
+ ec_laplace_get_start_freq(decay[i]), decay[i]);
+ if (d != val[i])
+ {
+ fprintf (stderr, "Got %d instead of %d\n", d, val[i]);
+ ret = 1;
+ }
+ }
+
+ return ret;
+}
--- /dev/null
+++ b/celt/tests/test_unit_mathops.c
@@ -1,0 +1,238 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifndef CUSTOM_MODES
+#define CUSTOM_MODES
+#endif
+
+#define CELT_C
+
+#include "mathops.c"
+#include "entenc.c"
+#include "entdec.c"
+#include "entcode.c"
+#include "bands.c"
+#include "vq.c"
+#include "cwrs.c"
+#include <stdio.h>
+#include <math.h>
+
+#ifdef FIXED_POINT
+#define WORD "%d"
+#else
+#define WORD "%f"
+#endif
+
+#ifdef FIXED_DEBUG
+long long celt_mips=0;
+#endif
+int ret = 0;
+
+void testdiv(void)
+{
+ opus_int32 i;
+ for (i=1;i<=327670;i++)
+ {
+ double prod;
+ opus_val32 val;
+ val = celt_rcp(i);
+#ifdef FIXED_POINT
+ prod = (1./32768./65526.)*val*i;
+#else
+ prod = val*i;
+#endif
+ if (fabs(prod-1) > .00025)
+ {
+ fprintf (stderr, "div failed: 1/%d="WORD" (product = %f)\n", i, val, prod);
+ ret = 1;
+ }
+ }
+}
+
+void testsqrt(void)
+{
+ opus_int32 i;
+ for (i=1;i<=1000000000;i++)
+ {
+ double ratio;
+ opus_val16 val;
+ val = celt_sqrt(i);
+ ratio = val/sqrt(i);
+ if (fabs(ratio - 1) > .0005 && fabs(val-sqrt(i)) > 2)
+ {
+ fprintf (stderr, "sqrt failed: sqrt(%d)="WORD" (ratio = %f)\n", i, val, ratio);
+ ret = 1;
+ }
+ i+= i>>10;
+ }
+}
+
+void testbitexactcos(void)
+{
+ int i;
+ opus_int32 min_d,max_d,last,chk;
+ chk=max_d=0;
+ last=min_d=32767;
+ for(i=64;i<=16320;i++)
+ {
+ opus_int32 d;
+ opus_int32 q=bitexact_cos(i);
+ chk ^= q*i;
+ d = last - q;
+ if (d>max_d)max_d=d;
+ if (d<min_d)min_d=d;
+ last = q;
+ }
+ if ((chk!=89408644)||(max_d!=5)||(min_d!=0)||(bitexact_cos(64)!=32767)||
+ (bitexact_cos(16320)!=200)||(bitexact_cos(8192)!=23171))
+ {
+ fprintf (stderr, "bitexact_cos failed\n");
+ ret = 1;
+ }
+}
+
+void testbitexactlog2tan(void)
+{
+ int i,fail;
+ opus_int32 min_d,max_d,last,chk;
+ fail=chk=max_d=0;
+ last=min_d=15059;
+ for(i=64;i<8193;i++)
+ {
+ opus_int32 d;
+ opus_int32 mid=bitexact_cos(i);
+ opus_int32 side=bitexact_cos(16384-i);
+ opus_int32 q=bitexact_log2tan(mid,side);
+ chk ^= q*i;
+ d = last - q;
+ if (q!=-1*bitexact_log2tan(side,mid))
+ fail = 1;
+ if (d>max_d)max_d=d;
+ if (d<min_d)min_d=d;
+ last = q;
+ }
+ if ((chk!=15821257)||(max_d!=61)||(min_d!=-2)||fail||
+ (bitexact_log2tan(32767,200)!=15059)||(bitexact_log2tan(30274,12540)!=2611)||
+ (bitexact_log2tan(23171,23171)!=0))
+ {
+ fprintf (stderr, "bitexact_log2tan failed\n");
+ ret = 1;
+ }
+}
+
+#ifndef FIXED_POINT
+void testlog2(void)
+{
+ float x;
+ for (x=0.001;x<1677700.0;x+=(x/8.0))
+ {
+ float error = fabs((1.442695040888963387*log(x))-celt_log2(x));
+ if (error>0.0009)
+ {
+ fprintf (stderr, "celt_log2 failed: fabs((1.442695040888963387*log(x))-celt_log2(x))>0.001 (x = %f, error = %f)\n", x,error);
+ ret = 1;
+ }
+ }
+}
+
+void testexp2(void)
+{
+ float x;
+ for (x=-11.0;x<24.0;x+=0.0007)
+ {
+ float error = fabs(x-(1.442695040888963387*log(celt_exp2(x))));
+ if (error>0.0002)
+ {
+ fprintf (stderr, "celt_exp2 failed: fabs(x-(1.442695040888963387*log(celt_exp2(x))))>0.0005 (x = %f, error = %f)\n", x,error);
+ ret = 1;
+ }
+ }
+}
+
+void testexp2log2(void)
+{
+ float x;
+ for (x=-11.0;x<24.0;x+=0.0007)
+ {
+ float error = fabs(x-(celt_log2(celt_exp2(x))));
+ if (error>0.001)
+ {
+ fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_log2(celt_exp2(x))))>0.001 (x = %f, error = %f)\n", x,error);
+ ret = 1;
+ }
+ }
+}
+#else
+void testlog2(void)
+{
+ opus_val32 x;
+ for (x=8;x<1073741824;x+=(x>>3))
+ {
+ float error = fabs((1.442695040888963387*log(x/16384.0))-celt_log2(x)/1024.0);
+ if (error>0.003)
+ {
+ fprintf (stderr, "celt_log2 failed: x = %ld, error = %f\n", (long)x,error);
+ ret = 1;
+ }
+ }
+}
+
+void testexp2(void)
+{
+ opus_val16 x;
+ for (x=-32768;x<15360;x++)
+ {
+ float error1 = fabs(x/1024.0-(1.442695040888963387*log(celt_exp2(x)/65536.0)));
+ float error2 = fabs(exp(0.6931471805599453094*x/1024.0)-celt_exp2(x)/65536.0);
+ if (error1>0.0002&&error2>0.00004)
+ {
+ fprintf (stderr, "celt_exp2 failed: x = "WORD", error1 = %f, error2 = %f\n", x,error1,error2);
+ ret = 1;
+ }
+ }
+}
+
+void testexp2log2(void)
+{
+ opus_val32 x;
+ for (x=8;x<65536;x+=(x>>3))
+ {
+ float error = fabs(x-0.25*celt_exp2(celt_log2(x)))/16384;
+ if (error>0.004)
+ {
+ fprintf (stderr, "celt_log2/celt_exp2 failed: fabs(x-(celt_exp2(celt_log2(x))))>0.001 (x = %ld, error = %f)\n", (long)x,error);
+ ret = 1;
+ }
+ }
+}
+
+void testilog2(void)
+{
+ opus_val32 x;
+ for (x=1;x<=268435455;x+=127)
+ {
+ opus_val32 error = abs(celt_ilog2(x)-(int)floor(log2(x)));
+ if (error!=0)
+ {
+ printf("celt_ilog2 failed: celt_ilog2(x)!=floor(log2(x)) (x = %d, error = %d)\n",x,error);
+ ret = 1;
+ }
+ }
+}
+#endif
+
+int main(void)
+{
+ testbitexactcos();
+ testbitexactlog2tan();
+ testdiv();
+ testsqrt();
+ testlog2();
+ testexp2();
+ testexp2log2();
+#ifdef FIXED_POINT
+ testilog2();
+#endif
+ return ret;
+}
--- /dev/null
+++ b/celt/tests/test_unit_mdct.c
@@ -1,0 +1,170 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#define SKIP_CONFIG_H
+
+#ifndef CUSTOM_MODES
+#define CUSTOM_MODES
+#endif
+
+#include <stdio.h>
+#include "mdct.h"
+#define CELT_C
+#include "stack_alloc.h"
+
+#include "kiss_fft.c"
+#include "mdct.c"
+#include "mathops.c"
+#include "entcode.c"
+
+#ifndef M_PI
+#define M_PI 3.141592653
+#endif
+
+#ifdef FIXED_DEBUG
+long long celt_mips=0;
+#endif
+int ret = 0;
+void check(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
+{
+ int bin,k;
+ double errpow=0,sigpow=0;
+ double snr;
+ for (bin=0;bin<nfft/2;++bin) {
+ double ansr = 0;
+ double difr;
+
+ for (k=0;k<nfft;++k) {
+ double phase = 2*M_PI*(k+.5+.25*nfft)*(bin+.5)/nfft;
+ double re = cos(phase);
+
+ re /= nfft/4;
+
+ ansr += in[k] * re;
+ }
+ /*printf ("%f %f\n", ansr, out[bin]);*/
+ difr = ansr - out[bin];
+ errpow += difr*difr;
+ sigpow += ansr*ansr;
+ }
+ snr = 10*log10(sigpow/errpow);
+ printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
+ if (snr<60) {
+ printf( "** poor snr: %f **\n", snr);
+ ret = 1;
+ }
+}
+
+void check_inv(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
+{
+ int bin,k;
+ double errpow=0,sigpow=0;
+ double snr;
+ for (bin=0;bin<nfft;++bin) {
+ double ansr = 0;
+ double difr;
+
+ for (k=0;k<nfft/2;++k) {
+ double phase = 2*M_PI*(bin+.5+.25*nfft)*(k+.5)/nfft;
+ double re = cos(phase);
+
+ /*re *= 2;*/
+
+ ansr += in[k] * re;
+ }
+ /*printf ("%f %f\n", ansr, out[bin]);*/
+ difr = ansr - out[bin];
+ errpow += difr*difr;
+ sigpow += ansr*ansr;
+ }
+ snr = 10*log10(sigpow/errpow);
+ printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
+ if (snr<60) {
+ printf( "** poor snr: %f **\n", snr);
+ ret = 1;
+ }
+}
+
+
+void test1d(int nfft,int isinverse)
+{
+ mdct_lookup cfg;
+ size_t buflen = sizeof(kiss_fft_scalar)*nfft;
+
+ kiss_fft_scalar * in = (kiss_fft_scalar*)malloc(buflen);
+ kiss_fft_scalar * in_copy = (kiss_fft_scalar*)malloc(buflen);
+ kiss_fft_scalar * out= (kiss_fft_scalar*)malloc(buflen);
+ opus_val16 * window= (opus_val16*)malloc(sizeof(opus_val16)*nfft/2);
+ int k;
+
+ clt_mdct_init(&cfg, nfft, 0);
+ for (k=0;k<nfft;++k) {
+ in[k] = (rand() % 32768) - 16384;
+ }
+
+ for (k=0;k<nfft/2;++k) {
+ window[k] = Q15ONE;
+ }
+ for (k=0;k<nfft;++k) {
+ in[k] *= 32768;
+ }
+
+ if (isinverse)
+ {
+ for (k=0;k<nfft;++k) {
+ in[k] /= nfft;
+ }
+ }
+
+ for (k=0;k<nfft;++k)
+ in_copy[k] = in[k];
+ /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/
+
+ if (isinverse)
+ {
+ for (k=0;k<nfft;++k)
+ out[k] = 0;
+ clt_mdct_backward(&cfg,in,out, window, nfft/2, 0, 1);
+ check_inv(in,out,nfft,isinverse);
+ } else {
+ clt_mdct_forward(&cfg,in,out,window, nfft/2, 0, 1);
+ check(in_copy,out,nfft,isinverse);
+ }
+ /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/
+
+
+ free(in);
+ free(out);
+ clt_mdct_clear(&cfg);
+}
+
+int main(int argc,char ** argv)
+{
+ ALLOC_STACK;
+ if (argc>1) {
+ int k;
+ for (k=1;k<argc;++k) {
+ test1d(atoi(argv[k]),0);
+ test1d(atoi(argv[k]),1);
+ }
+ }else{
+ test1d(32,0);
+ test1d(32,1);
+ test1d(256,0);
+ test1d(256,1);
+ test1d(512,0);
+ test1d(512,1);
+#ifndef RADIX_TWO_ONLY
+ test1d(40,0);
+ test1d(40,1);
+ test1d(120,0);
+ test1d(120,1);
+ test1d(240,0);
+ test1d(240,1);
+ test1d(480,0);
+ test1d(480,1);
+#endif
+ }
+ return ret;
+}
--- /dev/null
+++ b/celt/tests/test_unit_rotation.c
@@ -1,0 +1,63 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifndef CUSTOM_MODES
+#define CUSTOM_MODES
+#endif
+
+#define CELT_C
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "vq.c"
+#include "cwrs.c"
+#include "entcode.c"
+#include "entenc.c"
+#include "entdec.c"
+#include "mathops.c"
+#include "bands.h"
+#include <math.h>
+#define MAX_SIZE 100
+
+int ret=0;
+void test_rotation(int N, int K)
+{
+ int i;
+ double err = 0, ener = 0, snr, snr0;
+ opus_val16 x0[MAX_SIZE];
+ opus_val16 x1[MAX_SIZE];
+ for (i=0;i<N;i++)
+ x1[i] = x0[i] = rand()%32767-16384;
+ exp_rotation(x1, N, 1, 1, K, SPREAD_NORMAL);
+ for (i=0;i<N;i++)
+ {
+ err += (x0[i]-(double)x1[i])*(x0[i]-(double)x1[i]);
+ ener += x0[i]*(double)x0[i];
+ }
+ snr0 = 20*log10(ener/err);
+ err = ener = 0;
+ exp_rotation(x1, N, -1, 1, K, SPREAD_NORMAL);
+ for (i=0;i<N;i++)
+ {
+ err += (x0[i]-(double)x1[i])*(x0[i]-(double)x1[i]);
+ ener += x0[i]*(double)x0[i];
+ }
+ snr = 20*log10(ener/err);
+ printf ("SNR for size %d (%d pulses) is %f (was %f without inverse)\n", N, K, snr, snr0);
+ if (snr < 60 || snr0 > 20)
+ {
+ fprintf(stderr, "FAIL!\n");
+ ret = 1;
+ }
+}
+
+int main(void)
+{
+ ALLOC_STACK;
+ test_rotation(15, 3);
+ test_rotation(23, 5);
+ test_rotation(50, 3);
+ test_rotation(80, 1);
+ return ret;
+}
--- /dev/null
+++ b/celt/tests/test_unit_types.c
@@ -1,0 +1,23 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "opus_types.h"
+#include <stdio.h>
+
+int main(void)
+{
+ opus_int16 i = 1;
+ i <<= 14;
+ if (i>>14 != 1)
+ {
+ fprintf(stderr, "opus_int16 isn't 16 bits\n");
+ return 1;
+ }
+ if (sizeof(opus_int16)*2 != sizeof(opus_int32))
+ {
+ fprintf(stderr, "16*2 != 32\n");
+ return 1;
+ }
+ return 0;
+}
--- a/celt/tests/type-test.c
+++ /dev/null
@@ -1,23 +1,0 @@
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include "opus_types.h"
-#include <stdio.h>
-
-int main(void)
-{
- opus_int16 i = 1;
- i <<= 14;
- if (i>>14 != 1)
- {
- fprintf(stderr, "opus_int16 isn't 16 bits\n");
- return 1;
- }
- if (sizeof(opus_int16)*2 != sizeof(opus_int32))
- {
- fprintf(stderr, "16*2 != 32\n");
- return 1;
- }
- return 0;
-}
--- a/configure.ac
+++ b/configure.ac
@@ -227,8 +227,7 @@
AM_CONDITIONAL([FIXED_POINT], [test x$ac_enable_fixed = xyes])
AM_CONDITIONAL([CUSTOM_MODES], [test x$ac_enable_custom_modes = xyes])
-AC_OUTPUT([Makefile celt/tests/Makefile
- opus.pc opus-uninstalled.pc
+AC_OUTPUT([Makefile opus.pc opus-uninstalled.pc
doc/Makefile doc/Doxyfile])
AC_MSG_RESULT([