test_unit_laplace.c 2.7 KB

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