Path: blob/master/drivers/isdn/mISDN/l1oip_codec.c
15111 views
/*12* l1oip_codec.c generic codec using lookup table3* -> conversion from a-Law to u-Law4* -> conversion from u-Law to a-Law5* -> compression by reducing the number of sample resolution to 46*7* NOTE: It is not compatible with any standard codec like ADPCM.8*9* Author Andreas Eversberg ([email protected])10*11* This program is free software; you can redistribute it and/or modify12* it under the terms of the GNU General Public License as published by13* the Free Software Foundation; either version 2, or (at your option)14* any later version.15*16* This program is distributed in the hope that it will be useful,17* but WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19* GNU General Public License for more details.20*21* You should have received a copy of the GNU General Public License22* along with this program; if not, write to the Free Software23* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.2425*/2627/*2829How the codec works:30--------------------3132The volume is increased to increase the dynamic range of the audio signal.33Each sample is converted to a-LAW with only 16 steps of level resolution.34A pair of two samples are stored in one byte.3536The first byte is stored in the upper bits, the second byte is stored in the37lower bits.3839To speed up compression and decompression, two lookup tables are formed:4041- 16 bits index for two samples (law encoded) with 8 bit compressed result.42- 8 bits index for one compressed data with 16 bits decompressed result.4344NOTE: The bytes are handled as they are law-encoded.4546*/4748#include <linux/vmalloc.h>49#include <linux/mISDNif.h>50#include <linux/in.h>51#include "core.h"52#include "l1oip.h"5354/* definitions of codec. don't use calculations, code may run slower. */5556static u8 *table_com;57static u16 *table_dec;585960/* alaw -> ulaw */61static u8 alaw_to_ulaw[256] =62{630xab, 0x2b, 0xe3, 0x63, 0x8b, 0x0b, 0xc9, 0x49,640xba, 0x3a, 0xf6, 0x76, 0x9b, 0x1b, 0xd7, 0x57,650xa3, 0x23, 0xdd, 0x5d, 0x83, 0x03, 0xc1, 0x41,660xb2, 0x32, 0xeb, 0x6b, 0x93, 0x13, 0xcf, 0x4f,670xaf, 0x2f, 0xe7, 0x67, 0x8f, 0x0f, 0xcd, 0x4d,680xbe, 0x3e, 0xfe, 0x7e, 0x9f, 0x1f, 0xdb, 0x5b,690xa7, 0x27, 0xdf, 0x5f, 0x87, 0x07, 0xc5, 0x45,700xb6, 0x36, 0xef, 0x6f, 0x97, 0x17, 0xd3, 0x53,710xa9, 0x29, 0xe1, 0x61, 0x89, 0x09, 0xc7, 0x47,720xb8, 0x38, 0xf2, 0x72, 0x99, 0x19, 0xd5, 0x55,730xa1, 0x21, 0xdc, 0x5c, 0x81, 0x01, 0xbf, 0x3f,740xb0, 0x30, 0xe9, 0x69, 0x91, 0x11, 0xce, 0x4e,750xad, 0x2d, 0xe5, 0x65, 0x8d, 0x0d, 0xcb, 0x4b,760xbc, 0x3c, 0xfa, 0x7a, 0x9d, 0x1d, 0xd9, 0x59,770xa5, 0x25, 0xde, 0x5e, 0x85, 0x05, 0xc3, 0x43,780xb4, 0x34, 0xed, 0x6d, 0x95, 0x15, 0xd1, 0x51,790xac, 0x2c, 0xe4, 0x64, 0x8c, 0x0c, 0xca, 0x4a,800xbb, 0x3b, 0xf8, 0x78, 0x9c, 0x1c, 0xd8, 0x58,810xa4, 0x24, 0xde, 0x5e, 0x84, 0x04, 0xc2, 0x42,820xb3, 0x33, 0xec, 0x6c, 0x94, 0x14, 0xd0, 0x50,830xb0, 0x30, 0xe8, 0x68, 0x90, 0x10, 0xce, 0x4e,840xbf, 0x3f, 0xfe, 0x7e, 0xa0, 0x20, 0xdc, 0x5c,850xa8, 0x28, 0xe0, 0x60, 0x88, 0x08, 0xc6, 0x46,860xb7, 0x37, 0xf0, 0x70, 0x98, 0x18, 0xd4, 0x54,870xaa, 0x2a, 0xe2, 0x62, 0x8a, 0x0a, 0xc8, 0x48,880xb9, 0x39, 0xf4, 0x74, 0x9a, 0x1a, 0xd6, 0x56,890xa2, 0x22, 0xdd, 0x5d, 0x82, 0x02, 0xc0, 0x40,900xb1, 0x31, 0xea, 0x6a, 0x92, 0x12, 0xcf, 0x4f,910xae, 0x2e, 0xe6, 0x66, 0x8e, 0x0e, 0xcc, 0x4c,920xbd, 0x3d, 0xfc, 0x7c, 0x9e, 0x1e, 0xda, 0x5a,930xa6, 0x26, 0xdf, 0x5f, 0x86, 0x06, 0xc4, 0x44,940xb5, 0x35, 0xee, 0x6e, 0x96, 0x16, 0xd2, 0x5295};9697/* ulaw -> alaw */98static u8 ulaw_to_alaw[256] =99{1000xab, 0x55, 0xd5, 0x15, 0x95, 0x75, 0xf5, 0x35,1010xb5, 0x45, 0xc5, 0x05, 0x85, 0x65, 0xe5, 0x25,1020xa5, 0x5d, 0xdd, 0x1d, 0x9d, 0x7d, 0xfd, 0x3d,1030xbd, 0x4d, 0xcd, 0x0d, 0x8d, 0x6d, 0xed, 0x2d,1040xad, 0x51, 0xd1, 0x11, 0x91, 0x71, 0xf1, 0x31,1050xb1, 0x41, 0xc1, 0x01, 0x81, 0x61, 0xe1, 0x21,1060x59, 0xd9, 0x19, 0x99, 0x79, 0xf9, 0x39, 0xb9,1070x49, 0xc9, 0x09, 0x89, 0x69, 0xe9, 0x29, 0xa9,1080xd7, 0x17, 0x97, 0x77, 0xf7, 0x37, 0xb7, 0x47,1090xc7, 0x07, 0x87, 0x67, 0xe7, 0x27, 0xa7, 0xdf,1100x9f, 0x7f, 0xff, 0x3f, 0xbf, 0x4f, 0xcf, 0x0f,1110x8f, 0x6f, 0xef, 0x2f, 0x53, 0x13, 0x73, 0x33,1120xb3, 0x43, 0xc3, 0x03, 0x83, 0x63, 0xe3, 0x23,1130xa3, 0x5b, 0xdb, 0x1b, 0x9b, 0x7b, 0xfb, 0x3b,1140xbb, 0xbb, 0x4b, 0x4b, 0xcb, 0xcb, 0x0b, 0x0b,1150x8b, 0x8b, 0x6b, 0x6b, 0xeb, 0xeb, 0x2b, 0x2b,1160xab, 0x54, 0xd4, 0x14, 0x94, 0x74, 0xf4, 0x34,1170xb4, 0x44, 0xc4, 0x04, 0x84, 0x64, 0xe4, 0x24,1180xa4, 0x5c, 0xdc, 0x1c, 0x9c, 0x7c, 0xfc, 0x3c,1190xbc, 0x4c, 0xcc, 0x0c, 0x8c, 0x6c, 0xec, 0x2c,1200xac, 0x50, 0xd0, 0x10, 0x90, 0x70, 0xf0, 0x30,1210xb0, 0x40, 0xc0, 0x00, 0x80, 0x60, 0xe0, 0x20,1220x58, 0xd8, 0x18, 0x98, 0x78, 0xf8, 0x38, 0xb8,1230x48, 0xc8, 0x08, 0x88, 0x68, 0xe8, 0x28, 0xa8,1240xd6, 0x16, 0x96, 0x76, 0xf6, 0x36, 0xb6, 0x46,1250xc6, 0x06, 0x86, 0x66, 0xe6, 0x26, 0xa6, 0xde,1260x9e, 0x7e, 0xfe, 0x3e, 0xbe, 0x4e, 0xce, 0x0e,1270x8e, 0x6e, 0xee, 0x2e, 0x52, 0x12, 0x72, 0x32,1280xb2, 0x42, 0xc2, 0x02, 0x82, 0x62, 0xe2, 0x22,1290xa2, 0x5a, 0xda, 0x1a, 0x9a, 0x7a, 0xfa, 0x3a,1300xba, 0xba, 0x4a, 0x4a, 0xca, 0xca, 0x0a, 0x0a,1310x8a, 0x8a, 0x6a, 0x6a, 0xea, 0xea, 0x2a, 0x2a132};133134/* alaw -> 4bit compression */135static u8 alaw_to_4bit[256] = {1360x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1370x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,1380x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1390x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,1400x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1410x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,1420x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1430x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,1440x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1450x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,1460x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0d, 0x02,1470x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,1480x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1490x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,1500x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1510x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,1520x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1530x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,1540x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1550x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,1560x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,1570x0d, 0x02, 0x08, 0x07, 0x0f, 0x01, 0x0a, 0x05,1580x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1590x0d, 0x02, 0x09, 0x07, 0x0f, 0x00, 0x0b, 0x04,1600x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1610x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,1620x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1630x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,1640x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1650x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,1660x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,1670x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,168};169170/* 4bit -> alaw decompression */171static u8 _4bit_to_alaw[16] = {1720x5d, 0x51, 0xd9, 0xd7, 0x5f, 0x53, 0xa3, 0x4b,1730x2a, 0x3a, 0x22, 0x2e, 0x26, 0x56, 0x20, 0x2c,174};175176/* ulaw -> 4bit compression */177static u8 ulaw_to_4bit[256] = {1780x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,1790x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,1800x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,1810x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,1820x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,1830x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,1840x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,1850x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,1860x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,1870x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04,1880x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,1890x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,1900x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,1910x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,1920x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,1930x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08,1940x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,1950x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,1960x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,1970x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,1980x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,1990x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,2000x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,2010x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,2020x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,2030x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b,2040x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,2050x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a,2060x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,2070x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,2080x09, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,2090x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,210};211212/* 4bit -> ulaw decompression */213static u8 _4bit_to_ulaw[16] = {2140x11, 0x21, 0x31, 0x40, 0x4e, 0x5c, 0x68, 0x71,2150xfe, 0xef, 0xe7, 0xdb, 0xcd, 0xbf, 0xaf, 0x9f,216};217218219/*220* Compresses data to the result buffer221* The result size must be at least half of the input buffer.222* The number of samples also must be even!223*/224int225l1oip_law_to_4bit(u8 *data, int len, u8 *result, u32 *state)226{227int ii, i = 0, o = 0;228229if (!len)230return 0;231232/* send saved byte and first input byte */233if (*state) {234*result++ = table_com[(((*state)<<8)&0xff00) | (*data++)];235len--;236o++;237}238239ii = len >> 1;240241while (i < ii) {242*result++ = table_com[(data[0]<<8) | (data[1])];243data += 2;244i++;245o++;246}247248/* if len has an odd number, we save byte for next call */249if (len & 1)250*state = 0x100 + *data;251else252*state = 0;253254return o;255}256257/* Decompress data to the result buffer258* The result size must be the number of sample in packet. (2 * input data)259* The number of samples in the result are even!260*/261int262l1oip_4bit_to_law(u8 *data, int len, u8 *result)263{264int i = 0;265u16 r;266267while (i < len) {268r = table_dec[*data++];269*result++ = r>>8;270*result++ = r;271i++;272}273274return len << 1;275}276277278/*279* law conversion280*/281int282l1oip_alaw_to_ulaw(u8 *data, int len, u8 *result)283{284int i = 0;285286while (i < len) {287*result++ = alaw_to_ulaw[*data++];288i++;289}290291return len;292}293294int295l1oip_ulaw_to_alaw(u8 *data, int len, u8 *result)296{297int i = 0;298299while (i < len) {300*result++ = ulaw_to_alaw[*data++];301i++;302}303304return len;305}306307308/*309* generate/free compression and decompression table310*/311void312l1oip_4bit_free(void)313{314if (table_dec)315vfree(table_dec);316if (table_com)317vfree(table_com);318table_com = NULL;319table_dec = NULL;320}321322int323l1oip_4bit_alloc(int ulaw)324{325int i1, i2, c, sample;326327/* in case, it is called again */328if (table_dec)329return 0;330331/* alloc conversion tables */332table_com = vmalloc(65536);333table_dec = vmalloc(512);334if (!table_com || !table_dec) {335l1oip_4bit_free();336return -ENOMEM;337}338memset(table_com, 0, 65536);339memset(table_dec, 0, 512);340/* generate compression table */341i1 = 0;342while (i1 < 256) {343if (ulaw)344c = ulaw_to_4bit[i1];345else346c = alaw_to_4bit[i1];347i2 = 0;348while (i2 < 256) {349table_com[(i1<<8) | i2] |= (c<<4);350table_com[(i2<<8) | i1] |= c;351i2++;352}353i1++;354}355356/* generate decompression table */357i1 = 0;358while (i1 < 16) {359if (ulaw)360sample = _4bit_to_ulaw[i1];361else362sample = _4bit_to_alaw[i1];363i2 = 0;364while (i2 < 16) {365table_dec[(i1<<4) | i2] |= (sample<<8);366table_dec[(i2<<4) | i1] |= sample;367i2++;368}369i1++;370}371372return 0;373}374375376377378