1*3712b42dSKarsten Keil /* 2*3712b42dSKarsten Keil 3*3712b42dSKarsten Keil * l1oip_codec.c generic codec using lookup table 4*3712b42dSKarsten Keil * -> conversion from a-Law to u-Law 5*3712b42dSKarsten Keil * -> conversion from u-Law to a-Law 6*3712b42dSKarsten Keil * -> compression by reducing the number of sample resolution to 4 7*3712b42dSKarsten Keil * 8*3712b42dSKarsten Keil * NOTE: It is not compatible with any standard codec like ADPCM. 9*3712b42dSKarsten Keil * 10*3712b42dSKarsten Keil * Author Andreas Eversberg (jolly@eversberg.eu) 11*3712b42dSKarsten Keil * 12*3712b42dSKarsten Keil * This program is free software; you can redistribute it and/or modify 13*3712b42dSKarsten Keil * it under the terms of the GNU General Public License as published by 14*3712b42dSKarsten Keil * the Free Software Foundation; either version 2, or (at your option) 15*3712b42dSKarsten Keil * any later version. 16*3712b42dSKarsten Keil * 17*3712b42dSKarsten Keil * This program is distributed in the hope that it will be useful, 18*3712b42dSKarsten Keil * but WITHOUT ANY WARRANTY; without even the implied warranty of 19*3712b42dSKarsten Keil * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20*3712b42dSKarsten Keil * GNU General Public License for more details. 21*3712b42dSKarsten Keil * 22*3712b42dSKarsten Keil * You should have received a copy of the GNU General Public License 23*3712b42dSKarsten Keil * along with this program; if not, write to the Free Software 24*3712b42dSKarsten Keil * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 25*3712b42dSKarsten Keil 26*3712b42dSKarsten Keil */ 27*3712b42dSKarsten Keil 28*3712b42dSKarsten Keil /* 29*3712b42dSKarsten Keil 30*3712b42dSKarsten Keil How the codec works: 31*3712b42dSKarsten Keil -------------------- 32*3712b42dSKarsten Keil 33*3712b42dSKarsten Keil The volume is increased to increase the dynamic range of the audio signal. 34*3712b42dSKarsten Keil Each sample is converted to a-LAW with only 16 steps of level resolution. 35*3712b42dSKarsten Keil A pair of two samples are stored in one byte. 36*3712b42dSKarsten Keil 37*3712b42dSKarsten Keil The first byte is stored in the upper bits, the second byte is stored in the 38*3712b42dSKarsten Keil lower bits. 39*3712b42dSKarsten Keil 40*3712b42dSKarsten Keil To speed up compression and decompression, two lookup tables are formed: 41*3712b42dSKarsten Keil 42*3712b42dSKarsten Keil - 16 bits index for two samples (law encoded) with 8 bit compressed result. 43*3712b42dSKarsten Keil - 8 bits index for one compressed data with 16 bits decompressed result. 44*3712b42dSKarsten Keil 45*3712b42dSKarsten Keil NOTE: The bytes are handled as they are law-encoded. 46*3712b42dSKarsten Keil 47*3712b42dSKarsten Keil */ 48*3712b42dSKarsten Keil 49*3712b42dSKarsten Keil #include <linux/vmalloc.h> 50*3712b42dSKarsten Keil #include <linux/mISDNif.h> 51*3712b42dSKarsten Keil #include "core.h" 52*3712b42dSKarsten Keil 53*3712b42dSKarsten Keil /* definitions of codec. don't use calculations, code may run slower. */ 54*3712b42dSKarsten Keil 55*3712b42dSKarsten Keil static u8 *table_com; 56*3712b42dSKarsten Keil static u16 *table_dec; 57*3712b42dSKarsten Keil 58*3712b42dSKarsten Keil 59*3712b42dSKarsten Keil /* alaw -> ulaw */ 60*3712b42dSKarsten Keil static u8 alaw_to_ulaw[256] = 61*3712b42dSKarsten Keil { 62*3712b42dSKarsten Keil 0xab, 0x2b, 0xe3, 0x63, 0x8b, 0x0b, 0xc9, 0x49, 63*3712b42dSKarsten Keil 0xba, 0x3a, 0xf6, 0x76, 0x9b, 0x1b, 0xd7, 0x57, 64*3712b42dSKarsten Keil 0xa3, 0x23, 0xdd, 0x5d, 0x83, 0x03, 0xc1, 0x41, 65*3712b42dSKarsten Keil 0xb2, 0x32, 0xeb, 0x6b, 0x93, 0x13, 0xcf, 0x4f, 66*3712b42dSKarsten Keil 0xaf, 0x2f, 0xe7, 0x67, 0x8f, 0x0f, 0xcd, 0x4d, 67*3712b42dSKarsten Keil 0xbe, 0x3e, 0xfe, 0x7e, 0x9f, 0x1f, 0xdb, 0x5b, 68*3712b42dSKarsten Keil 0xa7, 0x27, 0xdf, 0x5f, 0x87, 0x07, 0xc5, 0x45, 69*3712b42dSKarsten Keil 0xb6, 0x36, 0xef, 0x6f, 0x97, 0x17, 0xd3, 0x53, 70*3712b42dSKarsten Keil 0xa9, 0x29, 0xe1, 0x61, 0x89, 0x09, 0xc7, 0x47, 71*3712b42dSKarsten Keil 0xb8, 0x38, 0xf2, 0x72, 0x99, 0x19, 0xd5, 0x55, 72*3712b42dSKarsten Keil 0xa1, 0x21, 0xdc, 0x5c, 0x81, 0x01, 0xbf, 0x3f, 73*3712b42dSKarsten Keil 0xb0, 0x30, 0xe9, 0x69, 0x91, 0x11, 0xce, 0x4e, 74*3712b42dSKarsten Keil 0xad, 0x2d, 0xe5, 0x65, 0x8d, 0x0d, 0xcb, 0x4b, 75*3712b42dSKarsten Keil 0xbc, 0x3c, 0xfa, 0x7a, 0x9d, 0x1d, 0xd9, 0x59, 76*3712b42dSKarsten Keil 0xa5, 0x25, 0xde, 0x5e, 0x85, 0x05, 0xc3, 0x43, 77*3712b42dSKarsten Keil 0xb4, 0x34, 0xed, 0x6d, 0x95, 0x15, 0xd1, 0x51, 78*3712b42dSKarsten Keil 0xac, 0x2c, 0xe4, 0x64, 0x8c, 0x0c, 0xca, 0x4a, 79*3712b42dSKarsten Keil 0xbb, 0x3b, 0xf8, 0x78, 0x9c, 0x1c, 0xd8, 0x58, 80*3712b42dSKarsten Keil 0xa4, 0x24, 0xde, 0x5e, 0x84, 0x04, 0xc2, 0x42, 81*3712b42dSKarsten Keil 0xb3, 0x33, 0xec, 0x6c, 0x94, 0x14, 0xd0, 0x50, 82*3712b42dSKarsten Keil 0xb0, 0x30, 0xe8, 0x68, 0x90, 0x10, 0xce, 0x4e, 83*3712b42dSKarsten Keil 0xbf, 0x3f, 0xfe, 0x7e, 0xa0, 0x20, 0xdc, 0x5c, 84*3712b42dSKarsten Keil 0xa8, 0x28, 0xe0, 0x60, 0x88, 0x08, 0xc6, 0x46, 85*3712b42dSKarsten Keil 0xb7, 0x37, 0xf0, 0x70, 0x98, 0x18, 0xd4, 0x54, 86*3712b42dSKarsten Keil 0xaa, 0x2a, 0xe2, 0x62, 0x8a, 0x0a, 0xc8, 0x48, 87*3712b42dSKarsten Keil 0xb9, 0x39, 0xf4, 0x74, 0x9a, 0x1a, 0xd6, 0x56, 88*3712b42dSKarsten Keil 0xa2, 0x22, 0xdd, 0x5d, 0x82, 0x02, 0xc0, 0x40, 89*3712b42dSKarsten Keil 0xb1, 0x31, 0xea, 0x6a, 0x92, 0x12, 0xcf, 0x4f, 90*3712b42dSKarsten Keil 0xae, 0x2e, 0xe6, 0x66, 0x8e, 0x0e, 0xcc, 0x4c, 91*3712b42dSKarsten Keil 0xbd, 0x3d, 0xfc, 0x7c, 0x9e, 0x1e, 0xda, 0x5a, 92*3712b42dSKarsten Keil 0xa6, 0x26, 0xdf, 0x5f, 0x86, 0x06, 0xc4, 0x44, 93*3712b42dSKarsten Keil 0xb5, 0x35, 0xee, 0x6e, 0x96, 0x16, 0xd2, 0x52 94*3712b42dSKarsten Keil }; 95*3712b42dSKarsten Keil 96*3712b42dSKarsten Keil /* ulaw -> alaw */ 97*3712b42dSKarsten Keil static u8 ulaw_to_alaw[256] = 98*3712b42dSKarsten Keil { 99*3712b42dSKarsten Keil 0xab, 0x55, 0xd5, 0x15, 0x95, 0x75, 0xf5, 0x35, 100*3712b42dSKarsten Keil 0xb5, 0x45, 0xc5, 0x05, 0x85, 0x65, 0xe5, 0x25, 101*3712b42dSKarsten Keil 0xa5, 0x5d, 0xdd, 0x1d, 0x9d, 0x7d, 0xfd, 0x3d, 102*3712b42dSKarsten Keil 0xbd, 0x4d, 0xcd, 0x0d, 0x8d, 0x6d, 0xed, 0x2d, 103*3712b42dSKarsten Keil 0xad, 0x51, 0xd1, 0x11, 0x91, 0x71, 0xf1, 0x31, 104*3712b42dSKarsten Keil 0xb1, 0x41, 0xc1, 0x01, 0x81, 0x61, 0xe1, 0x21, 105*3712b42dSKarsten Keil 0x59, 0xd9, 0x19, 0x99, 0x79, 0xf9, 0x39, 0xb9, 106*3712b42dSKarsten Keil 0x49, 0xc9, 0x09, 0x89, 0x69, 0xe9, 0x29, 0xa9, 107*3712b42dSKarsten Keil 0xd7, 0x17, 0x97, 0x77, 0xf7, 0x37, 0xb7, 0x47, 108*3712b42dSKarsten Keil 0xc7, 0x07, 0x87, 0x67, 0xe7, 0x27, 0xa7, 0xdf, 109*3712b42dSKarsten Keil 0x9f, 0x7f, 0xff, 0x3f, 0xbf, 0x4f, 0xcf, 0x0f, 110*3712b42dSKarsten Keil 0x8f, 0x6f, 0xef, 0x2f, 0x53, 0x13, 0x73, 0x33, 111*3712b42dSKarsten Keil 0xb3, 0x43, 0xc3, 0x03, 0x83, 0x63, 0xe3, 0x23, 112*3712b42dSKarsten Keil 0xa3, 0x5b, 0xdb, 0x1b, 0x9b, 0x7b, 0xfb, 0x3b, 113*3712b42dSKarsten Keil 0xbb, 0xbb, 0x4b, 0x4b, 0xcb, 0xcb, 0x0b, 0x0b, 114*3712b42dSKarsten Keil 0x8b, 0x8b, 0x6b, 0x6b, 0xeb, 0xeb, 0x2b, 0x2b, 115*3712b42dSKarsten Keil 0xab, 0x54, 0xd4, 0x14, 0x94, 0x74, 0xf4, 0x34, 116*3712b42dSKarsten Keil 0xb4, 0x44, 0xc4, 0x04, 0x84, 0x64, 0xe4, 0x24, 117*3712b42dSKarsten Keil 0xa4, 0x5c, 0xdc, 0x1c, 0x9c, 0x7c, 0xfc, 0x3c, 118*3712b42dSKarsten Keil 0xbc, 0x4c, 0xcc, 0x0c, 0x8c, 0x6c, 0xec, 0x2c, 119*3712b42dSKarsten Keil 0xac, 0x50, 0xd0, 0x10, 0x90, 0x70, 0xf0, 0x30, 120*3712b42dSKarsten Keil 0xb0, 0x40, 0xc0, 0x00, 0x80, 0x60, 0xe0, 0x20, 121*3712b42dSKarsten Keil 0x58, 0xd8, 0x18, 0x98, 0x78, 0xf8, 0x38, 0xb8, 122*3712b42dSKarsten Keil 0x48, 0xc8, 0x08, 0x88, 0x68, 0xe8, 0x28, 0xa8, 123*3712b42dSKarsten Keil 0xd6, 0x16, 0x96, 0x76, 0xf6, 0x36, 0xb6, 0x46, 124*3712b42dSKarsten Keil 0xc6, 0x06, 0x86, 0x66, 0xe6, 0x26, 0xa6, 0xde, 125*3712b42dSKarsten Keil 0x9e, 0x7e, 0xfe, 0x3e, 0xbe, 0x4e, 0xce, 0x0e, 126*3712b42dSKarsten Keil 0x8e, 0x6e, 0xee, 0x2e, 0x52, 0x12, 0x72, 0x32, 127*3712b42dSKarsten Keil 0xb2, 0x42, 0xc2, 0x02, 0x82, 0x62, 0xe2, 0x22, 128*3712b42dSKarsten Keil 0xa2, 0x5a, 0xda, 0x1a, 0x9a, 0x7a, 0xfa, 0x3a, 129*3712b42dSKarsten Keil 0xba, 0xba, 0x4a, 0x4a, 0xca, 0xca, 0x0a, 0x0a, 130*3712b42dSKarsten Keil 0x8a, 0x8a, 0x6a, 0x6a, 0xea, 0xea, 0x2a, 0x2a 131*3712b42dSKarsten Keil }; 132*3712b42dSKarsten Keil 133*3712b42dSKarsten Keil /* alaw -> 4bit compression */ 134*3712b42dSKarsten Keil static u8 alaw_to_4bit[256] = { 135*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 136*3712b42dSKarsten Keil 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04, 137*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 138*3712b42dSKarsten Keil 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04, 139*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 140*3712b42dSKarsten Keil 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04, 141*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 142*3712b42dSKarsten Keil 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04, 143*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 144*3712b42dSKarsten Keil 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04, 145*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0d, 0x02, 146*3712b42dSKarsten Keil 0x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04, 147*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 148*3712b42dSKarsten Keil 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04, 149*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 150*3712b42dSKarsten Keil 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04, 151*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 152*3712b42dSKarsten Keil 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04, 153*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 154*3712b42dSKarsten Keil 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04, 155*3712b42dSKarsten Keil 0x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04, 156*3712b42dSKarsten Keil 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x01, 0x0a, 0x05, 157*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 158*3712b42dSKarsten Keil 0x0d, 0x02, 0x09, 0x07, 0x0f, 0x00, 0x0b, 0x04, 159*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 160*3712b42dSKarsten Keil 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04, 161*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 162*3712b42dSKarsten Keil 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04, 163*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 164*3712b42dSKarsten Keil 0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04, 165*3712b42dSKarsten Keil 0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03, 166*3712b42dSKarsten Keil 0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04, 167*3712b42dSKarsten Keil }; 168*3712b42dSKarsten Keil 169*3712b42dSKarsten Keil /* 4bit -> alaw decompression */ 170*3712b42dSKarsten Keil static u8 _4bit_to_alaw[16] = { 171*3712b42dSKarsten Keil 0x5d, 0x51, 0xd9, 0xd7, 0x5f, 0x53, 0xa3, 0x4b, 172*3712b42dSKarsten Keil 0x2a, 0x3a, 0x22, 0x2e, 0x26, 0x56, 0x20, 0x2c, 173*3712b42dSKarsten Keil }; 174*3712b42dSKarsten Keil 175*3712b42dSKarsten Keil /* ulaw -> 4bit compression */ 176*3712b42dSKarsten Keil static u8 ulaw_to_4bit[256] = { 177*3712b42dSKarsten Keil 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 178*3712b42dSKarsten Keil 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 179*3712b42dSKarsten Keil 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 180*3712b42dSKarsten Keil 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 181*3712b42dSKarsten Keil 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 182*3712b42dSKarsten Keil 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 183*3712b42dSKarsten Keil 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 184*3712b42dSKarsten Keil 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 185*3712b42dSKarsten Keil 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 186*3712b42dSKarsten Keil 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 187*3712b42dSKarsten Keil 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 188*3712b42dSKarsten Keil 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 189*3712b42dSKarsten Keil 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 190*3712b42dSKarsten Keil 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 191*3712b42dSKarsten Keil 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 192*3712b42dSKarsten Keil 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08, 193*3712b42dSKarsten Keil 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 194*3712b42dSKarsten Keil 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 195*3712b42dSKarsten Keil 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 196*3712b42dSKarsten Keil 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 197*3712b42dSKarsten Keil 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 198*3712b42dSKarsten Keil 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 199*3712b42dSKarsten Keil 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 200*3712b42dSKarsten Keil 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 201*3712b42dSKarsten Keil 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 202*3712b42dSKarsten Keil 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 203*3712b42dSKarsten Keil 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 204*3712b42dSKarsten Keil 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 205*3712b42dSKarsten Keil 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 206*3712b42dSKarsten Keil 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 207*3712b42dSKarsten Keil 0x09, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 208*3712b42dSKarsten Keil 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 209*3712b42dSKarsten Keil }; 210*3712b42dSKarsten Keil 211*3712b42dSKarsten Keil /* 4bit -> ulaw decompression */ 212*3712b42dSKarsten Keil static u8 _4bit_to_ulaw[16] = { 213*3712b42dSKarsten Keil 0x11, 0x21, 0x31, 0x40, 0x4e, 0x5c, 0x68, 0x71, 214*3712b42dSKarsten Keil 0xfe, 0xef, 0xe7, 0xdb, 0xcd, 0xbf, 0xaf, 0x9f, 215*3712b42dSKarsten Keil }; 216*3712b42dSKarsten Keil 217*3712b42dSKarsten Keil 218*3712b42dSKarsten Keil /* 219*3712b42dSKarsten Keil * Compresses data to the result buffer 220*3712b42dSKarsten Keil * The result size must be at least half of the input buffer. 221*3712b42dSKarsten Keil * The number of samples also must be even! 222*3712b42dSKarsten Keil */ 223*3712b42dSKarsten Keil int 224*3712b42dSKarsten Keil l1oip_law_to_4bit(u8 *data, int len, u8 *result, u32 *state) 225*3712b42dSKarsten Keil { 226*3712b42dSKarsten Keil int ii, i = 0, o = 0; 227*3712b42dSKarsten Keil 228*3712b42dSKarsten Keil if (!len) 229*3712b42dSKarsten Keil return 0; 230*3712b42dSKarsten Keil 231*3712b42dSKarsten Keil /* send saved byte and first input byte */ 232*3712b42dSKarsten Keil if (*state) { 233*3712b42dSKarsten Keil *result++ = table_com[(((*state)<<8)&0xff00) | (*data++)]; 234*3712b42dSKarsten Keil len--; 235*3712b42dSKarsten Keil o++; 236*3712b42dSKarsten Keil } 237*3712b42dSKarsten Keil 238*3712b42dSKarsten Keil ii = len >> 1; 239*3712b42dSKarsten Keil 240*3712b42dSKarsten Keil while (i < ii) { 241*3712b42dSKarsten Keil *result++ = table_com[(data[0]<<8) | (data[1])]; 242*3712b42dSKarsten Keil data += 2; 243*3712b42dSKarsten Keil i++; 244*3712b42dSKarsten Keil o++; 245*3712b42dSKarsten Keil } 246*3712b42dSKarsten Keil 247*3712b42dSKarsten Keil /* if len has an odd number, we save byte for next call */ 248*3712b42dSKarsten Keil if (len & 1) 249*3712b42dSKarsten Keil *state = 0x100 + *data; 250*3712b42dSKarsten Keil else 251*3712b42dSKarsten Keil *state = 0; 252*3712b42dSKarsten Keil 253*3712b42dSKarsten Keil return o; 254*3712b42dSKarsten Keil } 255*3712b42dSKarsten Keil 256*3712b42dSKarsten Keil /* Decompress data to the result buffer 257*3712b42dSKarsten Keil * The result size must be the number of sample in packet. (2 * input data) 258*3712b42dSKarsten Keil * The number of samples in the result are even! 259*3712b42dSKarsten Keil */ 260*3712b42dSKarsten Keil int 261*3712b42dSKarsten Keil l1oip_4bit_to_law(u8 *data, int len, u8 *result) 262*3712b42dSKarsten Keil { 263*3712b42dSKarsten Keil int i = 0; 264*3712b42dSKarsten Keil u16 r; 265*3712b42dSKarsten Keil 266*3712b42dSKarsten Keil while (i < len) { 267*3712b42dSKarsten Keil r = table_dec[*data++]; 268*3712b42dSKarsten Keil *result++ = r>>8; 269*3712b42dSKarsten Keil *result++ = r; 270*3712b42dSKarsten Keil i++; 271*3712b42dSKarsten Keil } 272*3712b42dSKarsten Keil 273*3712b42dSKarsten Keil return len << 1; 274*3712b42dSKarsten Keil } 275*3712b42dSKarsten Keil 276*3712b42dSKarsten Keil 277*3712b42dSKarsten Keil /* 278*3712b42dSKarsten Keil * law conversion 279*3712b42dSKarsten Keil */ 280*3712b42dSKarsten Keil int 281*3712b42dSKarsten Keil l1oip_alaw_to_ulaw(u8 *data, int len, u8 *result) 282*3712b42dSKarsten Keil { 283*3712b42dSKarsten Keil int i = 0; 284*3712b42dSKarsten Keil 285*3712b42dSKarsten Keil while (i < len) { 286*3712b42dSKarsten Keil *result++ = alaw_to_ulaw[*data++]; 287*3712b42dSKarsten Keil i++; 288*3712b42dSKarsten Keil } 289*3712b42dSKarsten Keil 290*3712b42dSKarsten Keil return len; 291*3712b42dSKarsten Keil } 292*3712b42dSKarsten Keil 293*3712b42dSKarsten Keil int 294*3712b42dSKarsten Keil l1oip_ulaw_to_alaw(u8 *data, int len, u8 *result) 295*3712b42dSKarsten Keil { 296*3712b42dSKarsten Keil int i = 0; 297*3712b42dSKarsten Keil 298*3712b42dSKarsten Keil while (i < len) { 299*3712b42dSKarsten Keil *result++ = ulaw_to_alaw[*data++]; 300*3712b42dSKarsten Keil i++; 301*3712b42dSKarsten Keil } 302*3712b42dSKarsten Keil 303*3712b42dSKarsten Keil return len; 304*3712b42dSKarsten Keil } 305*3712b42dSKarsten Keil 306*3712b42dSKarsten Keil 307*3712b42dSKarsten Keil /* 308*3712b42dSKarsten Keil * generate/free compression and decompression table 309*3712b42dSKarsten Keil */ 310*3712b42dSKarsten Keil void 311*3712b42dSKarsten Keil l1oip_4bit_free(void) 312*3712b42dSKarsten Keil { 313*3712b42dSKarsten Keil if (table_dec) 314*3712b42dSKarsten Keil vfree(table_dec); 315*3712b42dSKarsten Keil if (table_com) 316*3712b42dSKarsten Keil vfree(table_com); 317*3712b42dSKarsten Keil table_com = NULL; 318*3712b42dSKarsten Keil table_dec = NULL; 319*3712b42dSKarsten Keil } 320*3712b42dSKarsten Keil 321*3712b42dSKarsten Keil int 322*3712b42dSKarsten Keil l1oip_4bit_alloc(int ulaw) 323*3712b42dSKarsten Keil { 324*3712b42dSKarsten Keil int i1, i2, c, sample; 325*3712b42dSKarsten Keil 326*3712b42dSKarsten Keil /* in case, it is called again */ 327*3712b42dSKarsten Keil if (table_dec) 328*3712b42dSKarsten Keil return 0; 329*3712b42dSKarsten Keil 330*3712b42dSKarsten Keil /* alloc conversion tables */ 331*3712b42dSKarsten Keil table_com = vmalloc(65536); 332*3712b42dSKarsten Keil table_dec = vmalloc(512); 333*3712b42dSKarsten Keil if (!table_com | !table_dec) { 334*3712b42dSKarsten Keil l1oip_4bit_free(); 335*3712b42dSKarsten Keil return -ENOMEM; 336*3712b42dSKarsten Keil } 337*3712b42dSKarsten Keil memset(table_com, 0, 65536); 338*3712b42dSKarsten Keil memset(table_dec, 0, 512); 339*3712b42dSKarsten Keil /* generate compression table */ 340*3712b42dSKarsten Keil i1 = 0; 341*3712b42dSKarsten Keil while (i1 < 256) { 342*3712b42dSKarsten Keil if (ulaw) 343*3712b42dSKarsten Keil c = ulaw_to_4bit[i1]; 344*3712b42dSKarsten Keil else 345*3712b42dSKarsten Keil c = alaw_to_4bit[i1]; 346*3712b42dSKarsten Keil i2 = 0; 347*3712b42dSKarsten Keil while (i2 < 256) { 348*3712b42dSKarsten Keil table_com[(i1<<8) | i2] |= (c<<4); 349*3712b42dSKarsten Keil table_com[(i2<<8) | i1] |= c; 350*3712b42dSKarsten Keil i2++; 351*3712b42dSKarsten Keil } 352*3712b42dSKarsten Keil i1++; 353*3712b42dSKarsten Keil } 354*3712b42dSKarsten Keil 355*3712b42dSKarsten Keil /* generate decompression table */ 356*3712b42dSKarsten Keil i1 = 0; 357*3712b42dSKarsten Keil while (i1 < 16) { 358*3712b42dSKarsten Keil if (ulaw) 359*3712b42dSKarsten Keil sample = _4bit_to_ulaw[i1]; 360*3712b42dSKarsten Keil else 361*3712b42dSKarsten Keil sample = _4bit_to_alaw[i1]; 362*3712b42dSKarsten Keil i2 = 0; 363*3712b42dSKarsten Keil while (i2 < 16) { 364*3712b42dSKarsten Keil table_dec[(i1<<4) | i2] |= (sample<<8); 365*3712b42dSKarsten Keil table_dec[(i2<<4) | i1] |= sample; 366*3712b42dSKarsten Keil i2++; 367*3712b42dSKarsten Keil } 368*3712b42dSKarsten Keil i1++; 369*3712b42dSKarsten Keil } 370*3712b42dSKarsten Keil 371*3712b42dSKarsten Keil return 0; 372*3712b42dSKarsten Keil } 373*3712b42dSKarsten Keil 374*3712b42dSKarsten Keil 375