• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2008-2011 Xiph.Org Foundation, Mozilla Corporation
2    Written by Jean-Marc Valin and Timothy B. Terriberry */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "laplace.h"
35 #define CELT_C
36 #include "stack_alloc.h"
37 
38 #include "entenc.c"
39 #include "entdec.c"
40 #include "entcode.c"
41 #include "laplace.c"
42 
43 #define DATA_SIZE 40000
44 
ec_laplace_get_start_freq(int decay)45 int ec_laplace_get_start_freq(int decay)
46 {
47    opus_uint32 ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN+1);
48    int fs = (ft*(16384-decay))/(16384+decay);
49    return fs+LAPLACE_MINP;
50 }
51 
main(void)52 int main(void)
53 {
54    int i;
55    int ret = 0;
56    ec_enc enc;
57    ec_dec dec;
58    unsigned char *ptr;
59    int val[10000], decay[10000];
60    ALLOC_STACK;
61    ptr = (unsigned char *)malloc(DATA_SIZE);
62    ec_enc_init(&enc,ptr,DATA_SIZE);
63 
64    val[0] = 3; decay[0] = 6000;
65    val[1] = 0; decay[1] = 5800;
66    val[2] = -1; decay[2] = 5600;
67    for (i=3;i<10000;i++)
68    {
69       val[i] = rand()%15-7;
70       decay[i] = rand()%11000+5000;
71    }
72    for (i=0;i<10000;i++)
73       ec_laplace_encode(&enc, &val[i],
74             ec_laplace_get_start_freq(decay[i]), decay[i]);
75 
76    ec_enc_done(&enc);
77 
78    ec_dec_init(&dec,ec_get_buffer(&enc),ec_range_bytes(&enc));
79 
80    for (i=0;i<10000;i++)
81    {
82       int d = ec_laplace_decode(&dec,
83             ec_laplace_get_start_freq(decay[i]), decay[i]);
84       if (d != val[i])
85       {
86          fprintf (stderr, "Got %d instead of %d\n", d, val[i]);
87          ret = 1;
88       }
89    }
90 
91    return ret;
92 }
93