* #30460: added opus dep
diff --git a/jni/libopus/tests/.deps/.dirstamp b/jni/libopus/tests/.deps/.dirstamp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/jni/libopus/tests/.deps/.dirstamp
diff --git a/jni/libopus/tests/.deps/test_opus_api.Po b/jni/libopus/tests/.deps/test_opus_api.Po
new file mode 100644
index 0000000..ca7f1b4
--- /dev/null
+++ b/jni/libopus/tests/.deps/test_opus_api.Po
@@ -0,0 +1,129 @@
+tests/test_opus_api.o: tests/test_opus_api.c /usr/include/stdc-predef.h \
+ config.h /usr/include/stdio.h /usr/include/features.h \
+ /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h \
+ /usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stddef.h \
+ /usr/include/bits/types.h /usr/include/bits/typesizes.h \
+ /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdarg.h \
+ /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h \
+ /usr/include/bits/stdio.h /usr/include/stdlib.h \
+ /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h \
+ /usr/include/endian.h /usr/include/bits/endian.h \
+ /usr/include/bits/byteswap.h /usr/include/bits/byteswap-16.h \
+ /usr/include/sys/types.h /usr/include/time.h /usr/include/sys/select.h \
+ /usr/include/bits/select.h /usr/include/bits/sigset.h \
+ /usr/include/bits/time.h /usr/include/sys/sysmacros.h \
+ /usr/include/bits/pthreadtypes.h /usr/include/alloca.h \
+ /usr/include/bits/stdlib-float.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdint.h \
+ /usr/include/stdint.h /usr/include/bits/wchar.h /usr/include/string.h \
+ /usr/include/xlocale.h /usr/include/bits/string.h \
+ /usr/include/bits/string2.h celt/arch.h include/opus_types.h \
+ include/opus_multistream.h include/opus.h include/opus_types.h \
+ include/opus_defines.h include/opus.h tests/test_opus_common.h \
+ celt/os_support.h /usr/include/malloc.h
+
+/usr/include/stdc-predef.h:
+
+config.h:
+
+/usr/include/stdio.h:
+
+/usr/include/features.h:
+
+/usr/include/sys/cdefs.h:
+
+/usr/include/bits/wordsize.h:
+
+/usr/include/gnu/stubs.h:
+
+/usr/include/gnu/stubs-64.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stddef.h:
+
+/usr/include/bits/types.h:
+
+/usr/include/bits/typesizes.h:
+
+/usr/include/libio.h:
+
+/usr/include/_G_config.h:
+
+/usr/include/wchar.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdarg.h:
+
+/usr/include/bits/stdio_lim.h:
+
+/usr/include/bits/sys_errlist.h:
+
+/usr/include/bits/stdio.h:
+
+/usr/include/stdlib.h:
+
+/usr/include/bits/waitflags.h:
+
+/usr/include/bits/waitstatus.h:
+
+/usr/include/endian.h:
+
+/usr/include/bits/endian.h:
+
+/usr/include/bits/byteswap.h:
+
+/usr/include/bits/byteswap-16.h:
+
+/usr/include/sys/types.h:
+
+/usr/include/time.h:
+
+/usr/include/sys/select.h:
+
+/usr/include/bits/select.h:
+
+/usr/include/bits/sigset.h:
+
+/usr/include/bits/time.h:
+
+/usr/include/sys/sysmacros.h:
+
+/usr/include/bits/pthreadtypes.h:
+
+/usr/include/alloca.h:
+
+/usr/include/bits/stdlib-float.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdint.h:
+
+/usr/include/stdint.h:
+
+/usr/include/bits/wchar.h:
+
+/usr/include/string.h:
+
+/usr/include/xlocale.h:
+
+/usr/include/bits/string.h:
+
+/usr/include/bits/string2.h:
+
+celt/arch.h:
+
+include/opus_types.h:
+
+include/opus_multistream.h:
+
+include/opus.h:
+
+include/opus_types.h:
+
+include/opus_defines.h:
+
+include/opus.h:
+
+tests/test_opus_common.h:
+
+celt/os_support.h:
+
+/usr/include/malloc.h:
diff --git a/jni/libopus/tests/.deps/test_opus_decode.Po b/jni/libopus/tests/.deps/test_opus_decode.Po
new file mode 100644
index 0000000..b5aba11
--- /dev/null
+++ b/jni/libopus/tests/.deps/test_opus_decode.Po
@@ -0,0 +1,169 @@
+tests/test_opus_decode.o: tests/test_opus_decode.c \
+ /usr/include/stdc-predef.h config.h /usr/include/stdio.h \
+ /usr/include/features.h /usr/include/sys/cdefs.h \
+ /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h \
+ /usr/include/gnu/stubs-64.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stddef.h \
+ /usr/include/bits/types.h /usr/include/bits/typesizes.h \
+ /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdarg.h \
+ /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h \
+ /usr/include/bits/stdio.h /usr/include/stdlib.h \
+ /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h \
+ /usr/include/endian.h /usr/include/bits/endian.h \
+ /usr/include/bits/byteswap.h /usr/include/bits/byteswap-16.h \
+ /usr/include/sys/types.h /usr/include/time.h /usr/include/sys/select.h \
+ /usr/include/bits/select.h /usr/include/bits/sigset.h \
+ /usr/include/bits/time.h /usr/include/sys/sysmacros.h \
+ /usr/include/bits/pthreadtypes.h /usr/include/alloca.h \
+ /usr/include/bits/stdlib-float.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/limits.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/syslimits.h \
+ /usr/include/limits.h /usr/include/bits/posix1_lim.h \
+ /usr/include/bits/local_lim.h /usr/include/linux/limits.h \
+ /usr/include/bits/posix2_lim.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdint.h \
+ /usr/include/stdint.h /usr/include/bits/wchar.h /usr/include/math.h \
+ /usr/include/bits/huge_val.h /usr/include/bits/huge_valf.h \
+ /usr/include/bits/huge_vall.h /usr/include/bits/inf.h \
+ /usr/include/bits/nan.h /usr/include/bits/mathdef.h \
+ /usr/include/bits/mathcalls.h /usr/include/bits/mathinline.h \
+ /usr/include/string.h /usr/include/xlocale.h /usr/include/bits/string.h \
+ /usr/include/bits/string2.h /usr/include/unistd.h \
+ /usr/include/bits/posix_opt.h /usr/include/bits/environments.h \
+ /usr/include/bits/confname.h /usr/include/getopt.h include/opus.h \
+ include/opus_types.h include/opus_defines.h tests/test_opus_common.h
+
+/usr/include/stdc-predef.h:
+
+config.h:
+
+/usr/include/stdio.h:
+
+/usr/include/features.h:
+
+/usr/include/sys/cdefs.h:
+
+/usr/include/bits/wordsize.h:
+
+/usr/include/gnu/stubs.h:
+
+/usr/include/gnu/stubs-64.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stddef.h:
+
+/usr/include/bits/types.h:
+
+/usr/include/bits/typesizes.h:
+
+/usr/include/libio.h:
+
+/usr/include/_G_config.h:
+
+/usr/include/wchar.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdarg.h:
+
+/usr/include/bits/stdio_lim.h:
+
+/usr/include/bits/sys_errlist.h:
+
+/usr/include/bits/stdio.h:
+
+/usr/include/stdlib.h:
+
+/usr/include/bits/waitflags.h:
+
+/usr/include/bits/waitstatus.h:
+
+/usr/include/endian.h:
+
+/usr/include/bits/endian.h:
+
+/usr/include/bits/byteswap.h:
+
+/usr/include/bits/byteswap-16.h:
+
+/usr/include/sys/types.h:
+
+/usr/include/time.h:
+
+/usr/include/sys/select.h:
+
+/usr/include/bits/select.h:
+
+/usr/include/bits/sigset.h:
+
+/usr/include/bits/time.h:
+
+/usr/include/sys/sysmacros.h:
+
+/usr/include/bits/pthreadtypes.h:
+
+/usr/include/alloca.h:
+
+/usr/include/bits/stdlib-float.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/limits.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/syslimits.h:
+
+/usr/include/limits.h:
+
+/usr/include/bits/posix1_lim.h:
+
+/usr/include/bits/local_lim.h:
+
+/usr/include/linux/limits.h:
+
+/usr/include/bits/posix2_lim.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdint.h:
+
+/usr/include/stdint.h:
+
+/usr/include/bits/wchar.h:
+
+/usr/include/math.h:
+
+/usr/include/bits/huge_val.h:
+
+/usr/include/bits/huge_valf.h:
+
+/usr/include/bits/huge_vall.h:
+
+/usr/include/bits/inf.h:
+
+/usr/include/bits/nan.h:
+
+/usr/include/bits/mathdef.h:
+
+/usr/include/bits/mathcalls.h:
+
+/usr/include/bits/mathinline.h:
+
+/usr/include/string.h:
+
+/usr/include/xlocale.h:
+
+/usr/include/bits/string.h:
+
+/usr/include/bits/string2.h:
+
+/usr/include/unistd.h:
+
+/usr/include/bits/posix_opt.h:
+
+/usr/include/bits/environments.h:
+
+/usr/include/bits/confname.h:
+
+/usr/include/getopt.h:
+
+include/opus.h:
+
+include/opus_types.h:
+
+include/opus_defines.h:
+
+tests/test_opus_common.h:
diff --git a/jni/libopus/tests/.deps/test_opus_encode.Po b/jni/libopus/tests/.deps/test_opus_encode.Po
new file mode 100644
index 0000000..09d1480
--- /dev/null
+++ b/jni/libopus/tests/.deps/test_opus_encode.Po
@@ -0,0 +1,181 @@
+tests/test_opus_encode.o: tests/test_opus_encode.c \
+ /usr/include/stdc-predef.h config.h /usr/include/stdio.h \
+ /usr/include/features.h /usr/include/sys/cdefs.h \
+ /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h \
+ /usr/include/gnu/stubs-64.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stddef.h \
+ /usr/include/bits/types.h /usr/include/bits/typesizes.h \
+ /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdarg.h \
+ /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h \
+ /usr/include/bits/stdio.h /usr/include/stdlib.h \
+ /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h \
+ /usr/include/endian.h /usr/include/bits/endian.h \
+ /usr/include/bits/byteswap.h /usr/include/bits/byteswap-16.h \
+ /usr/include/sys/types.h /usr/include/time.h /usr/include/sys/select.h \
+ /usr/include/bits/select.h /usr/include/bits/sigset.h \
+ /usr/include/bits/time.h /usr/include/sys/sysmacros.h \
+ /usr/include/bits/pthreadtypes.h /usr/include/alloca.h \
+ /usr/include/bits/stdlib-float.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/limits.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/syslimits.h \
+ /usr/include/limits.h /usr/include/bits/posix1_lim.h \
+ /usr/include/bits/local_lim.h /usr/include/linux/limits.h \
+ /usr/include/bits/posix2_lim.h \
+ /usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdint.h \
+ /usr/include/stdint.h /usr/include/bits/wchar.h /usr/include/math.h \
+ /usr/include/bits/huge_val.h /usr/include/bits/huge_valf.h \
+ /usr/include/bits/huge_vall.h /usr/include/bits/inf.h \
+ /usr/include/bits/nan.h /usr/include/bits/mathdef.h \
+ /usr/include/bits/mathcalls.h /usr/include/bits/mathinline.h \
+ /usr/include/string.h /usr/include/xlocale.h /usr/include/bits/string.h \
+ /usr/include/bits/string2.h /usr/include/unistd.h \
+ /usr/include/bits/posix_opt.h /usr/include/bits/environments.h \
+ /usr/include/bits/confname.h /usr/include/getopt.h \
+ include/opus_multistream.h include/opus.h include/opus_types.h \
+ include/opus_defines.h include/opus.h tests/../src/opus_private.h \
+ celt/arch.h include/opus_types.h tests/test_opus_common.h
+
+/usr/include/stdc-predef.h:
+
+config.h:
+
+/usr/include/stdio.h:
+
+/usr/include/features.h:
+
+/usr/include/sys/cdefs.h:
+
+/usr/include/bits/wordsize.h:
+
+/usr/include/gnu/stubs.h:
+
+/usr/include/gnu/stubs-64.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stddef.h:
+
+/usr/include/bits/types.h:
+
+/usr/include/bits/typesizes.h:
+
+/usr/include/libio.h:
+
+/usr/include/_G_config.h:
+
+/usr/include/wchar.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdarg.h:
+
+/usr/include/bits/stdio_lim.h:
+
+/usr/include/bits/sys_errlist.h:
+
+/usr/include/bits/stdio.h:
+
+/usr/include/stdlib.h:
+
+/usr/include/bits/waitflags.h:
+
+/usr/include/bits/waitstatus.h:
+
+/usr/include/endian.h:
+
+/usr/include/bits/endian.h:
+
+/usr/include/bits/byteswap.h:
+
+/usr/include/bits/byteswap-16.h:
+
+/usr/include/sys/types.h:
+
+/usr/include/time.h:
+
+/usr/include/sys/select.h:
+
+/usr/include/bits/select.h:
+
+/usr/include/bits/sigset.h:
+
+/usr/include/bits/time.h:
+
+/usr/include/sys/sysmacros.h:
+
+/usr/include/bits/pthreadtypes.h:
+
+/usr/include/alloca.h:
+
+/usr/include/bits/stdlib-float.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/limits.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/syslimits.h:
+
+/usr/include/limits.h:
+
+/usr/include/bits/posix1_lim.h:
+
+/usr/include/bits/local_lim.h:
+
+/usr/include/linux/limits.h:
+
+/usr/include/bits/posix2_lim.h:
+
+/usr/lib/gcc/x86_64-redhat-linux/4.8.1/include/stdint.h:
+
+/usr/include/stdint.h:
+
+/usr/include/bits/wchar.h:
+
+/usr/include/math.h:
+
+/usr/include/bits/huge_val.h:
+
+/usr/include/bits/huge_valf.h:
+
+/usr/include/bits/huge_vall.h:
+
+/usr/include/bits/inf.h:
+
+/usr/include/bits/nan.h:
+
+/usr/include/bits/mathdef.h:
+
+/usr/include/bits/mathcalls.h:
+
+/usr/include/bits/mathinline.h:
+
+/usr/include/string.h:
+
+/usr/include/xlocale.h:
+
+/usr/include/bits/string.h:
+
+/usr/include/bits/string2.h:
+
+/usr/include/unistd.h:
+
+/usr/include/bits/posix_opt.h:
+
+/usr/include/bits/environments.h:
+
+/usr/include/bits/confname.h:
+
+/usr/include/getopt.h:
+
+include/opus_multistream.h:
+
+include/opus.h:
+
+include/opus_types.h:
+
+include/opus_defines.h:
+
+include/opus.h:
+
+tests/../src/opus_private.h:
+
+celt/arch.h:
+
+include/opus_types.h:
+
+tests/test_opus_common.h:
diff --git a/jni/libopus/tests/.dirstamp b/jni/libopus/tests/.dirstamp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/jni/libopus/tests/.dirstamp
diff --git a/jni/libopus/tests/run_vectors.sh b/jni/libopus/tests/run_vectors.sh
new file mode 100755
index 0000000..1cc445d
--- /dev/null
+++ b/jni/libopus/tests/run_vectors.sh
@@ -0,0 +1,129 @@
+#!/bin/sh
+
+# Copyright (c) 2011-2012 Jean-Marc Valin
+#
+#  This file is extracted from RFC6716. Please see that RFC for additional
+#  information.
+#
+#  Redistribution and use in source and binary forms, with or without
+#  modification, are permitted provided that the following conditions
+#  are met:
+#
+#  - Redistributions of source code must retain the above copyright
+#  notice, this list of conditions and the following disclaimer.
+#
+#  - Redistributions in binary form must reproduce the above copyright
+#  notice, this list of conditions and the following disclaimer in the
+#  documentation and/or other materials provided with the distribution.
+#
+#  - Neither the name of Internet Society, IETF or IETF Trust, nor the
+#  names of specific contributors, may be used to endorse or promote
+#  products derived from this software without specific prior written
+#  permission.
+#
+#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+#  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+#  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+rm logs_mono.txt
+rm logs_stereo.txt
+
+if [ "$#" -ne "3" ]; then
+    echo "usage: run_vectors.sh <exec path> <vector path> <rate>"
+    exit 1
+fi
+
+CMD_PATH=$1
+VECTOR_PATH=$2
+RATE=$3
+
+OPUS_DEMO=$CMD_PATH/opus_demo
+OPUS_COMPARE=$CMD_PATH/opus_compare
+
+if [ -d $VECTOR_PATH ]; then
+    echo Test vectors found in $VECTOR_PATH
+else
+    echo No test vectors found
+    #Don't make the test fail here because the test vectors
+    #will be distributed separately
+    exit 0
+fi
+
+if [ -x $OPUS_DEMO ]; then
+    echo Decoding with $OPUS_DEMO
+else
+    echo ERROR: Decoder not found: $OPUS_DEMO
+    exit 1
+fi
+
+echo "=============="
+echo Testing mono
+echo "=============="
+echo
+
+for file in 01 02 03 04 05 06 07 08 09 10 11 12
+do
+    if [ -e $VECTOR_PATH/testvector$file.bit ]; then
+        echo Testing testvector$file
+    else
+        echo Bitstream file not found: testvector$file.bit
+    fi
+    if $OPUS_DEMO -d $RATE 1 $VECTOR_PATH/testvector$file.bit tmp.out >> logs_mono.txt 2>&1; then
+        echo successfully decoded
+    else
+        echo ERROR: decoding failed
+        exit 1
+    fi
+    $OPUS_COMPARE -r $RATE $VECTOR_PATH/testvector$file.dec tmp.out >> logs_mono.txt 2>&1
+    float_ret=$?
+    if [ "$float_ret" -eq "0" ]; then
+        echo output matches reference
+    else
+        echo ERROR: output does not match reference
+        exit 1
+    fi
+    echo
+done
+
+echo "=============="
+echo Testing stereo
+echo "=============="
+echo
+
+for file in 01 02 03 04 05 06 07 08 09 10 11 12
+do
+    if [ -e $VECTOR_PATH/testvector$file.bit ]; then
+        echo Testing testvector$file
+    else
+        echo Bitstream file not found: testvector$file
+    fi
+    if $OPUS_DEMO -d $RATE 2 $VECTOR_PATH/testvector$file.bit tmp.out >> logs_stereo.txt 2>&1; then
+        echo successfully decoded
+    else
+        echo ERROR: decoding failed
+        exit 1
+    fi
+    $OPUS_COMPARE -s -r $RATE $VECTOR_PATH/testvector$file.dec tmp.out >> logs_stereo.txt 2>&1
+    float_ret=$?
+    if [ "$float_ret" -eq "0" ]; then
+        echo output matches reference
+    else
+        echo ERROR: output does not match reference
+        exit 1
+    fi
+    echo
+done
+
+
+
+echo All tests have passed successfully
+grep quality logs_mono.txt | awk '{sum+=$4}END{print "Average mono quality is", sum/NR, "%"}'
+grep quality logs_stereo.txt | awk '{sum+=$4}END{print "Average stereo quality is", sum/NR, "%"}'
diff --git a/jni/libopus/tests/test_opus_api.c b/jni/libopus/tests/test_opus_api.c
new file mode 100644
index 0000000..c2d7e10
--- /dev/null
+++ b/jni/libopus/tests/test_opus_api.c
@@ -0,0 +1,1719 @@
+/* Copyright (c) 2011 Xiph.Org Foundation
+   Written by Gregory Maxwell */
+/*
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+   - Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/* This tests the API presented by the libopus system.
+   It does not attempt to extensively exercise the codec internals.
+   The strategy here is to simply the API interface invariants:
+   That sane options are accepted, insane options are rejected,
+   and that nothing blows up. In particular we don't actually test
+   that settings are heeded by the codec (though we do check that
+   get after set returns a sane value when it should). Other
+   tests check the actual codec behavior.
+   In cases where its reasonable to do so we test exhaustively,
+   but its not reasonable to do so in all cases.
+   Although these tests are simple they found several library bugs
+   when they were initially developed. */
+
+/* These tests are more sensitive if compiled with -DVALGRIND and
+   run inside valgrind. Malloc failure testing requires glibc. */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include "arch.h"
+#include "opus_multistream.h"
+#include "opus.h"
+#include "test_opus_common.h"
+
+#ifdef VALGRIND
+#include <valgrind/memcheck.h>
+#define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
+#define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
+#else
+#define VG_UNDEF(x,y)
+#define VG_CHECK(x,y)
+#endif
+
+#if defined(HAVE___MALLOC_HOOK)
+#define MALLOC_FAIL
+#include "os_support.h"
+#include <malloc.h>
+
+static const opus_int32 opus_apps[3] = {OPUS_APPLICATION_VOIP,
+       OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY};
+
+void *malloc_hook(__attribute__((unused)) size_t size,
+                  __attribute__((unused)) const void *caller)
+{
+   return 0;
+}
+#endif
+
+static const opus_int32 opus_rates[5] = {48000,24000,16000,12000,8000};
+
+opus_int32 test_dec_api(void)
+{
+   opus_uint32 dec_final_range;
+   OpusDecoder *dec;
+   OpusDecoder *dec2;
+   opus_int32 i,j,cfgs;
+   unsigned char packet[1276];
+#ifndef DISABLE_FLOAT_API
+   float fbuf[960*2];
+#endif
+   short sbuf[960*2];
+   int c,err;
+   opus_int32 *nullvalue;
+   nullvalue=0;
+
+   cfgs=0;
+   /*First test invalid configurations which should fail*/
+   fprintf(stdout,"\n  Decoder basic API tests\n");
+   fprintf(stdout,"  ---------------------------------------------------\n");
+   for(c=0;c<4;c++)
+   {
+      i=opus_decoder_get_size(c);
+      if(((c==1||c==2)&&(i<=2048||i>1<<16))||((c!=1&&c!=2)&&i!=0))test_failed();
+      fprintf(stdout,"    opus_decoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
+      cfgs++;
+   }
+
+   /*Test with unsupported sample rates*/
+   for(c=0;c<4;c++)
+   {
+      for(i=-7;i<=96000;i++)
+      {
+         int fs;
+         if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
+         switch(i)
+         {
+           case(-5):fs=-8000;break;
+           case(-6):fs=INT32_MAX;break;
+           case(-7):fs=INT32_MIN;break;
+           default:fs=i;
+         }
+         err = OPUS_OK;
+         VG_UNDEF(&err,sizeof(err));
+         dec = opus_decoder_create(fs, c, &err);
+         if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
+         cfgs++;
+         dec = opus_decoder_create(fs, c, 0);
+         if(dec!=NULL)test_failed();
+         cfgs++;
+         dec=malloc(opus_decoder_get_size(2));
+         if(dec==NULL)test_failed();
+         err = opus_decoder_init(dec,fs,c);
+         if(err!=OPUS_BAD_ARG)test_failed();
+         cfgs++;
+         free(dec);
+      }
+   }
+
+   VG_UNDEF(&err,sizeof(err));
+   dec = opus_decoder_create(48000, 2, &err);
+   if(err!=OPUS_OK || dec==NULL)test_failed();
+   VG_CHECK(dec,opus_decoder_get_size(2));
+   cfgs++;
+
+   fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
+   fprintf(stdout,"    opus_decoder_init() .......................... OK.\n");
+
+   VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
+   err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
+   if(err!=OPUS_OK)test_failed();
+   VG_CHECK(&dec_final_range,sizeof(dec_final_range));
+   fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
+   cfgs++;
+
+   err=opus_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
+   if(err!=OPUS_UNIMPLEMENTED)test_failed();
+   fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
+   cfgs++;
+
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
+   if(err != OPUS_OK || i!=0)test_failed();
+   fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
+   cfgs++;
+
+   /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
+   err=opus_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
+   if(err!=OPUS_BAD_ARG)test_failed();
+   cfgs++;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
+   if(err != OPUS_OK || i>0 || i<-1)test_failed();
+   cfgs++;
+   VG_UNDEF(packet,sizeof(packet));
+   packet[0]=63<<2;packet[1]=packet[2]=0;
+   if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
+   cfgs++;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
+   if(err != OPUS_OK || i>0 || i<-1)test_failed();
+   cfgs++;
+   packet[0]=1;
+   if(opus_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
+   cfgs++;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
+   if(err != OPUS_OK || i>0 || i<-1)test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_GET_PITCH ............................... OK.\n");
+
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
+   VG_CHECK(&i,sizeof(i));
+   if(err != OPUS_OK || i!=0)test_failed();
+   cfgs++;
+   err=opus_decoder_ctl(dec, OPUS_GET_GAIN(nullvalue));
+   if(err != OPUS_BAD_ARG)test_failed();
+   cfgs++;
+   err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-32769));
+   if(err != OPUS_BAD_ARG)test_failed();
+   cfgs++;
+   err=opus_decoder_ctl(dec, OPUS_SET_GAIN(32768));
+   if(err != OPUS_BAD_ARG)test_failed();
+   cfgs++;
+   err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-15));
+   if(err != OPUS_OK)test_failed();
+   cfgs++;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
+   VG_CHECK(&i,sizeof(i));
+   if(err != OPUS_OK || i!=-15)test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_SET_GAIN ................................ OK.\n");
+   fprintf(stdout,"    OPUS_GET_GAIN ................................ OK.\n");
+
+   /*Reset the decoder*/
+   dec2=malloc(opus_decoder_get_size(2));
+   memcpy(dec2,dec,opus_decoder_get_size(2));
+   if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
+   if(memcmp(dec2,dec,opus_decoder_get_size(2))==0)test_failed();
+   free(dec2);
+   fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
+   cfgs++;
+
+   VG_UNDEF(packet,sizeof(packet));
+   packet[0]=0;
+   if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed();
+   if(opus_packet_get_nb_samples(packet,1,48000)!=480)test_failed();
+   if(opus_packet_get_nb_samples(packet,1,96000)!=960)test_failed();
+   if(opus_packet_get_nb_samples(packet,1,32000)!=320)test_failed();
+   if(opus_packet_get_nb_samples(packet,1,8000)!=80)test_failed();
+   packet[0]=3;
+   if(opus_packet_get_nb_samples(packet,1,24000)!=OPUS_INVALID_PACKET)test_failed();
+   packet[0]=(63<<2)|3;
+   packet[1]=63;
+   if(opus_packet_get_nb_samples(packet,0,24000)!=OPUS_BAD_ARG)test_failed();
+   if(opus_packet_get_nb_samples(packet,2,48000)!=OPUS_INVALID_PACKET)test_failed();
+   if(opus_decoder_get_nb_samples(dec,packet,2)!=OPUS_INVALID_PACKET)test_failed();
+   fprintf(stdout,"    opus_{packet,decoder}_get_nb_samples() ....... OK.\n");
+   cfgs+=9;
+
+   if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed();
+   for(i=0;i<256;i++) {
+     int l1res[4]={1,2,2,OPUS_INVALID_PACKET};
+     packet[0]=i;
+     if(l1res[packet[0]&3]!=opus_packet_get_nb_frames(packet,1))test_failed();
+     cfgs++;
+     for(j=0;j<256;j++) {
+       packet[1]=j;
+       if(((packet[0]&3)!=3?l1res[packet[0]&3]:packet[1]&63)!=opus_packet_get_nb_frames(packet,2))test_failed();
+       cfgs++;
+     }
+   }
+   fprintf(stdout,"    opus_packet_get_nb_frames() .................. OK.\n");
+
+   for(i=0;i<256;i++) {
+     int bw;
+     packet[0]=i;
+     bw=packet[0]>>4;
+     bw=OPUS_BANDWIDTH_NARROWBAND+(((((bw&7)*9)&(63-(bw&8)))+2+12*((bw&8)!=0))>>4);
+     if(bw!=opus_packet_get_bandwidth(packet))test_failed();
+     cfgs++;
+   }
+   fprintf(stdout,"    opus_packet_get_bandwidth() .................. OK.\n");
+
+   for(i=0;i<256;i++) {
+     int fp3s,rate;
+     packet[0]=i;
+     fp3s=packet[0]>>3;
+     fp3s=((((3-(fp3s&3))*13&119)+9)>>2)*((fp3s>13)*(3-((fp3s&3)==3))+1)*25;
+     for(rate=0;rate<5;rate++) {
+       if((opus_rates[rate]*3/fp3s)!=opus_packet_get_samples_per_frame(packet,opus_rates[rate]))test_failed();
+       cfgs++;
+     }
+   }
+   fprintf(stdout,"    opus_packet_get_samples_per_frame() .......... OK.\n");
+
+   packet[0]=(63<<2)+3;
+   packet[1]=49;
+   for(j=2;j<51;j++)packet[j]=0;
+   VG_UNDEF(sbuf,sizeof(sbuf));
+   if(opus_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
+   cfgs++;
+   packet[0]=(63<<2);
+   packet[1]=packet[2]=0;
+   if(opus_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG)test_failed();
+   cfgs++;
+   if(opus_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+   cfgs++;
+   if(opus_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+   cfgs++;
+   if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
+   cfgs++;
+   fprintf(stdout,"    opus_decode() ................................ OK.\n");
+#ifndef DISABLE_FLOAT_API
+   VG_UNDEF(fbuf,sizeof(fbuf));
+   if(opus_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
+   cfgs++;
+   fprintf(stdout,"    opus_decode_float() .......................... OK.\n");
+#endif
+
+#if 0
+   /*These tests are disabled because the library crashes with null states*/
+   if(opus_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
+   if(opus_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
+   if(opus_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
+   if(opus_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
+   if(opus_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
+   if(opus_packet_get_nb_frames(NULL,1)            !=OPUS_BAD_ARG)test_failed();
+   if(opus_packet_get_bandwidth(NULL)              !=OPUS_BAD_ARG)test_failed();
+   if(opus_packet_get_samples_per_frame(NULL,48000)!=OPUS_BAD_ARG)test_failed();
+#endif
+   opus_decoder_destroy(dec);
+   cfgs++;
+   fprintf(stdout,"                   All decoder interface tests passed\n");
+   fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
+   return cfgs;
+}
+
+opus_int32 test_msdec_api(void)
+{
+   opus_uint32 dec_final_range;
+   OpusMSDecoder *dec;
+   OpusDecoder *streamdec;
+   opus_int32 i,j,cfgs;
+   unsigned char packet[1276];
+   unsigned char mapping[256];
+#ifndef DISABLE_FLOAT_API
+   float fbuf[960*2];
+#endif
+   short sbuf[960*2];
+   int a,b,c,err;
+#if 0
+   /*Relevant test not enabled for multistream*/
+   int *nullvalue;
+   nullvalue=0;
+#endif
+
+   mapping[0]=0;
+   mapping[1]=1;
+   for(i=2;i<256;i++)VG_UNDEF(&mapping[i],sizeof(unsigned char));
+
+   cfgs=0;
+   /*First test invalid configurations which should fail*/
+   fprintf(stdout,"\n  Multistream decoder basic API tests\n");
+   fprintf(stdout,"  ---------------------------------------------------\n");
+   for(a=-1;a<4;a++)
+   {
+      for(b=-1;b<4;b++)
+      {
+         i=opus_multistream_decoder_get_size(a,b);
+         if(((a>0&&b<=a&&b>=0)&&(i<=2048||i>((1<<16)*a)))||((a<1||b>a||b<0)&&i!=0))test_failed();
+         fprintf(stdout,"    opus_multistream_decoder_get_size(%2d,%2d)=%d %sOK.\n",a,b,i,i>0?"":"... ");
+         cfgs++;
+      }
+   }
+
+   /*Test with unsupported sample rates*/
+   for(c=1;c<3;c++)
+   {
+      for(i=-7;i<=96000;i++)
+      {
+         int fs;
+         if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
+         switch(i)
+         {
+           case(-5):fs=-8000;break;
+           case(-6):fs=INT32_MAX;break;
+           case(-7):fs=INT32_MIN;break;
+           default:fs=i;
+         }
+         err = OPUS_OK;
+         VG_UNDEF(&err,sizeof(err));
+         dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, &err);
+         if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
+         cfgs++;
+         dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, 0);
+         if(dec!=NULL)test_failed();
+         cfgs++;
+         dec=malloc(opus_multistream_decoder_get_size(1,1));
+         if(dec==NULL)test_failed();
+         err = opus_multistream_decoder_init(dec,fs,c,1,c-1, mapping);
+         if(err!=OPUS_BAD_ARG)test_failed();
+         cfgs++;
+         free(dec);
+      }
+   }
+
+   for(c=0;c<2;c++)
+   {
+      int *ret_err;
+      ret_err = c?0:&err;
+
+      mapping[0]=0;
+      mapping[1]=1;
+      for(i=2;i<256;i++)VG_UNDEF(&mapping[i],sizeof(unsigned char));
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      mapping[0]=mapping[1]=0;
+      dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
+      cfgs++;
+      opus_multistream_decoder_destroy(dec);
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, 1, 4, 1, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
+      cfgs++;
+
+      err = opus_multistream_decoder_init(dec,48000, 1, 0, 0, mapping);
+      if(err!=OPUS_BAD_ARG)test_failed();
+      cfgs++;
+
+      err = opus_multistream_decoder_init(dec,48000, 1, 1, -1, mapping);
+      if(err!=OPUS_BAD_ARG)test_failed();
+      cfgs++;
+
+      opus_multistream_decoder_destroy(dec);
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
+      cfgs++;
+      opus_multistream_decoder_destroy(dec);
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, 255, 255, 1, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, -1, 1, 1, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, 0, 1, 1, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, 1, -1, 2, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, 1, -1, -1, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, 256, 255, 1, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      dec = opus_multistream_decoder_create(48000, 256, 255, 0, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      mapping[0]=255;
+      mapping[1]=1;
+      mapping[2]=2;
+      dec = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      mapping[0]=0;
+      mapping[1]=0;
+      mapping[2]=0;
+      dec = opus_multistream_decoder_create(48000, 3, 2, 1, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
+      cfgs++;
+      opus_multistream_decoder_destroy(dec);
+      cfgs++;
+
+      VG_UNDEF(ret_err,sizeof(*ret_err));
+      mapping[0]=0;
+      mapping[1]=255;
+      mapping[2]=1;
+      mapping[3]=2;
+      mapping[4]=3;
+      dec = opus_multistream_decoder_create(48001, 5, 4, 1, mapping, ret_err);
+      if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
+      if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
+      cfgs++;
+   }
+
+   VG_UNDEF(&err,sizeof(err));
+   mapping[0]=0;
+   mapping[1]=255;
+   mapping[2]=1;
+   mapping[3]=2;
+   dec = opus_multistream_decoder_create(48000, 4, 2, 1, mapping, &err);
+   VG_CHECK(&err,sizeof(err));
+   if(err!=OPUS_OK || dec==NULL)test_failed();
+   cfgs++;
+
+   fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
+   fprintf(stdout,"    opus_multistream_decoder_init() .............. OK.\n");
+
+   VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
+   err=opus_multistream_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
+   if(err!=OPUS_OK)test_failed();
+   VG_CHECK(&dec_final_range,sizeof(dec_final_range));
+   fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
+   cfgs++;
+
+   streamdec=0;
+   VG_UNDEF(&streamdec,sizeof(streamdec));
+   err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(-1,&streamdec));
+   if(err!=OPUS_BAD_ARG)test_failed();
+   cfgs++;
+   err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(1,&streamdec));
+   if(err!=OPUS_OK||streamdec==NULL)test_failed();
+   VG_CHECK(streamdec,opus_decoder_get_size(1));
+   cfgs++;
+   err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(2,&streamdec));
+   if(err!=OPUS_BAD_ARG)test_failed();
+   cfgs++;
+   err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(0,&streamdec));
+   if(err!=OPUS_OK||streamdec==NULL)test_failed();
+   VG_CHECK(streamdec,opus_decoder_get_size(1));
+   fprintf(stdout,"    OPUS_MULTISTREAM_GET_DECODER_STATE ........... OK.\n");
+   cfgs++;
+
+   for(j=0;j<2;j++)
+   {
+      OpusDecoder *od;
+      err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
+      if(err != OPUS_OK)test_failed();
+      VG_UNDEF(&i,sizeof(i));
+      err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
+      VG_CHECK(&i,sizeof(i));
+      if(err != OPUS_OK || i!=0)test_failed();
+      cfgs++;
+   }
+   err=opus_multistream_decoder_ctl(dec,OPUS_SET_GAIN(15));
+   if(err!=OPUS_OK)test_failed();
+   fprintf(stdout,"    OPUS_SET_GAIN ................................ OK.\n");
+   for(j=0;j<2;j++)
+   {
+      OpusDecoder *od;
+      err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
+      if(err != OPUS_OK)test_failed();
+      VG_UNDEF(&i,sizeof(i));
+      err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
+      VG_CHECK(&i,sizeof(i));
+      if(err != OPUS_OK || i!=15)test_failed();
+      cfgs++;
+   }
+   fprintf(stdout,"    OPUS_GET_GAIN ................................ OK.\n");
+
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_multistream_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
+   if(err != OPUS_OK || i!=0)test_failed();
+   fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
+   cfgs++;
+
+   err=opus_multistream_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
+   if(err!=OPUS_UNIMPLEMENTED)test_failed();
+   fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
+   cfgs++;
+
+#if 0
+   /*Currently unimplemented for multistream*/
+   /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
+   err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
+   if(err!=OPUS_BAD_ARG)test_failed();
+   cfgs++;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
+   if(err != OPUS_OK || i>0 || i<-1)test_failed();
+   cfgs++;
+   VG_UNDEF(packet,sizeof(packet));
+   packet[0]=63<<2;packet[1]=packet[2]=0;
+   if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
+   cfgs++;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
+   if(err != OPUS_OK || i>0 || i<-1)test_failed();
+   cfgs++;
+   packet[0]=1;
+   if(opus_multistream_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
+   cfgs++;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
+   if(err != OPUS_OK || i>0 || i<-1)test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_GET_PITCH ............................... OK.\n");
+#endif
+
+   /*Reset the decoder*/
+   if(opus_multistream_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
+   fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
+   cfgs++;
+
+   opus_multistream_decoder_destroy(dec);
+   cfgs++;
+   VG_UNDEF(&err,sizeof(err));
+   dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
+   if(err!=OPUS_OK || dec==NULL)test_failed();
+   cfgs++;
+
+   packet[0]=(63<<2)+3;
+   packet[1]=49;
+   for(j=2;j<51;j++)packet[j]=0;
+   VG_UNDEF(sbuf,sizeof(sbuf));
+   if(opus_multistream_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
+   cfgs++;
+   packet[0]=(63<<2);
+   packet[1]=packet[2]=0;
+   if(opus_multistream_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG){printf("%d\n",opus_multistream_decode(dec, packet, -1, sbuf, 960, 0));test_failed();}
+   cfgs++;
+   if(opus_multistream_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+   cfgs++;
+   if(opus_multistream_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+   cfgs++;
+   if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
+   cfgs++;
+   fprintf(stdout,"    opus_multistream_decode() .................... OK.\n");
+#ifndef DISABLE_FLOAT_API
+   VG_UNDEF(fbuf,sizeof(fbuf));
+   if(opus_multistream_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
+   cfgs++;
+   fprintf(stdout,"    opus_multistream_decode_float() .............. OK.\n");
+#endif
+
+#if 0
+   /*These tests are disabled because the library crashes with null states*/
+   if(opus_multistream_decoder_ctl(0,OPUS_RESET_STATE)         !=OPUS_INVALID_STATE)test_failed();
+   if(opus_multistream_decoder_init(0,48000,1)                 !=OPUS_INVALID_STATE)test_failed();
+   if(opus_multistream_decode(0,packet,1,outbuf,2880,0)        !=OPUS_INVALID_STATE)test_failed();
+   if(opus_multistream_decode_float(0,packet,1,0,2880,0)       !=OPUS_INVALID_STATE)test_failed();
+   if(opus_multistream_decoder_get_nb_samples(0,packet,1)      !=OPUS_INVALID_STATE)test_failed();
+#endif
+   opus_multistream_decoder_destroy(dec);
+   cfgs++;
+   fprintf(stdout,"       All multistream decoder interface tests passed\n");
+   fprintf(stdout,"                             (%6d API invocations)\n",cfgs);
+   return cfgs;
+}
+
+#ifdef VALGRIND
+#define UNDEFINE_FOR_PARSE  toc=-1; \
+   frames[0]=(unsigned char *)0; \
+   frames[1]=(unsigned char *)0; \
+   payload_offset=-1; \
+   VG_UNDEF(&toc,sizeof(toc)); \
+   VG_UNDEF(frames,sizeof(frames));\
+   VG_UNDEF(&payload_offset,sizeof(payload_offset));
+#else
+#define UNDEFINE_FOR_PARSE  toc=-1; \
+   frames[0]=(unsigned char *)0; \
+   frames[1]=(unsigned char *)0; \
+   payload_offset=-1;
+#endif
+
+/* This test exercises the heck out of the libopus parser.
+   It is much larger than the parser itself in part because
+   it tries to hit a lot of corner cases that could never
+   fail with the libopus code, but might be problematic for
+   other implementations. */
+opus_int32 test_parse(void)
+{
+   opus_int32 i,j,jj,sz;
+   unsigned char packet[1276];
+   opus_int32 cfgs,cfgs_total;
+   unsigned char toc;
+   const unsigned char *frames[48];
+   short size[48];
+   int payload_offset, ret;
+   fprintf(stdout,"\n  Packet header parsing tests\n");
+   fprintf(stdout,"  ---------------------------------------------------\n");
+   memset(packet,0,sizeof(char)*1276);
+   packet[0]=63<<2;
+   if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
+   cfgs_total=cfgs=1;
+   /*code 0*/
+   for(i=0;i<64;i++)
+   {
+      UNDEFINE_FOR_PARSE
+      ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
+      cfgs++;
+      if(ret!=1)test_failed();
+      if(size[0]!=3)test_failed();
+      if(frames[0]!=packet+1)test_failed();
+   }
+   fprintf(stdout,"    code 0 (%2d cases) ............................ OK.\n",cfgs);
+   cfgs_total+=cfgs;cfgs=0;
+
+   /*code 1, two frames of the same size*/
+   for(i=0;i<64;i++)
+   {
+      packet[0]=(i<<2)+1;
+      for(jj=0;jj<=1275*2+3;jj++)
+      {
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if((jj&1)==1 && jj<=2551)
+         {
+            /* Must pass if payload length even (packet length odd) and
+               size<=2551, must fail otherwise. */
+            if(ret!=2)test_failed();
+            if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
+            if(frames[0]!=packet+1)test_failed();
+            if(frames[1]!=frames[0]+size[0])test_failed();
+            if((toc>>2)!=i)test_failed();
+         } else if(ret!=OPUS_INVALID_PACKET)test_failed();
+      }
+   }
+   fprintf(stdout,"    code 1 (%6d cases) ........................ OK.\n",cfgs);
+   cfgs_total+=cfgs;cfgs=0;
+
+   for(i=0;i<64;i++)
+   {
+      /*code 2, length code overflow*/
+      packet[0]=(i<<2)+2;
+      UNDEFINE_FOR_PARSE
+      ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
+      cfgs++;
+      if(ret!=OPUS_INVALID_PACKET)test_failed();
+      packet[1]=252;
+      UNDEFINE_FOR_PARSE
+      ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
+      cfgs++;
+      if(ret!=OPUS_INVALID_PACKET)test_failed();
+      for(j=0;j<1275;j++)
+      {
+         if(j<252)packet[1]=j;
+         else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
+         /*Code 2, one too short*/
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(ret!=OPUS_INVALID_PACKET)test_failed();
+         /*Code 2, one too long*/
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(ret!=OPUS_INVALID_PACKET)test_failed();
+         /*Code 2, second zero*/
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(ret!=2)test_failed();
+         if(size[0]!=j||size[1]!=0)test_failed();
+         if(frames[1]!=frames[0]+size[0])test_failed();
+         if((toc>>2)!=i)test_failed();
+         /*Code 2, normal*/
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(ret!=2)test_failed();
+         if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
+         if(frames[1]!=frames[0]+size[0])test_failed();
+         if((toc>>2)!=i)test_failed();
+      }
+   }
+   fprintf(stdout,"    code 2 (%6d cases) ........................ OK.\n",cfgs);
+   cfgs_total+=cfgs;cfgs=0;
+
+   for(i=0;i<64;i++)
+   {
+      packet[0]=(i<<2)+3;
+      /*code 3, length code overflow*/
+      UNDEFINE_FOR_PARSE
+      ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
+      cfgs++;
+      if(ret!=OPUS_INVALID_PACKET)test_failed();
+   }
+   fprintf(stdout,"    code 3 m-truncation (%2d cases) ............... OK.\n",cfgs);
+   cfgs_total+=cfgs;cfgs=0;
+
+   for(i=0;i<64;i++)
+   {
+      /*code 3, m is zero or 49-63*/
+      packet[0]=(i<<2)+3;
+      for(jj=49;jj<=64;jj++)
+      {
+        packet[1]=0+(jj&63); /*CBR, no padding*/
+        UNDEFINE_FOR_PARSE
+        ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
+        cfgs++;
+        if(ret!=OPUS_INVALID_PACKET)test_failed();
+        packet[1]=128+(jj&63); /*VBR, no padding*/
+        UNDEFINE_FOR_PARSE
+        ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
+        cfgs++;
+        if(ret!=OPUS_INVALID_PACKET)test_failed();
+        packet[1]=64+(jj&63); /*CBR, padding*/
+        UNDEFINE_FOR_PARSE
+        ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
+        cfgs++;
+        if(ret!=OPUS_INVALID_PACKET)test_failed();
+        packet[1]=128+64+(jj&63); /*VBR, padding*/
+        UNDEFINE_FOR_PARSE
+        ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
+        cfgs++;
+        if(ret!=OPUS_INVALID_PACKET)test_failed();
+      }
+   }
+   fprintf(stdout,"    code 3 m=0,49-64 (%2d cases) ................ OK.\n",cfgs);
+   cfgs_total+=cfgs;cfgs=0;
+
+   for(i=0;i<64;i++)
+   {
+      packet[0]=(i<<2)+3;
+      /*code 3, m is one, cbr*/
+      packet[1]=1;
+      for(j=0;j<1276;j++)
+      {
+        UNDEFINE_FOR_PARSE
+        ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
+        cfgs++;
+        if(ret!=1)test_failed();
+        if(size[0]!=j)test_failed();
+        if((toc>>2)!=i)test_failed();
+      }
+      UNDEFINE_FOR_PARSE
+      ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
+      cfgs++;
+      if(ret!=OPUS_INVALID_PACKET)test_failed();
+   }
+   fprintf(stdout,"    code 3 m=1 CBR (%2d cases) ................. OK.\n",cfgs);
+   cfgs_total+=cfgs;cfgs=0;
+
+   for(i=0;i<64;i++)
+   {
+      int frame_samp;
+      /*code 3, m>1 CBR*/
+      packet[0]=(i<<2)+3;
+      frame_samp=opus_packet_get_samples_per_frame(packet,48000);
+      for(j=2;j<49;j++)
+      {
+         packet[1]=j;
+         for(sz=2;sz<((j+2)*1275);sz++)
+         {
+            UNDEFINE_FOR_PARSE
+            ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
+            cfgs++;
+            /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
+            if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
+            {
+               if(ret!=j)test_failed();
+               for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
+               if((toc>>2)!=i)test_failed();
+            } else if(ret!=OPUS_INVALID_PACKET)test_failed();
+         }
+      }
+      /*Super jumbo packets*/
+      packet[1]=5760/frame_samp;
+      UNDEFINE_FOR_PARSE
+      ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
+      cfgs++;
+      if(ret!=packet[1])test_failed();
+      for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
+   }
+   fprintf(stdout,"    code 3 m=1-48 CBR (%2d cases) .......... OK.\n",cfgs);
+   cfgs_total+=cfgs;cfgs=0;
+
+   for(i=0;i<64;i++)
+   {
+      int frame_samp;
+      /*Code 3 VBR, m one*/
+      packet[0]=(i<<2)+3;
+      packet[1]=128+1;
+      frame_samp=opus_packet_get_samples_per_frame(packet,48000);
+      for(jj=0;jj<1276;jj++)
+      {
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(ret!=1)test_failed();
+         if(size[0]!=jj)test_failed();
+         if((toc>>2)!=i)test_failed();
+      }
+      UNDEFINE_FOR_PARSE
+      ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
+      cfgs++;
+      if(ret!=OPUS_INVALID_PACKET)test_failed();
+      for(j=2;j<49;j++)
+      {
+         packet[1]=128+j;
+         /*Length code overflow*/
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(ret!=OPUS_INVALID_PACKET)test_failed();
+         packet[2]=252;
+         packet[3]=0;
+         for(jj=4;jj<2+j;jj++)packet[jj]=0;
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(ret!=OPUS_INVALID_PACKET)test_failed();
+         /*One byte too short*/
+         for(jj=2;jj<2+j;jj++)packet[jj]=0;
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(ret!=OPUS_INVALID_PACKET)test_failed();
+         /*One byte too short thanks to length coding*/
+         packet[2]=252;
+         packet[3]=0;
+         for(jj=4;jj<2+j;jj++)packet[jj]=0;
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(ret!=OPUS_INVALID_PACKET)test_failed();
+         /*Most expensive way of coding zeros*/
+         for(jj=2;jj<2+j;jj++)packet[jj]=0;
+         UNDEFINE_FOR_PARSE
+         ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
+         cfgs++;
+         if(frame_samp*j<=5760){
+            if(ret!=j)test_failed();
+            for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
+            if((toc>>2)!=i)test_failed();
+         } else if(ret!=OPUS_INVALID_PACKET)test_failed();
+         /*Quasi-CBR use of mode 3*/
+         for(sz=0;sz<8;sz++)
+         {
+            const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
+            int pos=0;
+            int as=(tsz[sz]+i-j-2)/j;
+            for(jj=0;jj<j-1;jj++)
+            {
+              if(as<252){packet[2+pos]=as;pos++;}
+              else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
+            }
+            UNDEFINE_FOR_PARSE
+            ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
+            cfgs++;
+            if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
+               if(ret!=j)test_failed();
+               for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
+               if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
+               if((toc>>2)!=i)test_failed();
+            } else if(ret!=OPUS_INVALID_PACKET)test_failed();
+         }
+      }
+   }
+   fprintf(stdout,"    code 3 m=1-48 VBR (%2d cases) ............. OK.\n",cfgs);
+   cfgs_total+=cfgs;cfgs=0;
+
+   for(i=0;i<64;i++)
+   {
+      packet[0]=(i<<2)+3;
+      /*Padding*/
+      packet[1]=128+1+64;
+      /*Overflow the length coding*/
+      for(jj=2;jj<127;jj++)packet[jj]=255;
+      UNDEFINE_FOR_PARSE
+      ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
+      cfgs++;
+      if(ret!=OPUS_INVALID_PACKET)test_failed();
+
+      for(sz=0;sz<4;sz++)
+      {
+         const int tsz[4]={0,72,512,1275};
+         for(jj=sz;jj<65025;jj+=11)
+         {
+            int pos;
+            for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
+            packet[2+pos]=jj%254;
+            pos++;
+            if(sz==0&&i==63)
+            {
+               /*Code more padding than there is room in the packet*/
+               UNDEFINE_FOR_PARSE
+               ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
+               cfgs++;
+               if(ret!=OPUS_INVALID_PACKET)test_failed();
+            }
+            UNDEFINE_FOR_PARSE
+            ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
+            cfgs++;
+            if(tsz[sz]+i<1276)
+            {
+               if(ret!=1)test_failed();
+               if(size[0]!=tsz[sz]+i)test_failed();
+               if((toc>>2)!=i)test_failed();
+            } else if (ret!=OPUS_INVALID_PACKET)test_failed();
+         }
+      }
+   }
+   fprintf(stdout,"    code 3 padding (%2d cases) ............... OK.\n",cfgs);
+   cfgs_total+=cfgs;cfgs=0;
+   fprintf(stdout,"    opus_packet_parse ............................ OK.\n");
+   fprintf(stdout,"                      All packet parsing tests passed\n");
+   fprintf(stdout,"                          (%d API invocations)\n",cfgs_total);
+   return cfgs_total;
+}
+
+/* This is a helper macro for the encoder tests.
+   The encoder api tests all have a pattern of set-must-fail, set-must-fail,
+   set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
+#define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
+   i=(badv);\
+   if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
+   i=(badv2);\
+   if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
+   j=i=(goodv);\
+   if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
+   i=-12345;\
+   VG_UNDEF(&i,sizeof(i)); \
+   err=opus_encoder_ctl(enc,getcall);\
+   if(err!=OPUS_OK || i!=j)test_failed();\
+   j=i=(goodv2);\
+   if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
+   fprintf(stdout,sok);\
+   i=-12345;\
+   VG_UNDEF(&i,sizeof(i)); \
+   err=opus_encoder_ctl(enc,getcall);\
+   if(err!=OPUS_OK || i!=j)test_failed();\
+   fprintf(stdout,gok);\
+   cfgs+=6;
+
+opus_int32 test_enc_api(void)
+{
+   opus_uint32 enc_final_range;
+   OpusEncoder *enc;
+   opus_int32 i,j;
+   unsigned char packet[1276];
+#ifndef DISABLE_FLOAT_API
+   float fbuf[960*2];
+#endif
+   short sbuf[960*2];
+   int c,err,cfgs;
+
+   cfgs=0;
+   /*First test invalid configurations which should fail*/
+   fprintf(stdout,"\n  Encoder basic API tests\n");
+   fprintf(stdout,"  ---------------------------------------------------\n");
+   for(c=0;c<4;c++)
+   {
+      i=opus_encoder_get_size(c);
+      if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
+      fprintf(stdout,"    opus_encoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
+      cfgs++;
+   }
+
+   /*Test with unsupported sample rates, channel counts*/
+   for(c=0;c<4;c++)
+   {
+      for(i=-7;i<=96000;i++)
+      {
+         int fs;
+         if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
+         switch(i)
+         {
+           case(-5):fs=-8000;break;
+           case(-6):fs=INT32_MAX;break;
+           case(-7):fs=INT32_MIN;break;
+           default:fs=i;
+         }
+         err = OPUS_OK;
+         VG_UNDEF(&err,sizeof(err));
+         enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
+         if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
+         cfgs++;
+         enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, 0);
+         if(enc!=NULL)test_failed();
+         cfgs++;
+         opus_encoder_destroy(enc);
+         enc=malloc(opus_encoder_get_size(2));
+         if(enc==NULL)test_failed();
+         err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
+         if(err!=OPUS_BAD_ARG)test_failed();
+         cfgs++;
+         free(enc);
+      }
+   }
+
+   enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
+   if(enc!=NULL)test_failed();
+   cfgs++;
+
+   VG_UNDEF(&err,sizeof(err));
+   enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
+   if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
+   cfgs++;
+
+   VG_UNDEF(&err,sizeof(err));
+   enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
+   if(enc==NULL)test_failed();
+   opus_encoder_destroy(enc);
+   cfgs++;
+
+   VG_UNDEF(&err,sizeof(err));
+   enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
+   if(err!=OPUS_OK || enc==NULL)test_failed();
+   cfgs++;
+   err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
+   if(err!=OPUS_OK || i<0 || i>32766)test_failed();
+   opus_encoder_destroy(enc);
+   cfgs++;
+
+   VG_UNDEF(&err,sizeof(err));
+   enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
+   if(err!=OPUS_OK || enc==NULL)test_failed();
+   cfgs++;
+   err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
+   if(err!=OPUS_OK || i<0 || i>32766)test_failed();
+   opus_encoder_destroy(enc);
+   cfgs++;
+
+   VG_UNDEF(&err,sizeof(err));
+   enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
+   if(err!=OPUS_OK || enc==NULL)test_failed();
+   cfgs++;
+
+   fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
+   fprintf(stdout,"    opus_encoder_init() .......................... OK.\n");
+
+   i=-12345;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
+   if(err!=OPUS_OK || i<0 || i>32766)test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_GET_LOOKAHEAD ........................... OK.\n");
+
+   if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
+   fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
+   cfgs++;
+
+   CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
+     OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
+     "    OPUS_SET_APPLICATION ......................... OK.\n",
+     "    OPUS_GET_APPLICATION ......................... OK.\n")
+
+   if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
+   cfgs++;
+   VG_UNDEF(&i,sizeof(i));
+   if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
+   if(i>700000||i<256000)test_failed();
+   cfgs++;
+   CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
+     500,256000,
+     "    OPUS_SET_BITRATE ............................. OK.\n",
+     "    OPUS_GET_BITRATE ............................. OK.\n")
+
+   CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
+     1,OPUS_AUTO,
+     "    OPUS_SET_FORCE_CHANNELS ...................... OK.\n",
+     "    OPUS_GET_FORCE_CHANNELS ...................... OK.\n")
+
+   i=-2;
+   if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_FULLBAND+1;
+   if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_NARROWBAND;
+   if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_FULLBAND;
+   if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_WIDEBAND;
+   if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_MEDIUMBAND;
+   if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_SET_BANDWIDTH ........................... OK.\n");
+   /*We don't test if the bandwidth has actually changed.
+     because the change may be delayed until the encoder is advanced.*/
+   i=-12345;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
+   if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
+      i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
+      i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
+   cfgs++;
+   if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
+
+   i=-2;
+   if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_FULLBAND+1;
+   if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_NARROWBAND;
+   if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_FULLBAND;
+   if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_WIDEBAND;
+   if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
+   cfgs++;
+   i=OPUS_BANDWIDTH_MEDIUMBAND;
+   if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_SET_MAX_BANDWIDTH ....................... OK.\n");
+   /*We don't test if the bandwidth has actually changed.
+     because the change may be delayed until the encoder is advanced.*/
+   i=-12345;
+   VG_UNDEF(&i,sizeof(i));
+   err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
+   if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
+      i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
+      i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_GET_MAX_BANDWIDTH ....................... OK.\n");
+
+   CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
+     1,0,
+     "    OPUS_SET_DTX ................................. OK.\n",
+     "    OPUS_GET_DTX ................................. OK.\n")
+
+   CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
+     0,10,
+     "    OPUS_SET_COMPLEXITY .......................... OK.\n",
+     "    OPUS_GET_COMPLEXITY .......................... OK.\n")
+
+   CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
+     1,0,
+     "    OPUS_SET_INBAND_FEC .......................... OK.\n",
+     "    OPUS_GET_INBAND_FEC .......................... OK.\n")
+
+   CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
+     100,0,
+     "    OPUS_SET_PACKET_LOSS_PERC .................... OK.\n",
+     "    OPUS_GET_PACKET_LOSS_PERC .................... OK.\n")
+
+   CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
+     1,0,
+     "    OPUS_SET_VBR ................................. OK.\n",
+     "    OPUS_GET_VBR ................................. OK.\n")
+
+   /*CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
+     0,50,
+     "    OPUS_SET_VOICE_RATIO ......................... OK.\n",
+     "    OPUS_GET_VOICE_RATIO ......................... OK.\n")
+   */
+
+   CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
+     1,0,
+     "    OPUS_SET_VBR_CONSTRAINT ...................... OK.\n",
+     "    OPUS_GET_VBR_CONSTRAINT ...................... OK.\n")
+
+   CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
+     OPUS_SIGNAL_MUSIC,OPUS_AUTO,
+     "    OPUS_SET_SIGNAL .............................. OK.\n",
+     "    OPUS_GET_SIGNAL .............................. OK.\n")
+
+   CHECK_SETGET(OPUS_SET_LSB_DEPTH(i),OPUS_GET_LSB_DEPTH(&i),7,25,16,24,
+     "    OPUS_SET_LSB_DEPTH ........................... OK.\n",
+     "    OPUS_GET_LSB_DEPTH ........................... OK.\n")
+
+   /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
+
+   if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
+
+   /*Reset the encoder*/
+   if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
+   cfgs++;
+   fprintf(stdout,"    OPUS_RESET_STATE ............................. OK.\n");
+
+   memset(sbuf,0,sizeof(short)*2*960);
+   VG_UNDEF(packet,sizeof(packet));
+   i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
+   if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
+   VG_CHECK(packet,i);
+   cfgs++;
+   fprintf(stdout,"    opus_encode() ................................ OK.\n");
+#ifndef DISABLE_FLOAT_API
+   memset(fbuf,0,sizeof(float)*2*960);
+   VG_UNDEF(packet,sizeof(packet));
+   i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
+   if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
+   VG_CHECK(packet,i);
+   cfgs++;
+   fprintf(stdout,"    opus_encode_float() .......................... OK.\n");
+#endif
+
+#if 0
+   /*These tests are disabled because the library crashes with null states*/
+   if(opus_encoder_ctl(0,OPUS_RESET_STATE)               !=OPUS_INVALID_STATE)test_failed();
+   if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
+   if(opus_encode(0,sbuf,960,packet,sizeof(packet))      !=OPUS_INVALID_STATE)test_failed();
+   if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
+#endif
+   opus_encoder_destroy(enc);
+   cfgs++;
+   fprintf(stdout,"                   All encoder interface tests passed\n");
+   fprintf(stdout,"                             (%d API invocations)\n",cfgs);
+   return cfgs;
+}
+
+#define max_out (1276*48+48*2+2)
+int test_repacketizer_api(void)
+{
+   int ret,cfgs,i,j,k;
+   OpusRepacketizer *rp;
+   unsigned char *packet;
+   unsigned char *po;
+   cfgs=0;
+   fprintf(stdout,"\n  Repacketizer tests\n");
+   fprintf(stdout,"  ---------------------------------------------------\n");
+
+   packet=malloc(max_out);
+   if(packet==NULL)test_failed();
+   po=malloc(max_out);
+   if(po==NULL)test_failed();
+   memset(packet,0,max_out);
+
+   i=opus_repacketizer_get_size();
+   if(i<=0)test_failed();
+   cfgs++;
+   fprintf(stdout,"    opus_repacketizer_get_size()=%d ............. OK.\n",i);
+
+   rp=malloc(i);
+   rp=opus_repacketizer_init(rp);
+   if(rp==NULL)test_failed();
+   cfgs++;
+   free(rp);
+   fprintf(stdout,"    opus_repacketizer_init ....................... OK.\n");
+
+   rp=opus_repacketizer_create();
+   if(rp==NULL)test_failed();
+   cfgs++;
+   fprintf(stdout,"    opus_repacketizer_create ..................... OK.\n");
+
+   if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
+   cfgs++;
+   fprintf(stdout,"    opus_repacketizer_get_nb_frames .............. OK.\n");
+
+   /*Length overflows*/
+   VG_UNDEF(packet,4);
+   if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
+   cfgs++;
+   packet[0]=1;
+   if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
+   cfgs++;
+   packet[0]=2;
+   if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
+   cfgs++;
+   packet[0]=3;
+   if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
+   cfgs++;
+   packet[0]=2;
+   packet[1]=255;
+   if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
+   cfgs++;
+   packet[0]=2;
+   packet[1]=250;
+   if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
+   cfgs++;
+   packet[0]=3;
+   packet[1]=0;
+   if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
+   cfgs++;
+   packet[1]=49;
+   if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
+   cfgs++;
+   packet[0]=0;
+   if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
+   cfgs++;
+   packet[0]=1<<2;
+   if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
+   cfgs++;
+
+   /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
+   opus_repacketizer_init(rp);
+   for(j=0;j<32;j++)
+   {
+      /* TOC types, test half with stereo */
+      int maxi;
+      packet[0]=((j<<1)+(j&1))<<2;
+      maxi=960/opus_packet_get_samples_per_frame(packet,8000);
+      for(i=1;i<=maxi;i++)
+      {
+         /* Number of CBR frames in the input packets */
+         int maxp;
+         packet[0]=((j<<1)+(j&1))<<2;
+         if(i>1)packet[0]+=i==2?1:3;
+         packet[1]=i>2?i:0;
+         maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
+         for(k=0;k<=(1275+75);k+=3)
+         {
+            /*Payload size*/
+            opus_int32 cnt,rcnt;
+            if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
+            for(cnt=0;cnt<maxp+2;cnt++)
+            {
+               if(cnt>0)
+               {
+                  ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
+                  if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
+                  cfgs++;
+               }
+               rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
+               if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
+               cfgs++;
+               ret=opus_repacketizer_out_range(rp,0,rcnt*i,po,max_out);
+               if(rcnt>0)
+               {
+                  int len;
+                  len=k*rcnt+((rcnt*i)>2?2:1);
+                  if(ret!=len)test_failed();
+                  if((rcnt*i)<2&&(po[0]&3)!=0)test_failed();                      /* Code 0 */
+                  if((rcnt*i)==2&&(po[0]&3)!=1)test_failed();                     /* Code 1 */
+                  if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
+                  cfgs++;
+                  if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
+                  cfgs++;
+                  if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+                  cfgs++;
+                  if(len>1)
+                  {
+                     if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+                     cfgs++;
+                  }
+                  if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+                  cfgs++;
+               } else if (ret!=OPUS_BAD_ARG)test_failed();                        /* M must not be 0 */
+            }
+            opus_repacketizer_init(rp);
+         }
+      }
+   }
+
+   /*Change in input count code, CBR out*/
+   opus_repacketizer_init(rp);
+   packet[0]=0;
+   if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
+   cfgs++;
+   packet[0]+=1;
+   if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
+   cfgs++;
+   i=opus_repacketizer_out(rp,po,max_out);
+   if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
+   cfgs++;
+   i=opus_repacketizer_out_range(rp,0,1,po,max_out);
+   if(i!=5||(po[0]&3)!=0)test_failed();
+   cfgs++;
+   i=opus_repacketizer_out_range(rp,1,2,po,max_out);
+   if(i!=5||(po[0]&3)!=0)test_failed();
+   cfgs++;
+
+   /*Change in input count code, VBR out*/
+   opus_repacketizer_init(rp);
+   packet[0]=1;
+   if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
+   cfgs++;
+   packet[0]=0;
+   if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
+   cfgs++;
+   i=opus_repacketizer_out(rp,po,max_out);
+   if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
+   cfgs++;
+
+   /*VBR in, VBR out*/
+   opus_repacketizer_init(rp);
+   packet[0]=2;
+   packet[1]=4;
+   if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
+   cfgs++;
+   if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
+   cfgs++;
+   i=opus_repacketizer_out(rp,po,max_out);
+   if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
+   cfgs++;
+
+   /*VBR in, CBR out*/
+   opus_repacketizer_init(rp);
+   packet[0]=2;
+   packet[1]=4;
+   if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
+   cfgs++;
+   if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
+   cfgs++;
+   i=opus_repacketizer_out(rp,po,max_out);
+   if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
+   cfgs++;
+
+   /*Count 0 in, VBR out*/
+   for(j=0;j<32;j++)
+   {
+      /* TOC types, test half with stereo */
+      int maxi,sum,rcnt;
+      packet[0]=((j<<1)+(j&1))<<2;
+      maxi=960/opus_packet_get_samples_per_frame(packet,8000);
+      sum=0;
+      rcnt=0;
+      opus_repacketizer_init(rp);
+      for(i=1;i<=maxi+2;i++)
+      {
+         int len;
+         ret=opus_repacketizer_cat(rp,packet,i);
+         if(rcnt<maxi)
+         {
+            if(ret!=OPUS_OK)test_failed();
+            rcnt++;
+            sum+=i-1;
+         } else if (ret!=OPUS_INVALID_PACKET)test_failed();
+         cfgs++;
+         len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
+         if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
+         if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
+         if(rcnt==2&&(po[0]&3)!=2)test_failed();
+         if(rcnt==1&&(po[0]&3)!=0)test_failed();
+         cfgs++;
+         if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
+         cfgs++;
+         if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+         cfgs++;
+         if(len>1)
+         {
+            if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+            cfgs++;
+         }
+         if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
+         cfgs++;
+      }
+   }
+
+   fprintf(stdout,"    opus_repacketizer_cat ........................ OK.\n");
+   fprintf(stdout,"    opus_repacketizer_out ........................ OK.\n");
+   fprintf(stdout,"    opus_repacketizer_out_range .................. OK.\n");
+
+   opus_repacketizer_destroy(rp);
+   cfgs++;
+   free(packet);
+   free(po);
+   fprintf(stdout,"                        All repacketizer tests passed\n");
+   fprintf(stdout,"                            (%7d API invocations)\n",cfgs);
+
+   return cfgs;
+}
+
+#ifdef MALLOC_FAIL
+/* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning
+ * under GCC. However, this is the cleanest way to test malloc failure
+ * handling in our codebase, and the lack of thread safety isn't an
+ * issue here. We therefore disable the warning for this function.
+ */
+#if OPUS_GNUC_PREREQ(4,6)
+/* Save the current warning settings */
+#pragma GCC diagnostic push
+#endif
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+
+typedef void *(*mhook)(size_t __size, __const __malloc_ptr_t);
+#endif
+
+int test_malloc_fail(void)
+{
+#ifdef MALLOC_FAIL
+   OpusDecoder *dec;
+   OpusEncoder *enc;
+   OpusRepacketizer *rp;
+   unsigned char mapping[256] = {0,1};
+   OpusMSDecoder *msdec;
+   OpusMSEncoder *msenc;
+   int rate,c,app,cfgs,err,useerr;
+   int *ep;
+   mhook orig_malloc;
+   cfgs=0;
+#endif
+   fprintf(stdout,"\n  malloc() failure tests\n");
+   fprintf(stdout,"  ---------------------------------------------------\n");
+#ifdef MALLOC_FAIL
+   orig_malloc=__malloc_hook;
+   __malloc_hook=malloc_hook;
+   ep=(int *)opus_alloc(sizeof(int));
+   if(ep!=NULL)
+   {
+      if(ep)free(ep);
+      __malloc_hook=orig_malloc;
+#endif
+      fprintf(stdout,"    opus_decoder_create() ................... SKIPPED.\n");
+      fprintf(stdout,"    opus_encoder_create() ................... SKIPPED.\n");
+      fprintf(stdout,"    opus_repacketizer_create() .............. SKIPPED.\n");
+      fprintf(stdout,"    opus_multistream_decoder_create() ....... SKIPPED.\n");
+      fprintf(stdout,"    opus_multistream_encoder_create() ....... SKIPPED.\n");
+      fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
+      return 0;
+#ifdef MALLOC_FAIL
+   }
+   for(useerr=0;useerr<2;useerr++)
+   {
+      ep=useerr?&err:0;
+      for(rate=0;rate<5;rate++)
+      {
+        for(c=1;c<3;c++)
+        {
+           err=1;
+           if(useerr)
+           {
+              VG_UNDEF(&err,sizeof(err));
+           }
+           dec=opus_decoder_create(opus_rates[rate], c, ep);
+           if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
+           {
+              __malloc_hook=orig_malloc;
+              test_failed();
+           }
+           cfgs++;
+           msdec=opus_multistream_decoder_create(opus_rates[rate], c, 1, c-1, mapping, ep);
+           if(msdec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
+           {
+              __malloc_hook=orig_malloc;
+              test_failed();
+           }
+           cfgs++;
+           for(app=0;app<3;app++)
+           {
+              if(useerr)
+              {
+                 VG_UNDEF(&err,sizeof(err));
+              }
+              enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
+              if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
+              {
+                 __malloc_hook=orig_malloc;
+                 test_failed();
+              }
+              cfgs++;
+              msenc=opus_multistream_encoder_create(opus_rates[rate], c, 1, c-1, mapping, opus_apps[app],ep);
+              if(msenc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
+              {
+                 __malloc_hook=orig_malloc;
+                 test_failed();
+              }
+              cfgs++;
+           }
+        }
+     }
+   }
+   rp=opus_repacketizer_create();
+   if(rp!=NULL)
+   {
+      __malloc_hook=orig_malloc;
+      test_failed();
+   }
+   cfgs++;
+   __malloc_hook=orig_malloc;
+   fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
+   fprintf(stdout,"    opus_encoder_create() ........................ OK.\n");
+   fprintf(stdout,"    opus_repacketizer_create() ................... OK.\n");
+   fprintf(stdout,"    opus_multistream_decoder_create() ............ OK.\n");
+   fprintf(stdout,"    opus_multistream_encoder_create() ............ OK.\n");
+   fprintf(stdout,"                      All malloc failure tests passed\n");
+   fprintf(stdout,"                                 (%2d API invocations)\n",cfgs);
+   return cfgs;
+#endif
+}
+
+#ifdef MALLOC_FAIL
+#if __GNUC_PREREQ(4,6)
+#pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */
+#endif
+#endif
+
+int main(int _argc, char **_argv)
+{
+   opus_int32 total;
+   const char * oversion;
+   if(_argc>1)
+   {
+      fprintf(stderr,"Usage: %s\n",_argv[0]);
+      return 1;
+   }
+   iseed=0;
+
+   oversion=opus_get_version_string();
+   if(!oversion)test_failed();
+   fprintf(stderr,"Testing the %s API deterministically\n", oversion);
+   if(opus_strerror(-32768)==NULL)test_failed();
+   if(opus_strerror(32767)==NULL)test_failed();
+   if(strlen(opus_strerror(0))<1)test_failed();
+   total=4;
+
+   total+=test_dec_api();
+   total+=test_msdec_api();
+   total+=test_parse();
+   total+=test_enc_api();
+   total+=test_repacketizer_api();
+   total+=test_malloc_fail();
+
+   fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
+
+   return 0;
+}
diff --git a/jni/libopus/tests/test_opus_api.vcxproj b/jni/libopus/tests/test_opus_api.vcxproj
new file mode 100644
index 0000000..acecdde
--- /dev/null
+++ b/jni/libopus/tests/test_opus_api.vcxproj
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <Configuration>Debug</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="test_opus_api.c" />
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <ProjectGuid>{1D257A17-D254-42E5-82D6-1C87A6EC775A}</ProjectGuid>
+    <Keyword>Win32Proj</Keyword>
+    <RootNamespace>test_opus_api</RootNamespace>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <PropertyGroup Label="UserMacros" />
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <PreprocessorDefinitions>HAVE_CONFIG_H;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <AdditionalIncludeDirectories>../silk;../celt;../win32;../include;</AdditionalIncludeDirectories>
+      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>$(SolutionDir)$(Configuration)\opus.lib;$(SolutionDir)$(Configuration)\celt.lib;$(SolutionDir)$(Configuration)\silk_common.lib;$(SolutionDir)$(Configuration)\silk_float.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>HAVE_CONFIG_H;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+      <AdditionalIncludeDirectories>../silk;../celt;../win32;../include;</AdditionalIncludeDirectories>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>$(SolutionDir)$(Configuration)\opus.lib;$(SolutionDir)$(Configuration)\celt.lib;$(SolutionDir)$(Configuration)\silk_common.lib;$(SolutionDir)$(Configuration)\silk_float.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>
diff --git a/jni/libopus/tests/test_opus_api.vcxproj.filters b/jni/libopus/tests/test_opus_api.vcxproj.filters
new file mode 100644
index 0000000..66498da
--- /dev/null
+++ b/jni/libopus/tests/test_opus_api.vcxproj.filters
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <Filter Include="Source Files">
+      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+    </Filter>
+    <Filter Include="Header Files">
+      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+      <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
+    </Filter>
+    <Filter Include="Resource Files">
+      <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
+      <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
+    </Filter>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="test_opus_api.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+  </ItemGroup>
+</Project>
\ No newline at end of file
diff --git a/jni/libopus/tests/test_opus_common.h b/jni/libopus/tests/test_opus_common.h
new file mode 100644
index 0000000..d1e6b45
--- /dev/null
+++ b/jni/libopus/tests/test_opus_common.h
@@ -0,0 +1,78 @@
+/* Copyright (c) 2011 Xiph.Org Foundation
+   Written by Gregory Maxwell */
+/*
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+   - Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+static inline void deb2_impl(unsigned char *_t,unsigned char **_p,int _k,int _x,int _y)
+{
+  int i;
+  if(_x>2){
+     if(_y<3)for(i=0;i<_y;i++)*(--*_p)=_t[i+1];
+  }else{
+     _t[_x]=_t[_x-_y];
+     deb2_impl(_t,_p,_k,_x+1,_y);
+     for(i=_t[_x-_y]+1;i<_k;i++){
+       _t[_x]=i;
+       deb2_impl(_t,_p,_k,_x+1,_x);
+     }
+  }
+}
+
+/*Generates a De Bruijn sequence (k,2) with length k^2*/
+static inline void debruijn2(int _k, unsigned char *_res)
+{
+   unsigned char *p;
+   unsigned char *t;
+   t=malloc(sizeof(unsigned char)*_k*2);
+   memset(t,0,sizeof(unsigned char)*_k*2);
+   p=&_res[_k*_k];
+   deb2_impl(t,&p,_k,1,1);
+   free(t);
+}
+
+/*MWC RNG of George Marsaglia*/
+static opus_uint32 Rz, Rw;
+static inline opus_uint32 fast_rand(void)
+{
+  Rz=36969*(Rz&65535)+(Rz>>16);
+  Rw=18000*(Rw&65535)+(Rw>>16);
+  return (Rz<<16)+Rw;
+}
+static opus_uint32 iseed;
+
+#ifdef __GNUC__
+__attribute__((noreturn))
+#endif
+static inline void _test_failed(const char *file, int line)
+{
+  fprintf(stderr,"\n ***************************************************\n");
+  fprintf(stderr," ***         A fatal error was detected.         ***\n");
+  fprintf(stderr," ***************************************************\n");
+  fprintf(stderr,"Please report this failure and include\n");
+  fprintf(stderr,"'make check SEED=%u fails %s at line %d for %s'\n",iseed,file,line,opus_get_version_string());
+  fprintf(stderr,"and any relevant details about your system.\n\n");
+  abort();
+}
+#define test_failed() _test_failed(__FILE__, __LINE__);
diff --git a/jni/libopus/tests/test_opus_decode.c b/jni/libopus/tests/test_opus_decode.c
new file mode 100644
index 0000000..e2c04c2
--- /dev/null
+++ b/jni/libopus/tests/test_opus_decode.c
@@ -0,0 +1,406 @@
+/* Copyright (c) 2011 Xiph.Org Foundation
+   Written by Gregory Maxwell */
+/*
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+   - Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <stdint.h>
+#include <math.h>
+#include <string.h>
+#include <time.h>
+#if (!defined WIN32 && !defined _WIN32) || defined(__MINGW32__)
+#include <unistd.h>
+#else
+#include <process.h>
+#define getpid _getpid
+#endif
+#include "opus.h"
+#include "test_opus_common.h"
+
+#define MAX_PACKET (1500)
+#define MAX_FRAME_SAMP (5760)
+
+int test_decoder_code0(int no_fuzz)
+{
+   static const opus_int32 fsv[5]={48000,24000,16000,12000,8000};
+   int err,skip,plen;
+   int out_samples,fec;
+   int t;
+   opus_int32 i;
+   OpusDecoder *dec[5*2];
+   opus_int32 decsize;
+   OpusDecoder *decbak;
+   opus_uint32 dec_final_range1,dec_final_range2,dec_final_acc;
+   unsigned char *packet;
+   unsigned char modes[4096];
+   short *outbuf_int;
+   short *outbuf;
+
+   dec_final_range1=dec_final_range2=2;
+
+   packet=malloc(sizeof(unsigned char)*MAX_PACKET);
+   if(packet==NULL)test_failed();
+
+   outbuf_int=malloc(sizeof(short)*(MAX_FRAME_SAMP+16)*2);
+   for(i=0;i<(MAX_FRAME_SAMP+16)*2;i++)outbuf_int[i]=32749;
+   outbuf=&outbuf_int[8*2];
+
+   fprintf(stdout,"  Starting %d decoders...\n",5*2);
+   for(t=0;t<5*2;t++)
+   {
+      int fs=fsv[t>>1];
+      int c=(t&1)+1;
+      err=OPUS_INTERNAL_ERROR;
+      dec[t] = opus_decoder_create(fs, c, &err);
+      if(err!=OPUS_OK || dec[t]==NULL)test_failed();
+      fprintf(stdout,"    opus_decoder_create(%5d,%d) OK. Copy ",fs,c);
+      {
+         OpusDecoder *dec2;
+         /*The opus state structures contain no pointers and can be freely copied*/
+         dec2=(OpusDecoder *)malloc(opus_decoder_get_size(c));
+         if(dec2==NULL)test_failed();
+         memcpy(dec2,dec[t],opus_decoder_get_size(c));
+         memset(dec[t],255,opus_decoder_get_size(c));
+         opus_decoder_destroy(dec[t]);
+         printf("OK.\n");
+         dec[t]=dec2;
+      }
+   }
+
+   decsize=opus_decoder_get_size(1);
+   decbak=(OpusDecoder *)malloc(decsize);
+   if(decbak==NULL)test_failed();
+
+   for(t=0;t<5*2;t++)
+   {
+      int factor=48000/fsv[t>>1];
+      for(fec=0;fec<2;fec++)
+      {
+         int dur;
+         /*Test PLC on a fresh decoder*/
+         out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor, fec);
+         if(out_samples!=120/factor)test_failed();
+         if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
+         if(dur!=120/factor)test_failed();
+
+         /*Test null pointer input*/
+         out_samples = opus_decode(dec[t], 0, -1, outbuf, 120/factor, fec);
+         if(out_samples!=120/factor)test_failed();
+         out_samples = opus_decode(dec[t], 0, 1, outbuf, 120/factor, fec);
+         if(out_samples!=120/factor)test_failed();
+         out_samples = opus_decode(dec[t], 0, 10, outbuf, 120/factor, fec);
+         if(out_samples!=120/factor)test_failed();
+         out_samples = opus_decode(dec[t], 0, fast_rand(), outbuf, 120/factor, fec);
+         if(out_samples!=120/factor)test_failed();
+         if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
+         if(dur!=120/factor)test_failed();
+
+         /*Zero lengths*/
+         out_samples = opus_decode(dec[t], packet, 0, outbuf, 120/factor, fec);
+         if(out_samples!=120/factor)test_failed();
+
+         /*Zero buffer*/
+         outbuf[0]=32749;
+         out_samples = opus_decode(dec[t], packet, 0, outbuf, 0, fec);
+         if(out_samples>0)test_failed();
+         out_samples = opus_decode(dec[t], packet, 0, 0, 0, fec);
+         if(out_samples>0)test_failed();
+         if(outbuf[0]!=32749)test_failed();
+
+         /*Invalid lengths*/
+         out_samples = opus_decode(dec[t], packet, -1, outbuf, MAX_FRAME_SAMP, fec);
+         if(out_samples>=0)test_failed();
+         out_samples = opus_decode(dec[t], packet, INT_MIN, outbuf, MAX_FRAME_SAMP, fec);
+         if(out_samples>=0)test_failed();
+         out_samples = opus_decode(dec[t], packet, -1, outbuf, -1, fec);
+         if(out_samples>=0)test_failed();
+
+         /*Crazy FEC values*/
+         out_samples = opus_decode(dec[t], packet, 1, outbuf, MAX_FRAME_SAMP, fec?-1:2);
+         if(out_samples>=0)test_failed();
+
+         /*Reset the decoder*/
+         if(opus_decoder_ctl(dec[t], OPUS_RESET_STATE)!=OPUS_OK)test_failed();
+      }
+   }
+   fprintf(stdout,"  dec[all] initial frame PLC OK.\n");
+
+   /*Count code 0 tests*/
+   for(i=0;i<64;i++)
+   {
+      int dur;
+      int j,expected[5*2];
+      packet[0]=i<<2;
+      packet[1]=255;
+      packet[2]=255;
+      err=opus_packet_get_nb_channels(packet);
+      if(err!=(i&1)+1)test_failed();
+
+      for(t=0;t<5*2;t++){
+         expected[t]=opus_decoder_get_nb_samples(dec[t],packet,1);
+         if(expected[t]>2880)test_failed();
+      }
+
+      for(j=0;j<256;j++)
+      {
+         packet[1]=j;
+         for(t=0;t<5*2;t++)
+         {
+            out_samples = opus_decode(dec[t], packet, 3, outbuf, MAX_FRAME_SAMP, 0);
+            if(out_samples!=expected[t])test_failed();
+            if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
+            if(dur!=out_samples)test_failed();
+            opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1));
+            if(t==0)dec_final_range2=dec_final_range1;
+            else if(dec_final_range1!=dec_final_range2)test_failed();
+         }
+      }
+
+      for(t=0;t<5*2;t++){
+         int factor=48000/fsv[t>>1];
+         /* The PLC is run for 6 frames in order to get better PLC coverage. */
+         for(j=0;j<6;j++)
+         {
+            out_samples = opus_decode(dec[t], 0, 0, outbuf, expected[t], 0);
+            if(out_samples!=expected[t])test_failed();
+            if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
+            if(dur!=out_samples)test_failed();
+         }
+         /* Run the PLC once at 2.5ms, as a simulation of someone trying to
+            do small drift corrections. */
+         if(expected[t]!=120/factor)
+         {
+            out_samples = opus_decode(dec[t], 0, 0, outbuf, 120/factor, 0);
+            if(out_samples!=120/factor)test_failed();
+            if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
+            if(dur!=out_samples)test_failed();
+         }
+         out_samples = opus_decode(dec[t], packet, 2, outbuf, expected[t]-1, 0);
+         if(out_samples>0)test_failed();
+      }
+   }
+   fprintf(stdout,"  dec[all] all 2-byte prefix for length 3 and PLC, all modes (64) OK.\n");
+
+   if(no_fuzz)
+   {
+      fprintf(stdout,"  Skipping many tests which fuzz the decoder as requested.\n");
+      free(decbak);
+      for(t=0;t<5*2;t++)opus_decoder_destroy(dec[t]);
+      printf("  Decoders stopped.\n");
+
+      err=0;
+      for(i=0;i<8*2;i++)err|=outbuf_int[i]!=32749;
+      for(i=MAX_FRAME_SAMP*2;i<(MAX_FRAME_SAMP+8)*2;i++)err|=outbuf[i]!=32749;
+      if(err)test_failed();
+
+      free(outbuf_int);
+      free(packet);
+      return 0;
+   }
+
+   {
+     /*We only test a subset of the modes here simply because the longer
+       durations end up taking a long time.*/
+      static const int cmodes[4]={16,20,24,28};
+      static const opus_uint32 cres[4]={116290185,2172123586,2172123586,2172123586};
+      static const opus_uint32 lres[3]={3285687739,1481572662,694350475};
+      static const int lmodes[3]={0,4,8};
+      int mode=fast_rand()%4;
+
+      packet[0]=cmodes[mode]<<3;
+      dec_final_acc=0;
+      t=fast_rand()%10;
+
+      for(i=0;i<65536;i++)
+      {
+         int factor=48000/fsv[t>>1];
+         packet[1]=i>>8;
+         packet[2]=i&255;
+         packet[3]=255;
+         out_samples = opus_decode(dec[t], packet, 4, outbuf, MAX_FRAME_SAMP, 0);
+         if(out_samples!=120/factor)test_failed();
+         opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1));
+         dec_final_acc+=dec_final_range1;
+      }
+      if(dec_final_acc!=cres[mode])test_failed();
+      fprintf(stdout,"  dec[%3d] all 3-byte prefix for length 4, mode %2d OK.\n",t,cmodes[mode]);
+
+      mode=fast_rand()%3;
+      packet[0]=lmodes[mode]<<3;
+      dec_final_acc=0;
+      t=fast_rand()%10;
+      for(i=0;i<65536;i++)
+      {
+         int factor=48000/fsv[t>>1];
+         packet[1]=i>>8;
+         packet[2]=i&255;
+         packet[3]=255;
+         out_samples = opus_decode(dec[t], packet, 4, outbuf, MAX_FRAME_SAMP, 0);
+         if(out_samples!=480/factor)test_failed();
+         opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1));
+         dec_final_acc+=dec_final_range1;
+      }
+      if(dec_final_acc!=lres[mode])test_failed();
+      fprintf(stdout,"  dec[%3d] all 3-byte prefix for length 4, mode %2d OK.\n",t,lmodes[mode]);
+   }
+
+   skip=fast_rand()%7;
+   for(i=0;i<64;i++)
+   {
+      int j,expected[5*2];
+      packet[0]=i<<2;
+      for(t=0;t<5*2;t++)expected[t]=opus_decoder_get_nb_samples(dec[t],packet,1);
+      for(j=2+skip;j<1275;j+=4)
+      {
+         int jj;
+         for(jj=0;jj<j;jj++)packet[jj+1]=fast_rand()&255;
+         for(t=0;t<5*2;t++)
+         {
+            out_samples = opus_decode(dec[t], packet, j+1, outbuf, MAX_FRAME_SAMP, 0);
+            if(out_samples!=expected[t])test_failed();
+            opus_decoder_ctl(dec[t], OPUS_GET_FINAL_RANGE(&dec_final_range1));
+            if(t==0)dec_final_range2=dec_final_range1;
+            else if(dec_final_range1!=dec_final_range2)test_failed();
+         }
+      }
+   }
+   fprintf(stdout,"  dec[all] random packets, all modes (64), every 8th size from from %d bytes to maximum OK.\n",2+skip);
+
+   debruijn2(64,modes);
+   plen=(fast_rand()%18+3)*8+skip+3;
+   for(i=0;i<4096;i++)
+   {
+      int j,expected[5*2];
+      packet[0]=modes[i]<<2;
+      for(t=0;t<5*2;t++)expected[t]=opus_decoder_get_nb_samples(dec[t],packet,plen);
+      for(j=0;j<plen;j++)packet[j+1]=(fast_rand()|fast_rand())&255;
+      memcpy(decbak,dec[0],decsize);
+      if(opus_decode(decbak, packet, plen+1, outbuf, expected[0], 1)!=expected[0])test_failed();
+      memcpy(decbak,dec[0],decsize);
+      if(opus_decode(decbak,  0, 0, outbuf, MAX_FRAME_SAMP, 1)<20)test_failed();
+      memcpy(decbak,dec[0],decsize);
+      if(opus_decode(decbak,  0, 0, outbuf, MAX_FRAME_SAMP, 0)<20)test_failed();
+      for(t=0;t<5*2;t++)
+      {
+         int dur;
+         out_samples = opus_decode(dec[t], packet, plen+1, outbuf, MAX_FRAME_SAMP, 0);
+         if(out_samples!=expected[t])test_failed();
+         if(t==0)dec_final_range2=dec_final_range1;
+         else if(dec_final_range1!=dec_final_range2)test_failed();
+         if(opus_decoder_ctl(dec[t], OPUS_GET_LAST_PACKET_DURATION(&dur))!=OPUS_OK)test_failed();
+         if(dur!=out_samples)test_failed();
+      }
+   }
+   fprintf(stdout,"  dec[all] random packets, all mode pairs (4096), %d bytes/frame OK.\n",plen+1);
+
+   plen=(fast_rand()%18+3)*8+skip+3;
+   t=rand()&3;
+   for(i=0;i<4096;i++)
+   {
+      int count,j,expected;
+      packet[0]=modes[i]<<2;
+      expected=opus_decoder_get_nb_samples(dec[t],packet,plen);
+      for(count=0;count<10;count++)
+      {
+         for(j=0;j<plen;j++)packet[j+1]=(fast_rand()|fast_rand())&255;
+         out_samples = opus_decode(dec[t], packet, plen+1, outbuf, MAX_FRAME_SAMP, 0);
+         if(out_samples!=expected)test_failed();
+      }
+   }
+   fprintf(stdout,"  dec[%3d] random packets, all mode pairs (4096)*10, %d bytes/frame OK.\n",t,plen+1);
+
+   {
+      int tmodes[1]={25<<2};
+      opus_uint32 tseeds[1]={140441};
+      int tlen[1]={157};
+      opus_int32 tret[1]={480};
+      t=fast_rand()&1;
+      for(i=0;i<1;i++)
+      {
+         int j;
+         packet[0]=tmodes[i];
+         Rw=Rz=tseeds[i];
+         for(j=1;j<tlen[i];j++)packet[j]=fast_rand()&255;
+         out_samples=opus_decode(dec[t], packet, tlen[i], outbuf, MAX_FRAME_SAMP, 0);
+         if(out_samples!=tret[i])test_failed();
+      }
+      fprintf(stdout,"  dec[%3d] pre-selected random packets OK.\n",t);
+   }
+
+   free(decbak);
+   for(t=0;t<5*2;t++)opus_decoder_destroy(dec[t]);
+   printf("  Decoders stopped.\n");
+
+   err=0;
+   for(i=0;i<8*2;i++)err|=outbuf_int[i]!=32749;
+   for(i=MAX_FRAME_SAMP*2;i<(MAX_FRAME_SAMP+8)*2;i++)err|=outbuf[i]!=32749;
+   if(err)test_failed();
+
+   free(outbuf_int);
+   free(packet);
+   return 0;
+}
+
+int main(int _argc, char **_argv)
+{
+   const char * oversion;
+   const char * env_seed;
+   int env_used;
+
+   if(_argc>2)
+   {
+      fprintf(stderr,"Usage: %s [<seed>]\n",_argv[0]);
+      return 1;
+   }
+
+   env_used=0;
+   env_seed=getenv("SEED");
+   if(_argc>1)iseed=atoi(_argv[1]);
+   else if(env_seed)
+   {
+      iseed=atoi(env_seed);
+      env_used=1;
+   }
+   else iseed=(opus_uint32)time(NULL)^((getpid()&65535)<<16);
+   Rw=Rz=iseed;
+
+   oversion=opus_get_version_string();
+   if(!oversion)test_failed();
+   fprintf(stderr,"Testing %s decoder. Random seed: %u (%.4X)\n", oversion, iseed, fast_rand() % 65535);
+   if(env_used)fprintf(stderr,"  Random seed set from the environment (SEED=%s).\n", env_seed);
+
+   /*Setting TEST_OPUS_NOFUZZ tells the tool not to send garbage data
+     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);
+
+   return 0;
+}
diff --git a/jni/libopus/tests/test_opus_decode.vcxproj b/jni/libopus/tests/test_opus_decode.vcxproj
new file mode 100644
index 0000000..17d3043
--- /dev/null
+++ b/jni/libopus/tests/test_opus_decode.vcxproj
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <Configuration>Debug</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="test_opus_decode.c" />
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <ProjectGuid>{8578322A-1883-486B-B6FA-E0094B65C9F2}</ProjectGuid>
+    <Keyword>Win32Proj</Keyword>
+    <RootNamespace>test_opus_api</RootNamespace>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <PropertyGroup Label="UserMacros" />
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <PreprocessorDefinitions>HAVE_CONFIG_H;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <AdditionalIncludeDirectories>../silk;../celt;../win32;../include;</AdditionalIncludeDirectories>
+      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>$(SolutionDir)$(Configuration)\opus.lib;$(SolutionDir)$(Configuration)\celt.lib;$(SolutionDir)$(Configuration)\silk_common.lib;$(SolutionDir)$(Configuration)\silk_float.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>HAVE_CONFIG_H;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+      <AdditionalIncludeDirectories>../silk;../celt;../win32;../include;</AdditionalIncludeDirectories>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>$(SolutionDir)$(Configuration)\opus.lib;$(SolutionDir)$(Configuration)\celt.lib;$(SolutionDir)$(Configuration)\silk_common.lib;$(SolutionDir)$(Configuration)\silk_float.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>
diff --git a/jni/libopus/tests/test_opus_decode.vcxproj.filters b/jni/libopus/tests/test_opus_decode.vcxproj.filters
new file mode 100644
index 0000000..595c6d4
--- /dev/null
+++ b/jni/libopus/tests/test_opus_decode.vcxproj.filters
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <Filter Include="Source Files">
+      <UniqueIdentifier>{4a0dd677-931f-4728-afe5-b761149fc7eb}</UniqueIdentifier>
+      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+    </Filter>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="test_opus_decode.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+  </ItemGroup>
+</Project>
\ No newline at end of file
diff --git a/jni/libopus/tests/test_opus_encode.c b/jni/libopus/tests/test_opus_encode.c
new file mode 100644
index 0000000..b80def3
--- /dev/null
+++ b/jni/libopus/tests/test_opus_encode.c
@@ -0,0 +1,444 @@
+/* Copyright (c) 2011 Xiph.Org Foundation
+   Written by Gregory Maxwell */
+/*
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+   - Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <stdint.h>
+#include <math.h>
+#include <string.h>
+#include <time.h>
+#if (!defined WIN32 && !defined _WIN32) || defined(__MINGW32__)
+#include <unistd.h>
+#else
+#include <process.h>
+#define getpid _getpid
+#endif
+#include "opus_multistream.h"
+#include "opus.h"
+#include "../src/opus_private.h"
+#include "test_opus_common.h"
+
+#define MAX_PACKET (1500)
+#define SAMPLES (48000*30)
+#define SSAMPLES (SAMPLES/3)
+#define MAX_FRAME_SAMP (5760)
+
+#define PI (3.141592653589793238462643f)
+
+void generate_music(short *buf, opus_int32 len)
+{
+   opus_int32 a1,b1,a2,b2;
+   opus_int32 c1,c2,d1,d2;
+   opus_int32 i,j;
+   a1=b1=a2=b2=0;
+   c1=c2=d1=d2=0;
+   j=0;
+   /*60ms silence*/
+   for(i=0;i<2880;i++)buf[i*2]=buf[i*2+1]=0;
+   for(i=2880;i<len;i++)
+   {
+     opus_uint32 r;
+     opus_int32 v1,v2;
+     v1=v2=(((j*((j>>12)^((j>>10|j>>12)&26&j>>7)))&128)+128)<<15;
+     r=fast_rand();v1+=r&65535;v1-=r>>16;
+     r=fast_rand();v2+=r&65535;v2-=r>>16;
+     b1=v1-a1+((b1*61+32)>>6);a1=v1;
+     b2=v2-a2+((b2*61+32)>>6);a2=v2;
+     c1=(30*(c1+b1+d1)+32)>>6;d1=b1;
+     c2=(30*(c2+b2+d2)+32)>>6;d2=b2;
+     v1=(c1+128)>>8;
+     v2=(c2+128)>>8;
+     buf[i*2]=v1>32767?32767:(v1<-32768?-32768:v1);
+     buf[i*2+1]=v2>32767?32767:(v2<-32768?-32768:v2);
+     if(i%6==0)j++;
+   }
+}
+
+#if 0
+static int save_ctr = 0;
+static void int_to_char(opus_uint32 i, unsigned char ch[4])
+{
+    ch[0] = i>>24;
+    ch[1] = (i>>16)&0xFF;
+    ch[2] = (i>>8)&0xFF;
+    ch[3] = i&0xFF;
+}
+
+static inline void save_packet(unsigned char* p, int len, opus_uint32 rng)
+{
+   FILE *fout;
+   unsigned char int_field[4];
+   char name[256];
+   snprintf(name,255,"test_opus_encode.%llu.%d.bit",(unsigned long long)iseed,save_ctr);
+   fprintf(stdout,"writing %d byte packet to %s\n",len,name);
+   fout=fopen(name, "wb+");
+   if(fout==NULL)test_failed();
+   int_to_char(len, int_field);
+   fwrite(int_field, 1, 4, fout);
+   int_to_char(rng, int_field);
+   fwrite(int_field, 1, 4, fout);
+   fwrite(p, 1, len, fout);
+   fclose(fout);
+   save_ctr++;
+}
+#endif
+
+int run_test1(int no_fuzz)
+{
+   static const int fsizes[6]={960*3,960*2,120,240,480,960};
+   static const char *mstrings[3] = {"    LP","Hybrid","  MDCT"};
+   unsigned char mapping[256] = {0,1,255};
+   unsigned char db62[36];
+   opus_int32 i;
+   int rc,j,err;
+   OpusEncoder *enc;
+   OpusMSEncoder *MSenc;
+   OpusDecoder *dec;
+   OpusMSDecoder *MSdec;
+   OpusMSDecoder *MSdec_err;
+   OpusDecoder *dec_err[10];
+   short *inbuf;
+   short *outbuf;
+   short *out2buf;
+   opus_int32 bitrate_bps;
+   unsigned char packet[MAX_PACKET];
+   opus_uint32 enc_final_range;
+   opus_uint32 dec_final_range;
+   int fswitch;
+   int fsize;
+   int count;
+
+  /*FIXME: encoder api tests, fs!=48k, mono, VBR*/
+
+   fprintf(stdout,"  Encode+Decode tests.\n");
+
+   enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
+   if(err != OPUS_OK || enc==NULL)test_failed();
+
+   for(i=0;i<2;i++)
+   {
+      int *ret_err;
+      ret_err = i?0:&err;
+      MSenc = opus_multistream_encoder_create(8000, 2, 2, 0, mapping, OPUS_UNIMPLEMENTED, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(8000, 0, 1, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(44100, 2, 2, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(8000, 2, 2, 3, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(8000, 2, -1, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(8000, 256, 2, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+   }
+
+   MSenc = opus_multistream_encoder_create(8000, 2, 2, 0, mapping, OPUS_APPLICATION_AUDIO, &err);
+   if(err != OPUS_OK || MSenc==NULL)test_failed();
+
+   /*Some multistream encoder API tests*/
+   if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
+   if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_LSB_DEPTH(&i))!=OPUS_OK)test_failed();
+   if(i<16)test_failed();
+
+   {
+      OpusEncoder *tmp_enc;
+      if(opus_multistream_encoder_ctl(MSenc,  OPUS_MULTISTREAM_GET_ENCODER_STATE(1,&tmp_enc))!=OPUS_OK)test_failed();
+      if(opus_encoder_ctl(tmp_enc, OPUS_GET_LSB_DEPTH(&j))!=OPUS_OK)test_failed();
+      if(i!=j)test_failed();
+      if(opus_multistream_encoder_ctl(MSenc,  OPUS_MULTISTREAM_GET_ENCODER_STATE(2,&tmp_enc))!=OPUS_BAD_ARG)test_failed();
+   }
+
+   dec = opus_decoder_create(48000, 2, &err);
+   if(err != OPUS_OK || dec==NULL)test_failed();
+
+   MSdec = opus_multistream_decoder_create(48000, 2, 2, 0, mapping, &err);
+   if(err != OPUS_OK || MSdec==NULL)test_failed();
+
+   MSdec_err = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, &err);
+   if(err != OPUS_OK || MSdec_err==NULL)test_failed();
+
+   dec_err[0]=(OpusDecoder *)malloc(opus_decoder_get_size(2));
+   memcpy(dec_err[0],dec,opus_decoder_get_size(2));
+   dec_err[1] = opus_decoder_create(48000, 1, &err);
+   dec_err[2] = opus_decoder_create(24000, 2, &err);
+   dec_err[3] = opus_decoder_create(24000, 1, &err);
+   dec_err[4] = opus_decoder_create(16000, 2, &err);
+   dec_err[5] = opus_decoder_create(16000, 1, &err);
+   dec_err[6] = opus_decoder_create(12000, 2, &err);
+   dec_err[7] = opus_decoder_create(12000, 1, &err);
+   dec_err[8] = opus_decoder_create(8000, 2, &err);
+   dec_err[9] = opus_decoder_create(8000, 1, &err);
+   for(i=0;i<10;i++)if(dec_err[i]==NULL)test_failed();
+
+   {
+      OpusEncoder *enccpy;
+      /*The opus state structures contain no pointers and can be freely copied*/
+      enccpy=(OpusEncoder *)malloc(opus_encoder_get_size(2));
+      memcpy(enccpy,enc,opus_encoder_get_size(2));
+      memset(enc,255,opus_encoder_get_size(2));
+      opus_encoder_destroy(enc);
+      enc=enccpy;
+   }
+
+   inbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
+   outbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
+   out2buf=(short *)malloc(sizeof(short)*MAX_FRAME_SAMP*3);
+   if(inbuf==NULL || outbuf==NULL || out2buf==NULL)test_failed();
+
+   generate_music(inbuf,SAMPLES);
+
+/*   FILE *foo;
+   foo = fopen("foo.sw", "wb+");
+   fwrite(inbuf, 1, SAMPLES*2*2, foo);
+   fclose(foo);*/
+
+   if(opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
+   if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(-2))!=OPUS_BAD_ARG)test_failed();
+
+   for(rc=0;rc<3;rc++)
+   {
+      if(opus_encoder_ctl(enc, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_failed();
+      if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
+      if(opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
+      if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
+      for(j=0;j<13;j++)
+      {
+         int rate;
+         int modes[13]={0,0,0,1,1,1,1,2,2,2,2,2,2};
+         int rates[13]={6000,12000,48000,16000,32000,48000,64000,512000,13000,24000,48000,64000,96000};
+         int frame[13]={960*2,960,480,960,960,960,480,960*3,960*3,960,480,240,120};
+         rate=rates[j]+fast_rand()%rates[j];
+         count=i=0;
+         do {
+            int bw,len,out_samples,frame_size;
+            frame_size=frame[j];
+            if(fast_rand()%50==0)opus_encoder_ctl(enc, OPUS_RESET_STATE);
+            if(fast_rand()%50==0)opus_decoder_ctl(dec, OPUS_RESET_STATE);
+            if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
+            if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY+modes[j]))!=OPUS_OK)test_failed();
+            if(opus_encoder_ctl(enc, OPUS_SET_DTX(fast_rand()&1))!=OPUS_OK)test_failed();
+            if(opus_encoder_ctl(enc, OPUS_SET_BITRATE(rate))!=OPUS_OK)test_failed();
+            if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS((rates[j]>=64000?2:1)))!=OPUS_OK)test_failed();
+            if(opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY((count>>2)%11))!=OPUS_OK)test_failed();
+            if(opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC((fast_rand()&15)&(fast_rand()%15)))!=OPUS_OK)test_failed();
+            bw=modes[j]==0?OPUS_BANDWIDTH_NARROWBAND+(fast_rand()%3):
+               modes[j]==1?OPUS_BANDWIDTH_SUPERWIDEBAND+(fast_rand()&1):
+                           OPUS_BANDWIDTH_NARROWBAND+(fast_rand()%5);
+            if(modes[j]==2&&bw==OPUS_BANDWIDTH_MEDIUMBAND)bw+=3;
+            if(opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(bw))!=OPUS_OK)test_failed();
+            len = opus_encode(enc, &inbuf[i<<1], frame_size, packet, MAX_PACKET);
+            if(len<0 || len>MAX_PACKET)test_failed();
+            if(opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
+            out_samples = opus_decode(dec, packet, len, &outbuf[i<<1], MAX_FRAME_SAMP, 0);
+            if(out_samples!=frame_size)test_failed();
+            if(opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OPUS_OK)test_failed();
+            if(enc_final_range!=dec_final_range)test_failed();
+            /*LBRR decode*/
+            out_samples = opus_decode(dec_err[0], packet, len, out2buf, frame_size, (fast_rand()&3)!=0);
+            if(out_samples!=frame_size)test_failed();
+            out_samples = opus_decode(dec_err[1], packet, (fast_rand()&3)==0?0:len, out2buf, MAX_FRAME_SAMP, (fast_rand()&7)!=0);
+            if(out_samples<120)test_failed();
+            i+=frame_size;
+            count++;
+         }while(i<(SSAMPLES-MAX_FRAME_SAMP));
+         fprintf(stdout,"    Mode %s FB encode %s, %6d bps OK.\n",mstrings[modes[j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
+      }
+   }
+
+   if(opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(OPUS_AUTO))!=OPUS_OK)test_failed();
+   if(opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO))!=OPUS_OK)test_failed();
+   if(opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(0))!=OPUS_OK)test_failed();
+   if(opus_encoder_ctl(enc, OPUS_SET_DTX(0))!=OPUS_OK)test_failed();
+
+   for(rc=0;rc<3;rc++)
+   {
+      if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_failed();
+      if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
+      if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
+      if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
+      for(j=0;j<16;j++)
+      {
+         int rate;
+         int modes[16]={0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2};
+         int rates[16]={4000,12000,32000,8000,16000,32000,48000,88000,4000,12000,32000,8000,16000,32000,48000,88000};
+         int frame[16]={160*1,160,80,160,160,80,40,20,160*1,160,80,160,160,80,40,20};
+         if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0&&j==1))!=OPUS_OK)test_failed();
+         if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY+modes[j]))!=OPUS_OK)test_failed();
+         rate=rates[j]+fast_rand()%rates[j];
+         if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_DTX(fast_rand()&1))!=OPUS_OK)test_failed();
+         if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_BITRATE(rate))!=OPUS_OK)test_failed();
+         count=i=0;
+         do {
+            int len,out_samples,frame_size,loss;
+            frame_size=frame[j];
+            if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_COMPLEXITY((count>>2)%11))!=OPUS_OK)test_failed();
+            if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_PACKET_LOSS_PERC((fast_rand()&15)&(fast_rand()%15)))!=OPUS_OK)test_failed();
+            len = opus_multistream_encode(MSenc, &inbuf[i<<1], frame_size, packet, MAX_PACKET);
+            if(len<0 || len>MAX_PACKET)test_failed();
+            if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
+            out_samples = opus_multistream_decode(MSdec, packet, len, out2buf, MAX_FRAME_SAMP, 0);
+            if(out_samples!=frame_size*6)test_failed();
+            if(opus_multistream_decoder_ctl(MSdec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OPUS_OK)test_failed();
+            if(enc_final_range!=dec_final_range)test_failed();
+            /*LBRR decode*/
+            loss=(fast_rand()&63)==0;
+            out_samples = opus_multistream_decode(MSdec_err, packet, loss?0:len, out2buf, frame_size*6, (fast_rand()&3)!=0);
+            if(out_samples!=(frame_size*6))test_failed();
+            i+=frame_size;
+            count++;
+         }while(i<(SSAMPLES/12-MAX_FRAME_SAMP));
+         fprintf(stdout,"    Mode %s NB dual-mono MS encode %s, %6d bps OK.\n",mstrings[modes[j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
+      }
+   }
+
+   bitrate_bps=512000;
+   fsize=fast_rand()%31;
+   fswitch=100;
+
+   debruijn2(6,db62);
+   count=i=0;
+   do {
+      unsigned char toc;
+      const unsigned char *frames[48];
+      short size[48];
+      int payload_offset;
+      opus_uint32 dec_final_range2;
+      int jj,dec2;
+      int len,out_samples;
+      int frame_size=fsizes[db62[fsize]];
+      opus_int32 offset=i%(SAMPLES-MAX_FRAME_SAMP);
+
+      opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps));
+
+      len = opus_encode(enc, &inbuf[offset<<1], frame_size, packet, MAX_PACKET);
+      if(len<0 || len>MAX_PACKET)test_failed();
+      count++;
+
+      opus_encoder_ctl(enc, OPUS_GET_FINAL_RANGE(&enc_final_range));
+
+      out_samples = opus_decode(dec, packet, len, &outbuf[offset<<1], MAX_FRAME_SAMP, 0);
+      if(out_samples!=frame_size)test_failed();
+
+      opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
+
+      /* compare final range encoder rng values of encoder and decoder */
+      if(dec_final_range!=enc_final_range)test_failed();
+
+      /* We fuzz the packet, but take care not to only corrupt the payload
+         Corrupted headers are tested elsewhere and we need to actually run
+         the decoders in order to compare them. */
+      if(opus_packet_parse(packet,len,&toc,frames,size,&payload_offset)<=0)test_failed();
+      if((fast_rand()&1023)==0)len=0;
+      for(j=(frames[0]-packet);j<len;j++)for(jj=0;jj<8;jj++)packet[j]^=((!no_fuzz)&&((fast_rand()&1023)==0))<<jj;
+      out_samples = opus_decode(dec_err[0], len>0?packet:NULL, len, out2buf, MAX_FRAME_SAMP, 0);
+      if(out_samples<0||out_samples>MAX_FRAME_SAMP)test_failed();
+      if((len>0&&out_samples!=frame_size))test_failed(); /*FIXME use lastframe*/
+
+      opus_decoder_ctl(dec_err[0], OPUS_GET_FINAL_RANGE(&dec_final_range));
+
+      /*randomly select one of the decoders to compare with*/
+      dec2=fast_rand()%9+1;
+      out_samples = opus_decode(dec_err[dec2], len>0?packet:NULL, len, out2buf, MAX_FRAME_SAMP, 0);
+      if(out_samples<0||out_samples>MAX_FRAME_SAMP)test_failed(); /*FIXME, use factor, lastframe for loss*/
+
+      opus_decoder_ctl(dec_err[dec2], OPUS_GET_FINAL_RANGE(&dec_final_range2));
+      if(len>0&&dec_final_range!=dec_final_range2)test_failed();
+
+      fswitch--;
+      if(fswitch<1)
+      {
+        int new_size;
+        fsize=(fsize+1)%36;
+        new_size=fsizes[db62[fsize]];
+        if(new_size==960||new_size==480)fswitch=2880/new_size*(fast_rand()%19+1);
+        else fswitch=(fast_rand()%(2880/new_size))+1;
+      }
+      bitrate_bps=((fast_rand()%508000+4000)+bitrate_bps)>>1;
+      i+=frame_size;
+   }while(i<SAMPLES*4);
+   fprintf(stdout,"    All framesize pairs switching encode, %d frames OK.\n",count);
+
+   opus_encoder_destroy(enc);
+   opus_multistream_encoder_destroy(MSenc);
+   opus_decoder_destroy(dec);
+   opus_multistream_decoder_destroy(MSdec);
+   opus_multistream_decoder_destroy(MSdec_err);
+   for(i=0;i<10;i++)opus_decoder_destroy(dec_err[i]);
+   free(inbuf);
+   free(outbuf);
+   free(out2buf);
+   return 0;
+}
+
+int main(int _argc, char **_argv)
+{
+   const char * oversion;
+   const char * env_seed;
+   int env_used;
+
+   if(_argc>2)
+   {
+      fprintf(stderr,"Usage: %s [<seed>]\n",_argv[0]);
+      return 1;
+   }
+
+   env_used=0;
+   env_seed=getenv("SEED");
+   if(_argc>1)iseed=atoi(_argv[1]);
+   else if(env_seed)
+   {
+      iseed=atoi(env_seed);
+      env_used=1;
+   }
+   else iseed=(opus_uint32)time(NULL)^((getpid()&65535)<<16);
+   Rw=Rz=iseed;
+
+   oversion=opus_get_version_string();
+   if(!oversion)test_failed();
+   fprintf(stderr,"Testing %s encoder. Random seed: %u (%.4X)\n", oversion, iseed, fast_rand() % 65535);
+   if(env_used)fprintf(stderr,"  Random seed set from the environment (SEED=%s).\n", env_seed);
+
+   /*Setting TEST_OPUS_NOFUZZ tells the tool not to send garbage data
+     into the decoders. This is helpful because garbage data
+     may cause the decoders to clip, which angers CLANG IOC.*/
+   run_test1(getenv("TEST_OPUS_NOFUZZ")!=NULL);
+
+   fprintf(stderr,"Tests completed successfully.\n");
+
+   return 0;
+}
diff --git a/jni/libopus/tests/test_opus_encode.vcxproj b/jni/libopus/tests/test_opus_encode.vcxproj
new file mode 100644
index 0000000..7e2471e
--- /dev/null
+++ b/jni/libopus/tests/test_opus_encode.vcxproj
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <Configuration>Debug</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="test_opus_encode.c" />
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <ProjectGuid>{84DAA768-1A38-4312-BB61-4C78BB59E5B8}</ProjectGuid>
+    <Keyword>Win32Proj</Keyword>
+    <RootNamespace>test_opus_api</RootNamespace>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <PropertyGroup Label="UserMacros" />
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <PreprocessorDefinitions>HAVE_CONFIG_H;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <AdditionalIncludeDirectories>../silk;../celt;../win32;../include;</AdditionalIncludeDirectories>
+      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>$(SolutionDir)$(Configuration)\opus.lib;$(SolutionDir)$(Configuration)\celt.lib;$(SolutionDir)$(Configuration)\silk_common.lib;$(SolutionDir)$(Configuration)\silk_float.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>HAVE_CONFIG_H;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+      <AdditionalIncludeDirectories>../silk;../celt;../win32;../include;</AdditionalIncludeDirectories>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>$(SolutionDir)$(Configuration)\opus.lib;$(SolutionDir)$(Configuration)\celt.lib;$(SolutionDir)$(Configuration)\silk_common.lib;$(SolutionDir)$(Configuration)\silk_float.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>
diff --git a/jni/libopus/tests/test_opus_encode.vcxproj.filters b/jni/libopus/tests/test_opus_encode.vcxproj.filters
new file mode 100644
index 0000000..9ec2355
--- /dev/null
+++ b/jni/libopus/tests/test_opus_encode.vcxproj.filters
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <Filter Include="Source Files">
+      <UniqueIdentifier>{546c8d9a-103e-4f78-972b-b44e8d3c8aba}</UniqueIdentifier>
+      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+    </Filter>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="test_opus_encode.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+  </ItemGroup>
+</Project>
\ No newline at end of file