From 56735f8c160a5c648ef1b6a338a8daa6d6ae826c Mon Sep 17 00:00:00 2001 From: Jeff Morriss Date: Tue, 16 Jul 2013 02:04:55 +0000 Subject: Move u3.{h,c} and g711.{h,c} into wsutil. Project g711.h against multiple inclusion and add C++ compatibility wrapper. svn path=/trunk/; revision=50654 --- wsutil/CMakeLists.txt | 2 + wsutil/Makefile.common | 8 +- wsutil/g711.c | 289 +++++++++++++++++++++++++++++++++++++++++++++++++ wsutil/g711.h | 45 ++++++++ wsutil/u3.c | 199 ++++++++++++++++++++++++++++++++++ wsutil/u3.h | 48 ++++++++ 6 files changed, 589 insertions(+), 2 deletions(-) create mode 100644 wsutil/g711.c create mode 100644 wsutil/g711.h create mode 100644 wsutil/u3.c create mode 100644 wsutil/u3.h (limited to 'wsutil') diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt index 0864101435..85fe2a96f5 100644 --- a/wsutil/CMakeLists.txt +++ b/wsutil/CMakeLists.txt @@ -46,6 +46,7 @@ set(WSUTIL_FILES crc8.c crc11.c crcdrm.c + g711.c mpeg-audio.c nstime.c privileges.c @@ -54,6 +55,7 @@ set(WSUTIL_FILES report_err.c tempfile.c type_util.c + u3.c ${WSUTIL_PLATFORM_FILES} ) diff --git a/wsutil/Makefile.common b/wsutil/Makefile.common index dd15f9fb51..8e2903bef5 100644 --- a/wsutil/Makefile.common +++ b/wsutil/Makefile.common @@ -40,6 +40,7 @@ LIBWSUTIL_SRC = \ crc16-plain.c \ crc32.c \ crcdrm.c \ + g711.c \ mpeg-audio.c \ nstime.c \ privileges.c \ @@ -47,7 +48,8 @@ LIBWSUTIL_SRC = \ str_util.c \ report_err.c \ tempfile.c \ - type_util.c + type_util.c \ + u3.c # Header files that are not generated from other files LIBWSUTIL_INCLUDES = \ @@ -61,6 +63,7 @@ LIBWSUTIL_INCLUDES = \ crc16-plain.h \ crc32.h \ crcdrm.h \ + g711.h \ mpeg-audio.h \ nstime.h \ privileges.h \ @@ -68,4 +71,5 @@ LIBWSUTIL_INCLUDES = \ str_util.h \ report_err.h \ tempfile.h \ - type_util.h + type_util.h \ + u3.h diff --git a/wsutil/g711.c b/wsutil/g711.c new file mode 100644 index 0000000000..5ae74148ba --- /dev/null +++ b/wsutil/g711.c @@ -0,0 +1,289 @@ +/* + * $Id$ + * + * This source code is a product of Sun Microsystems, Inc. and is provided + * for unrestricted use. Users may copy or modify this source code without + * charge. + * + * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING + * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR + * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. + * + * Sun source code is provided with no support and without any obligation on + * the part of Sun Microsystems, Inc. to assist in its use, correction, + * modification or enhancement. + * + * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE + * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE + * OR ANY PART THEREOF. + * + * In no event will Sun Microsystems, Inc. be liable for any lost revenue + * or profits or other special, indirect and consequential damages, even if + * Sun has been advised of the possibility of such damages. + * + * Sun Microsystems, Inc. + * 2550 Garcia Avenue + * Mountain View, California 94043 + */ + +#include "g711.h" + +/* + * g711.c + * + * u-law, A-law and linear PCM conversions. + */ +#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */ +#define QUANT_MASK (0xf) /* Quantization field mask. */ +#define NSEGS (8) /* Number of A-law segments. */ +#define SEG_SHIFT (4) /* Left shift for segment number. */ +#define SEG_MASK (0x70) /* Segment field mask. */ + +static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF, + 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF}; + +/* copy from CCITT G.711 specifications */ +unsigned char _u2a[128] = { /* u- to A-law conversions */ + 1, 1, 2, 2, 3, 3, 4, 4, + 5, 5, 6, 6, 7, 7, 8, 8, + 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, + 25, 27, 29, 31, 33, 34, 35, 36, + 37, 38, 39, 40, 41, 42, 43, 44, + 46, 48, 49, 50, 51, 52, 53, 54, + 55, 56, 57, 58, 59, 60, 61, 62, + 64, 65, 66, 67, 68, 69, 70, 71, + 72, 73, 74, 75, 76, 77, 78, 79, + 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 91, 92, 93, 94, 95, 96, + 97, 98, 99, 100, 101, 102, 103, 104, + 105, 106, 107, 108, 109, 110, 111, 112, + 113, 114, 115, 116, 117, 118, 119, 120, + 121, 122, 123, 124, 125, 126, 127, 128}; + +unsigned char _a2u[128] = { /* A- to u-law conversions */ + 1, 3, 5, 7, 9, 11, 13, 15, + 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, + 32, 32, 33, 33, 34, 34, 35, 35, + 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 47, 48, 48, 49, 49, + 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 64, 64, + 65, 66, 67, 68, 69, 70, 71, 72, + 73, 74, 75, 76, 77, 78, 79, 79, + 80, 81, 82, 83, 84, 85, 86, 87, + 88, 89, 90, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 101, 102, 103, + 104, 105, 106, 107, 108, 109, 110, 111, + 112, 113, 114, 115, 116, 117, 118, 119, + 120, 121, 122, 123, 124, 125, 126, 127}; + +static int +search( + int val, + short *table, + int size) +{ + int i; + + for (i = 0; i < size; i++) { + if (val <= *table++) + return (i); + } + return (size); +} + +/* + * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law + * + * linear2alaw() accepts an 16-bit integer and encodes it as A-law data. + * + * Linear Input Code Compressed Code + * ------------------------ --------------- + * 0000000wxyza 000wxyz + * 0000001wxyza 001wxyz + * 000001wxyzab 010wxyz + * 00001wxyzabc 011wxyz + * 0001wxyzabcd 100wxyz + * 001wxyzabcde 101wxyz + * 01wxyzabcdef 110wxyz + * 1wxyzabcdefg 111wxyz + * + * For further information see John C. Bellamy's Digital Telephony, 1982, + * John Wiley & Sons, pps 98-111 and 472-476. + */ +unsigned char +linear2alaw( + int pcm_val) /* 2's complement (16-bit range) */ +{ + int mask; + int seg; + unsigned char aval; + if (pcm_val >= 0) { + mask = 0xD5; /* sign (7th) bit = 1 */ + } else { + mask = 0x55; /* sign bit = 0 */ + pcm_val = -pcm_val - 8; + } + + /* Convert the scaled magnitude to segment number. */ + seg = search(pcm_val, seg_end, 8); + + /* Combine the sign, segment, and quantization bits. */ + + if (seg >= 8) /* out of range, return maximum value. */ + return (0x7F ^ mask); + else { + aval = seg << SEG_SHIFT; + if (seg < 2) + aval |= (pcm_val >> 4) & QUANT_MASK; + else + aval |= (pcm_val >> (seg + 3)) & QUANT_MASK; + return (aval ^ mask); + } +} + +/* + * alaw2linear() - Convert an A-law value to 16-bit linear PCM + * + */ +int +alaw2linear( + unsigned char a_val) +{ + int t; + int seg; + /*printf(" vrednost a_val %X ", a_val);*/ + a_val ^= 0x55; + + t = (a_val & QUANT_MASK) << 4; + seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT; + switch (seg) { + case 0: + t += 8; + break; + case 1: + t += 0x108; + break; + default: + t += 0x108; + t <<= seg - 1; + } + /*printf("izracunan int %d in njegov hex %X \n", t,t);*/ + return ((a_val & SIGN_BIT) ? t : -t); +} + +#define BIAS (0x84) /* Bias for linear code. */ + +/* + * linear2ulaw() - Convert a linear PCM value to u-law + * + * In order to simplify the encoding process, the original linear magnitude + * is biased by adding 33 which shifts the encoding range from (0 - 8158) to + * (33 - 8191). The result can be seen in the following encoding table: + * + * Biased Linear Input Code Compressed Code + * ------------------------ --------------- + * 00000001wxyza 000wxyz + * 0000001wxyzab 001wxyz + * 000001wxyzabc 010wxyz + * 00001wxyzabcd 011wxyz + * 0001wxyzabcde 100wxyz + * 001wxyzabcdef 101wxyz + * 01wxyzabcdefg 110wxyz + * 1wxyzabcdefgh 111wxyz + * + * Each biased linear code has a leading 1 which identifies the segment + * number. The value of the segment number is equal to 7 minus the number + * of leading 0's. The quantization interval is directly available as the + * four bits wxyz. * The trailing bits (a - h) are ignored. + * + * Ordinarily the complement of the resulting code word is used for + * transmission, and so the code word is complemented before it is returned. + * + * For further information see John C. Bellamy's Digital Telephony, 1982, + * John Wiley & Sons, pps 98-111 and 472-476. + */ +unsigned char +linear2ulaw( + int pcm_val) /* 2's complement (16-bit range) */ +{ + int mask; + int seg; + unsigned char uval; + + /* Get the sign and the magnitude of the value. */ + if (pcm_val < 0) { + pcm_val = BIAS - pcm_val; + mask = 0x7F; + } else { + pcm_val += BIAS; + mask = 0xFF; + } + + /* Convert the scaled magnitude to segment number. */ + seg = search(pcm_val, seg_end, 8); + + /* + * Combine the sign, segment, quantization bits; + * and complement the code word. + */ + if (seg >= 8) /* out of range, return maximum value. */ + return (0x7F ^ mask); + else { + uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF); + return (uval ^ mask); + } + +} + +/* + * ulaw2linear() - Convert a u-law value to 16-bit linear PCM + * + * First, a biased linear code is derived from the code word. An unbiased + * output can then be obtained by subtracting 33 from the biased code. + * + * Note that this function expects to be passed the complement of the + * original code word. This is in keeping with ISDN conventions. + */ +int +ulaw2linear( + unsigned char u_val) +{ + int t; + + /* Complement to obtain normal u-law value. */ + u_val = ~u_val; + + /* + * Extract and bias the quantization bits. Then + * shift up by the segment number and subtract out the bias. + */ + t = ((u_val & QUANT_MASK) << 3) + BIAS; + t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; + + return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS)); +} + +/* A-law to u-law conversion */ +/* unsigned char + * alaw2ulaw( + * unsigned char aval) + * { + * aval &= 0xff; + * return ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) : + * (0x7F ^ _a2u[aval ^ 0x55])); + * } + */ + +/* u-law to A-law conversion */ +/* unsigned char + * ulaw2alaw( + * unsigned char uval) + * { + * uval &= 0xff; + * return ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) : + * (0x55 ^ (_u2a[0x7F ^ uval] - 1))); + * } + */ diff --git a/wsutil/g711.h b/wsutil/g711.h new file mode 100644 index 0000000000..8dc7d206ef --- /dev/null +++ b/wsutil/g711.h @@ -0,0 +1,45 @@ +/* + * g711.h + * + * Definitions for routines for u-law, A-law and linear PCM conversions + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __G711_H__ +#define __G711_H__ + +#include "ws_symbol_export.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +WS_DLL_PUBLIC unsigned char linear2alaw( int ); +WS_DLL_PUBLIC int alaw2linear( unsigned char ); +WS_DLL_PUBLIC unsigned char linear2ulaw( int ); +WS_DLL_PUBLIC int ulaw2linear( unsigned char ); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __G711_H__ */ diff --git a/wsutil/u3.c b/wsutil/u3.c new file mode 100644 index 0000000000..217d9f3bac --- /dev/null +++ b/wsutil/u3.c @@ -0,0 +1,199 @@ +/* u3.c + * u3 2006 Graeme Lunt + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* + * Indentation logic: 2-space + */ + + +#include "config.h" + +#include +#include + +#ifdef HAVE_UNISTD_H +#include +#endif + +#ifdef HAVE_FCNTL_H +#include +#endif + +#ifdef _WIN32 +#include /* getpid */ +#endif + +#include + +#include "u3.h" + + +#define U3_DEVICE_PATH_VAR "$U3_DEVICE_PATH" + +static char *pid_file = NULL; +static char *u3devicepath = (char*)-1; +static gchar *newpath = NULL; + +static const char *u3_change_path(const char *path, const char *old, const char *new_u3devicepath); + +gboolean u3_active(void) +{ + + return ( +#ifdef _WIN32 + getenv_utf8 +#else + getenv +#endif + ("U3_HOST_EXEC_PATH") != NULL); + +} + +void u3_runtime_info(GString *str) +{ + + char *u3devicepath_lcl = NULL; + char *u3deviceproduct = NULL; + + if((u3deviceproduct = +#ifdef _WIN32 + getenv_utf8 +#else + getenv +#endif + ("U3_DEVICE_PRODUCT")) != NULL) { + g_string_append(str, " from the "); + g_string_append(str, u3deviceproduct); + } else { + g_string_append(str, " from a "); + } + + g_string_append(str, " U3 device"); + + if((u3devicepath_lcl = +#ifdef _WIN32 + getenv_utf8 +#else + getenv +#endif + ("U3_DEVICE_PATH")) != NULL) { + g_string_append(str, " in drive "); + g_string_append(str, u3devicepath_lcl); + } + +} + +void u3_register_pid(void) +{ + int pid; + int pid_fd; + char *u3hostexecpath; + int pf_size; + + if((u3hostexecpath = +#ifdef _WIN32 + getenv_utf8 +#else + getenv +#endif + ("U3_HOST_EXEC_PATH")) != NULL) { + + pid = getpid(); + + pf_size = (int) strlen(u3hostexecpath) + 32; + pid_file = (char *)g_malloc(pf_size); + + g_snprintf(pid_file, pf_size, "%s\\%d.pid", u3hostexecpath, pid); + + pid_fd = ws_open(pid_file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); + + if(pid_fd != -1) + ws_close(pid_fd); + else { + g_free(pid_file); + pid_file = NULL; + } + } +} + + +void u3_deregister_pid(void) +{ + if(pid_file) { + /* we don't care if we succeed or fail - u3utils may have deleted the file */ + ws_unlink(pid_file); + + g_free(pid_file); + + pid_file = NULL; + + } +} + +const char *u3_expand_device_path(const char *path) +{ + return u3_change_path(path, U3_DEVICE_PATH_VAR, NULL); +} + + +const char *u3_contract_device_path(char *path) +{ + return u3_change_path(path, NULL, U3_DEVICE_PATH_VAR); +} + +static const char *u3_change_path(const char *path, const char *old, const char *new_u3devicepath) +{ + + if(u3devicepath == (char*)-1) { + /* cache the device path */ + u3devicepath = +#ifdef _WIN32 + getenv_utf8 +#else + getenv +#endif + ("U3_DEVICE_PATH"); + } + + if(new_u3devicepath == NULL) + new_u3devicepath = u3devicepath; + if(old == NULL) + old = u3devicepath; + + if(newpath != NULL) { + g_free(newpath); + newpath = NULL; + } + + if((path != NULL) && (u3devicepath != NULL) && (strncmp(path, old, strlen(old)) == 0)) { + + newpath = g_strconcat(new_u3devicepath, path + strlen(old), NULL); + + return newpath; + + } + + return path; + +} diff --git a/wsutil/u3.h b/wsutil/u3.h new file mode 100644 index 0000000000..fac385aebe --- /dev/null +++ b/wsutil/u3.h @@ -0,0 +1,48 @@ +/* u3.h + * u3 2006 Graeme Lunt + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __U3_H__ +#define __U3_H__ + +#include "ws_symbol_export.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +WS_DLL_PUBLIC gboolean u3_active(void); + +WS_DLL_PUBLIC void u3_runtime_info(GString *str); + +WS_DLL_PUBLIC void u3_register_pid(void); +WS_DLL_PUBLIC void u3_deregister_pid(void); + +WS_DLL_PUBLIC const char *u3_expand_device_path(const char *path); +WS_DLL_PUBLIC const char *u3_contract_device_path(char *path); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __U3_H__ */ -- cgit v1.2.3