[PATCH 2/2] sipdroid: add the speex codec

42 views
Skip to first unread message

James Bottomley

unread,
Jan 29, 2010, 1:53:14 PM1/29/10
to sipdroid-...@googlegroups.com
This patch adds some of the speex-1.2rc1 files (enough to build a
narrow band codec) to our JNI. The licence appears to be compatible
with GPL. The end result is a JNI type wrapper like the GSM codec.

[I've cut most of the speex files out of the diff ... they're unmodified
from the original, and I figured the reflector wouldn't like the
additional 10k lines of code]

Signed-off-by: James Bottomley <James.B...@HansenPartnership.com>
---
jni/Android.mk | 35 +
jni/Application.mk | 3 +-
jni/speex-1.2rc1/COPYING | 35 +
jni/speex-1.2rc1/README.android | 5 +
jni/speex-1.2rc1/include/speex/speex.h | 424 +++++
jni/speex-1.2rc1/include/speex/speex_bits.h | 174 ++
jni/speex-1.2rc1/include/speex/speex_callbacks.h | 134 ++
.../include/speex/speex_config_types.h | 11 +
jni/speex-1.2rc1/include/speex/speex_types.h | 126 ++
jni/speex-1.2rc1/libspeex/arch.h | 239 +++
jni/speex-1.2rc1/libspeex/bits.c | 372 ++++
jni/speex-1.2rc1/libspeex/cb_search.c | 612 +++++++
jni/speex-1.2rc1/libspeex/cb_search.h | 103 ++
jni/speex-1.2rc1/libspeex/exc_10_16_table.c | 50 +
jni/speex-1.2rc1/libspeex/exc_10_32_table.c | 66 +
jni/speex-1.2rc1/libspeex/exc_20_32_table.c | 66 +
jni/speex-1.2rc1/libspeex/exc_5_256_table.c | 290 +++
jni/speex-1.2rc1/libspeex/exc_5_64_table.c | 98 +
jni/speex-1.2rc1/libspeex/exc_8_128_table.c | 162 ++
jni/speex-1.2rc1/libspeex/filters.c | 821 +++++++++
jni/speex-1.2rc1/libspeex/filters.h | 90 +
jni/speex-1.2rc1/libspeex/fixed_generic.h | 106 ++
jni/speex-1.2rc1/libspeex/gain_table.c | 160 ++
jni/speex-1.2rc1/libspeex/gain_table_lbr.c | 64 +
jni/speex-1.2rc1/libspeex/high_lsp_tables.c | 163 ++
jni/speex-1.2rc1/libspeex/lpc.c | 201 ++
jni/speex-1.2rc1/libspeex/lpc.h | 53 +
jni/speex-1.2rc1/libspeex/lsp.c | 656 +++++++
jni/speex-1.2rc1/libspeex/lsp.h | 64 +
jni/speex-1.2rc1/libspeex/lsp_tables_nb.c | 360 ++++
jni/speex-1.2rc1/libspeex/ltp.c | 839 +++++++++
jni/speex-1.2rc1/libspeex/ltp.h | 141 ++
jni/speex-1.2rc1/libspeex/math_approx.h | 332 ++++
jni/speex-1.2rc1/libspeex/modes.c | 366 ++++
jni/speex-1.2rc1/libspeex/modes.h | 161 ++
jni/speex-1.2rc1/libspeex/nb_celp.c | 1903 ++++++++++++++++++++
jni/speex-1.2rc1/libspeex/nb_celp.h | 203 +++
jni/speex-1.2rc1/libspeex/os_support.h | 169 ++
jni/speex-1.2rc1/libspeex/quant_lsp.c | 385 ++++
jni/speex-1.2rc1/libspeex/quant_lsp.h | 74 +
jni/speex-1.2rc1/libspeex/sb_celp.h | 155 ++
jni/speex-1.2rc1/libspeex/speex.c | 250 +++
jni/speex-1.2rc1/libspeex/speex_callbacks.c | 144 ++
jni/speex-1.2rc1/libspeex/stack_alloc.h | 115 ++
jni/speex-1.2rc1/libspeex/vbr.c | 275 +++
jni/speex-1.2rc1/libspeex/vbr.h | 70 +
jni/speex-1.2rc1/libspeex/vq.c | 147 ++
jni/speex-1.2rc1/libspeex/vq.h | 54 +
jni/speex-1.2rc1/libspeex/window.c | 102 ++
jni/speex_jni.cpp | 110 ++
src/org/sipdroid/codecs/Codecs.java | 1 +
src/org/sipdroid/codecs/Speex.java | 65 +
52 files changed, 11803 insertions(+), 1 deletions(-)
create mode 100644 jni/speex-1.2rc1/COPYING
create mode 100644 jni/speex-1.2rc1/README.android
create mode 100644 jni/speex-1.2rc1/include/speex/speex.h
create mode 100644 jni/speex-1.2rc1/include/speex/speex_bits.h
create mode 100644 jni/speex-1.2rc1/include/speex/speex_callbacks.h
create mode 100644 jni/speex-1.2rc1/include/speex/speex_config_types.h
create mode 100644 jni/speex-1.2rc1/include/speex/speex_types.h
create mode 100644 jni/speex-1.2rc1/libspeex/arch.h
create mode 100644 jni/speex-1.2rc1/libspeex/bits.c
create mode 100644 jni/speex-1.2rc1/libspeex/cb_search.c
create mode 100644 jni/speex-1.2rc1/libspeex/cb_search.h
create mode 100644 jni/speex-1.2rc1/libspeex/exc_10_16_table.c
create mode 100644 jni/speex-1.2rc1/libspeex/exc_10_32_table.c
create mode 100644 jni/speex-1.2rc1/libspeex/exc_20_32_table.c
create mode 100644 jni/speex-1.2rc1/libspeex/exc_5_256_table.c
create mode 100644 jni/speex-1.2rc1/libspeex/exc_5_64_table.c
create mode 100644 jni/speex-1.2rc1/libspeex/exc_8_128_table.c
create mode 100644 jni/speex-1.2rc1/libspeex/filters.c
create mode 100644 jni/speex-1.2rc1/libspeex/filters.h
create mode 100644 jni/speex-1.2rc1/libspeex/fixed_generic.h
create mode 100644 jni/speex-1.2rc1/libspeex/gain_table.c
create mode 100644 jni/speex-1.2rc1/libspeex/gain_table_lbr.c
create mode 100644 jni/speex-1.2rc1/libspeex/high_lsp_tables.c
create mode 100644 jni/speex-1.2rc1/libspeex/lpc.c
create mode 100644 jni/speex-1.2rc1/libspeex/lpc.h
create mode 100644 jni/speex-1.2rc1/libspeex/lsp.c
create mode 100644 jni/speex-1.2rc1/libspeex/lsp.h
create mode 100644 jni/speex-1.2rc1/libspeex/lsp_tables_nb.c
create mode 100644 jni/speex-1.2rc1/libspeex/ltp.c
create mode 100644 jni/speex-1.2rc1/libspeex/ltp.h
create mode 100644 jni/speex-1.2rc1/libspeex/math_approx.h
create mode 100644 jni/speex-1.2rc1/libspeex/modes.c
create mode 100644 jni/speex-1.2rc1/libspeex/modes.h
create mode 100644 jni/speex-1.2rc1/libspeex/nb_celp.c
create mode 100644 jni/speex-1.2rc1/libspeex/nb_celp.h
create mode 100644 jni/speex-1.2rc1/libspeex/os_support.h
create mode 100644 jni/speex-1.2rc1/libspeex/quant_lsp.c
create mode 100644 jni/speex-1.2rc1/libspeex/quant_lsp.h
create mode 100644 jni/speex-1.2rc1/libspeex/sb_celp.h
create mode 100644 jni/speex-1.2rc1/libspeex/speex.c
create mode 100644 jni/speex-1.2rc1/libspeex/speex_callbacks.c
create mode 100644 jni/speex-1.2rc1/libspeex/stack_alloc.h
create mode 100644 jni/speex-1.2rc1/libspeex/vbr.c
create mode 100644 jni/speex-1.2rc1/libspeex/vbr.h
create mode 100644 jni/speex-1.2rc1/libspeex/vq.c
create mode 100644 jni/speex-1.2rc1/libspeex/vq.h
create mode 100644 jni/speex-1.2rc1/libspeex/window.c
create mode 100644 jni/speex_jni.cpp
create mode 100644 src/org/sipdroid/codecs/Speex.java

diff --git a/jni/Android.mk b/jni/Android.mk
index b3bc8a4..92558d7 100644
--- a/jni/Android.mk
+++ b/jni/Android.mk
@@ -1,4 +1,5 @@
LOCAL_PATH := $(call my-dir)
+SPEEX := speex-1.2rc1

include $(CLEAR_VARS)

@@ -6,3 +7,37 @@ LOCAL_MODULE := OSNetworkSystem
LOCAL_SRC_FILES := OSNetworkSystem.cpp

include $(BUILD_SHARED_LIBRARY)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := speex_jni
+LOCAL_SRC_FILES := speex_jni.cpp \
+ $(SPEEX)/libspeex/speex.c \
+ $(SPEEX)/libspeex/speex_callbacks.c \
+ $(SPEEX)/libspeex/bits.c \
+ $(SPEEX)/libspeex/modes.c \
+ $(SPEEX)/libspeex/nb_celp.c \
+ $(SPEEX)/libspeex/exc_20_32_table.c \
+ $(SPEEX)/libspeex/exc_5_256_table.c \
+ $(SPEEX)/libspeex/exc_5_64_table.c \
+ $(SPEEX)/libspeex/exc_8_128_table.c \
+ $(SPEEX)/libspeex/exc_10_32_table.c \
+ $(SPEEX)/libspeex/exc_10_16_table.c \
+ $(SPEEX)/libspeex/filters.c \
+ $(SPEEX)/libspeex/quant_lsp.c \
+ $(SPEEX)/libspeex/ltp.c \
+ $(SPEEX)/libspeex/lpc.c \
+ $(SPEEX)/libspeex/lsp.c \
+ $(SPEEX)/libspeex/vbr.c \
+ $(SPEEX)/libspeex/gain_table.c \
+ $(SPEEX)/libspeex/gain_table_lbr.c \
+ $(SPEEX)/libspeex/lsp_tables_nb.c \
+ $(SPEEX)/libspeex/cb_search.c \
+ $(SPEEX)/libspeex/vq.c \
+ $(SPEEX)/libspeex/window.c \
+ $(SPEEX)/libspeex/high_lsp_tables.c
+
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(SPEEX)/include
+LOCAL_CFLAGS = -DFIXED_POINT -DEXPORT="" -UHAVE_CONFIG_H
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/jni/Application.mk b/jni/Application.mk
index 627ecc0..8a9ce26 100644
--- a/jni/Application.mk
+++ b/jni/Application.mk
@@ -1,2 +1,3 @@
-APP_PROJECT_PATH := /cygdrive/c/android/sipdroid/
+APP_PROJECT_PATH := $(call my-dir)
APP_MODULES := OSNetworkSystem
+APP_MODULES += speex_jni
diff --git a/jni/speex-1.2rc1/COPYING b/jni/speex-1.2rc1/COPYING
new file mode 100644
index 0000000..de6fbe2
--- /dev/null
+++ b/jni/speex-1.2rc1/COPYING
@@ -0,0 +1,35 @@
+Copyright 2002-2008 Xiph.org Foundation
+Copyright 2002-2008 Jean-Marc Valin
+Copyright 2005-2007 Analog Devices Inc.
+Copyright 2005-2008 Commonwealth Scientific and Industrial Research
+ Organisation (CSIRO)
+Copyright 1993, 2002, 2006 David Rowe
+Copyright 2003 EpicGames
+Copyright 1992-1994 Jutta Degener, Carsten Bormann
+
+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 the Xiph.org Foundation nor the names of its
+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 FOUNDATION 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.
diff --git a/jni/speex-1.2rc1/README.android b/jni/speex-1.2rc1/README.android
new file mode 100644
index 0000000..3e6f21e
--- /dev/null
+++ b/jni/speex-1.2rc1/README.android
@@ -0,0 +1,5 @@
+libspeex version 1.2 rc1
+see http://www.speex.org/
+
+This copy is not complete, it only contains enough files to
+compile a narrow band speex codec (which is all SIP needs)
diff --git a/jni/speex_jni.cpp b/jni/speex_jni.cpp
new file mode 100644
index 0000000..c3e483f
--- /dev/null
+++ b/jni/speex_jni.cpp
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2009 The Sipdroid Open Source Project
+ *
+ * This file is part of Sipdroid (http://www.sipdroid.org)
+ *
+ * Sipdroid is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This source code is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this source code; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <jni.h>
+
+#include <string.h>
+#include <unistd.h>
+
+#include <speex/speex.h>
+
+// the header length of the RTP frame (must skip when en/decoding)
+static const int rtp_header = 12;
+
+static int dec_frame_size;
+static int enc_frame_size;
+
+static SpeexBits ebits, dbits;
+void *enc_state;
+void *dec_state;
+
+static JavaVM *gJavaVM;
+const char *kInterfacePath = "org/sipdroid/pjlib/Speex";
+
+extern "C"
+JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_Speex_open
+ (JNIEnv *env, jobject obj, jint compression) {
+ int tmp;
+
+ speex_bits_init(&ebits);
+ speex_bits_init(&dbits);
+
+ enc_state = speex_encoder_init(&speex_nb_mode);
+ dec_state = speex_decoder_init(&speex_nb_mode);
+ tmp = compression;
+ speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
+ speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &enc_frame_size);
+ speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &dec_frame_size);
+
+ return (jint)0;
+}
+
+extern "C"
+JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_Speex_encode
+ (JNIEnv *env, jobject obj, jshortArray lin, jint offset, jbyteArray encoded, jint size) {
+
+ jshort buffer[enc_frame_size];
+ jbyte output_buffer[enc_frame_size];
+ int nsamples = (size-1)/enc_frame_size + 1;
+ int i, tot_bytes = 0;
+
+ speex_bits_reset(&ebits);
+
+ for (i = 0; i < nsamples; i++) {
+ env->GetShortArrayRegion(lin, offset + i*enc_frame_size,
+ enc_frame_size, buffer);
+ speex_encode_int(enc_state, buffer, &ebits);
+ }
+ // SDP frame terminator
+ speex_bits_pack(&ebits, 0xf, 5);
+ tot_bytes = speex_bits_write(&ebits, (char *)output_buffer,
+ enc_frame_size);
+ env->SetByteArrayRegion(encoded, rtp_header, tot_bytes,
+ output_buffer);
+
+ return (jint)tot_bytes;
+}
+
+extern "C"
+JNIEXPORT jint JNICALL Java_org_sipdroid_codecs_Speex_decode
+ (JNIEnv *env, jobject obj, jbyteArray encoded, jshortArray lin, jint size) {
+
+ jbyte buffer[dec_frame_size];
+ jshort output_buffer[dec_frame_size];
+ jsize encoded_length = size;
+
+ env->GetByteArrayRegion(encoded, rtp_header, encoded_length, buffer);
+ speex_bits_read_from(&dbits, (char *)buffer, encoded_length);
+ speex_decode_int(dec_state, &dbits, output_buffer);
+ env->SetShortArrayRegion(lin, 0, dec_frame_size,
+ output_buffer);
+
+ return (jint)dec_frame_size;
+}
+
+extern "C"
+JNIEXPORT void JNICALL Java_org_sipdroid_codecs_Speex_close
+ (JNIEnv *env, jobject obj) {
+
+ speex_bits_destroy(&ebits);
+ speex_bits_destroy(&dbits);
+ speex_decoder_destroy(dec_state);
+ speex_encoder_destroy(enc_state);
+}
diff --git a/src/org/sipdroid/codecs/Codecs.java b/src/org/sipdroid/codecs/Codecs.java
index dc9d6df..48efee9 100644
--- a/src/org/sipdroid/codecs/Codecs.java
+++ b/src/org/sipdroid/codecs/Codecs.java
@@ -26,6 +26,7 @@ import android.widget.AdapterView.AdapterContextMenuInfo;

public class Codecs {
private static final Vector<Codec> codecs = new Vector<Codec>() {{
+ add(new Speex());
add(new GSM());
add(new alaw());
add(new ulaw());
diff --git a/src/org/sipdroid/codecs/Speex.java b/src/org/sipdroid/codecs/Speex.java
new file mode 100644
index 0000000..755925e
--- /dev/null
+++ b/src/org/sipdroid/codecs/Speex.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2009 The Sipdroid Open Source Project
+ *
+ * This file is part of Sipdroid (http://www.sipdroid.org)
+ *
+ * Sipdroid is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This source code is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this source code; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.sipdroid.codecs;
+
+import java.lang.String;
+import java.util.Map;
+
+import org.sipdroid.sipua.ui.Sipdroid;
+
+class Speex extends CodecBase implements Codec {
+
+ /* quality
+ * 1 : 4kbps (very noticeable artifacts, usually intelligible)
+ * 2 : 6kbps (very noticeable artifacts, good intelligibility)
+ * 4 : 8kbps (noticeable artifacts sometimes)
+ * 6 : 11kpbs (artifacts usually only noticeable with headphones)
+ * 8 : 15kbps (artifacts not usually noticeable)
+ */
+ private static final int DEFAULT_COMPRESSION = 6;
+
+ Speex() {
+ CODEC_NAME = "speex";
+ CODEC_USER_NAME = "speex";
+ CODEC_DESCRIPTION = "11kbit";
+ CODEC_NUMBER = 97;
+
+ load();
+ }
+
+ void load() {
+ try {
+ System.loadLibrary("speex_jni");
+ super.load();
+ } catch (Throwable e) {
+ if (!Sipdroid.release) e.printStackTrace();
+ }
+
+ }
+
+ public native int open(int compression);
+ public native int decode(byte encoded[], short lin[], int size);
+ public native int encode(short lin[], int offset, byte encoded[], int size);
+ public native void close();
+
+ public void init() {
+ open(DEFAULT_COMPRESSION);
+ }
+}
--
1.6.4.2

Reply all
Reply to author
Forward
0 new messages