Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003 
0004  * l1oip_codec.c  generic codec using lookup table
0005  *  -> conversion from a-Law to u-Law
0006  *  -> conversion from u-Law to a-Law
0007  *  -> compression by reducing the number of sample resolution to 4
0008  *
0009  * NOTE: It is not compatible with any standard codec like ADPCM.
0010  *
0011  * Author   Andreas Eversberg (jolly@eversberg.eu)
0012  *
0013 
0014  */
0015 
0016 /*
0017 
0018   How the codec works:
0019   --------------------
0020 
0021   The volume is increased to increase the dynamic range of the audio signal.
0022   Each sample is converted to a-LAW with only 16 steps of level resolution.
0023   A pair of two samples are stored in one byte.
0024 
0025   The first byte is stored in the upper bits, the second byte is stored in the
0026   lower bits.
0027 
0028   To speed up compression and decompression, two lookup tables are formed:
0029 
0030   - 16 bits index for two samples (law encoded) with 8 bit compressed result.
0031   - 8 bits index for one compressed data with 16 bits decompressed result.
0032 
0033   NOTE: The bytes are handled as they are law-encoded.
0034 
0035 */
0036 
0037 #include <linux/vmalloc.h>
0038 #include <linux/mISDNif.h>
0039 #include <linux/in.h>
0040 #include "core.h"
0041 #include "l1oip.h"
0042 
0043 /* definitions of codec. don't use calculations, code may run slower. */
0044 
0045 static u8 *table_com;
0046 static u16 *table_dec;
0047 
0048 
0049 /* alaw -> ulaw */
0050 static u8 alaw_to_ulaw[256] =
0051 {
0052     0xab, 0x2b, 0xe3, 0x63, 0x8b, 0x0b, 0xc9, 0x49,
0053     0xba, 0x3a, 0xf6, 0x76, 0x9b, 0x1b, 0xd7, 0x57,
0054     0xa3, 0x23, 0xdd, 0x5d, 0x83, 0x03, 0xc1, 0x41,
0055     0xb2, 0x32, 0xeb, 0x6b, 0x93, 0x13, 0xcf, 0x4f,
0056     0xaf, 0x2f, 0xe7, 0x67, 0x8f, 0x0f, 0xcd, 0x4d,
0057     0xbe, 0x3e, 0xfe, 0x7e, 0x9f, 0x1f, 0xdb, 0x5b,
0058     0xa7, 0x27, 0xdf, 0x5f, 0x87, 0x07, 0xc5, 0x45,
0059     0xb6, 0x36, 0xef, 0x6f, 0x97, 0x17, 0xd3, 0x53,
0060     0xa9, 0x29, 0xe1, 0x61, 0x89, 0x09, 0xc7, 0x47,
0061     0xb8, 0x38, 0xf2, 0x72, 0x99, 0x19, 0xd5, 0x55,
0062     0xa1, 0x21, 0xdc, 0x5c, 0x81, 0x01, 0xbf, 0x3f,
0063     0xb0, 0x30, 0xe9, 0x69, 0x91, 0x11, 0xce, 0x4e,
0064     0xad, 0x2d, 0xe5, 0x65, 0x8d, 0x0d, 0xcb, 0x4b,
0065     0xbc, 0x3c, 0xfa, 0x7a, 0x9d, 0x1d, 0xd9, 0x59,
0066     0xa5, 0x25, 0xde, 0x5e, 0x85, 0x05, 0xc3, 0x43,
0067     0xb4, 0x34, 0xed, 0x6d, 0x95, 0x15, 0xd1, 0x51,
0068     0xac, 0x2c, 0xe4, 0x64, 0x8c, 0x0c, 0xca, 0x4a,
0069     0xbb, 0x3b, 0xf8, 0x78, 0x9c, 0x1c, 0xd8, 0x58,
0070     0xa4, 0x24, 0xde, 0x5e, 0x84, 0x04, 0xc2, 0x42,
0071     0xb3, 0x33, 0xec, 0x6c, 0x94, 0x14, 0xd0, 0x50,
0072     0xb0, 0x30, 0xe8, 0x68, 0x90, 0x10, 0xce, 0x4e,
0073     0xbf, 0x3f, 0xfe, 0x7e, 0xa0, 0x20, 0xdc, 0x5c,
0074     0xa8, 0x28, 0xe0, 0x60, 0x88, 0x08, 0xc6, 0x46,
0075     0xb7, 0x37, 0xf0, 0x70, 0x98, 0x18, 0xd4, 0x54,
0076     0xaa, 0x2a, 0xe2, 0x62, 0x8a, 0x0a, 0xc8, 0x48,
0077     0xb9, 0x39, 0xf4, 0x74, 0x9a, 0x1a, 0xd6, 0x56,
0078     0xa2, 0x22, 0xdd, 0x5d, 0x82, 0x02, 0xc0, 0x40,
0079     0xb1, 0x31, 0xea, 0x6a, 0x92, 0x12, 0xcf, 0x4f,
0080     0xae, 0x2e, 0xe6, 0x66, 0x8e, 0x0e, 0xcc, 0x4c,
0081     0xbd, 0x3d, 0xfc, 0x7c, 0x9e, 0x1e, 0xda, 0x5a,
0082     0xa6, 0x26, 0xdf, 0x5f, 0x86, 0x06, 0xc4, 0x44,
0083     0xb5, 0x35, 0xee, 0x6e, 0x96, 0x16, 0xd2, 0x52
0084 };
0085 
0086 /* ulaw -> alaw */
0087 static u8 ulaw_to_alaw[256] =
0088 {
0089     0xab, 0x55, 0xd5, 0x15, 0x95, 0x75, 0xf5, 0x35,
0090     0xb5, 0x45, 0xc5, 0x05, 0x85, 0x65, 0xe5, 0x25,
0091     0xa5, 0x5d, 0xdd, 0x1d, 0x9d, 0x7d, 0xfd, 0x3d,
0092     0xbd, 0x4d, 0xcd, 0x0d, 0x8d, 0x6d, 0xed, 0x2d,
0093     0xad, 0x51, 0xd1, 0x11, 0x91, 0x71, 0xf1, 0x31,
0094     0xb1, 0x41, 0xc1, 0x01, 0x81, 0x61, 0xe1, 0x21,
0095     0x59, 0xd9, 0x19, 0x99, 0x79, 0xf9, 0x39, 0xb9,
0096     0x49, 0xc9, 0x09, 0x89, 0x69, 0xe9, 0x29, 0xa9,
0097     0xd7, 0x17, 0x97, 0x77, 0xf7, 0x37, 0xb7, 0x47,
0098     0xc7, 0x07, 0x87, 0x67, 0xe7, 0x27, 0xa7, 0xdf,
0099     0x9f, 0x7f, 0xff, 0x3f, 0xbf, 0x4f, 0xcf, 0x0f,
0100     0x8f, 0x6f, 0xef, 0x2f, 0x53, 0x13, 0x73, 0x33,
0101     0xb3, 0x43, 0xc3, 0x03, 0x83, 0x63, 0xe3, 0x23,
0102     0xa3, 0x5b, 0xdb, 0x1b, 0x9b, 0x7b, 0xfb, 0x3b,
0103     0xbb, 0xbb, 0x4b, 0x4b, 0xcb, 0xcb, 0x0b, 0x0b,
0104     0x8b, 0x8b, 0x6b, 0x6b, 0xeb, 0xeb, 0x2b, 0x2b,
0105     0xab, 0x54, 0xd4, 0x14, 0x94, 0x74, 0xf4, 0x34,
0106     0xb4, 0x44, 0xc4, 0x04, 0x84, 0x64, 0xe4, 0x24,
0107     0xa4, 0x5c, 0xdc, 0x1c, 0x9c, 0x7c, 0xfc, 0x3c,
0108     0xbc, 0x4c, 0xcc, 0x0c, 0x8c, 0x6c, 0xec, 0x2c,
0109     0xac, 0x50, 0xd0, 0x10, 0x90, 0x70, 0xf0, 0x30,
0110     0xb0, 0x40, 0xc0, 0x00, 0x80, 0x60, 0xe0, 0x20,
0111     0x58, 0xd8, 0x18, 0x98, 0x78, 0xf8, 0x38, 0xb8,
0112     0x48, 0xc8, 0x08, 0x88, 0x68, 0xe8, 0x28, 0xa8,
0113     0xd6, 0x16, 0x96, 0x76, 0xf6, 0x36, 0xb6, 0x46,
0114     0xc6, 0x06, 0x86, 0x66, 0xe6, 0x26, 0xa6, 0xde,
0115     0x9e, 0x7e, 0xfe, 0x3e, 0xbe, 0x4e, 0xce, 0x0e,
0116     0x8e, 0x6e, 0xee, 0x2e, 0x52, 0x12, 0x72, 0x32,
0117     0xb2, 0x42, 0xc2, 0x02, 0x82, 0x62, 0xe2, 0x22,
0118     0xa2, 0x5a, 0xda, 0x1a, 0x9a, 0x7a, 0xfa, 0x3a,
0119     0xba, 0xba, 0x4a, 0x4a, 0xca, 0xca, 0x0a, 0x0a,
0120     0x8a, 0x8a, 0x6a, 0x6a, 0xea, 0xea, 0x2a, 0x2a
0121 };
0122 
0123 /* alaw -> 4bit compression */
0124 static u8 alaw_to_4bit[256] = {
0125     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0126     0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
0127     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0128     0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
0129     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0130     0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
0131     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0132     0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
0133     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0134     0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
0135     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0d, 0x02,
0136     0x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
0137     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0138     0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
0139     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0140     0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
0141     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0142     0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
0143     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0144     0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
0145     0x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
0146     0x0d, 0x02, 0x08, 0x07, 0x0f, 0x01, 0x0a, 0x05,
0147     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0148     0x0d, 0x02, 0x09, 0x07, 0x0f, 0x00, 0x0b, 0x04,
0149     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0150     0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
0151     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0152     0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
0153     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0154     0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
0155     0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
0156     0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
0157 };
0158 
0159 /* 4bit -> alaw decompression */
0160 static u8 _4bit_to_alaw[16] = {
0161     0x5d, 0x51, 0xd9, 0xd7, 0x5f, 0x53, 0xa3, 0x4b,
0162     0x2a, 0x3a, 0x22, 0x2e, 0x26, 0x56, 0x20, 0x2c,
0163 };
0164 
0165 /* ulaw -> 4bit compression */
0166 static u8 ulaw_to_4bit[256] = {
0167     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0168     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0169     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0170     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0171     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0172     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0173     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0174     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0175     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0176     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04,
0177     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0178     0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
0179     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
0180     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0181     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
0182     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08,
0183     0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
0184     0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
0185     0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
0186     0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
0187     0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
0188     0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
0189     0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
0190     0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
0191     0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
0192     0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b,
0193     0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0194     0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a,
0195     0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
0196     0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
0197     0x09, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0198     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
0199 };
0200 
0201 /* 4bit -> ulaw decompression */
0202 static u8 _4bit_to_ulaw[16] = {
0203     0x11, 0x21, 0x31, 0x40, 0x4e, 0x5c, 0x68, 0x71,
0204     0xfe, 0xef, 0xe7, 0xdb, 0xcd, 0xbf, 0xaf, 0x9f,
0205 };
0206 
0207 
0208 /*
0209  * Compresses data to the result buffer
0210  * The result size must be at least half of the input buffer.
0211  * The number of samples also must be even!
0212  */
0213 int
0214 l1oip_law_to_4bit(u8 *data, int len, u8 *result, u32 *state)
0215 {
0216     int ii, i = 0, o = 0;
0217 
0218     if (!len)
0219         return 0;
0220 
0221     /* send saved byte and first input byte */
0222     if (*state) {
0223         *result++ = table_com[(((*state) << 8) & 0xff00) | (*data++)];
0224         len--;
0225         o++;
0226     }
0227 
0228     ii = len >> 1;
0229 
0230     while (i < ii) {
0231         *result++ = table_com[(data[0]<<8) | (data[1])];
0232         data += 2;
0233         i++;
0234         o++;
0235     }
0236 
0237     /* if len has an odd number, we save byte for next call */
0238     if (len & 1)
0239         *state = 0x100 + *data;
0240     else
0241         *state = 0;
0242 
0243     return o;
0244 }
0245 
0246 /* Decompress data to the result buffer
0247  * The result size must be the number of sample in packet. (2 * input data)
0248  * The number of samples in the result are even!
0249  */
0250 int
0251 l1oip_4bit_to_law(u8 *data, int len, u8 *result)
0252 {
0253     int i = 0;
0254     u16 r;
0255 
0256     while (i < len) {
0257         r = table_dec[*data++];
0258         *result++ = r >> 8;
0259         *result++ = r;
0260         i++;
0261     }
0262 
0263     return len << 1;
0264 }
0265 
0266 
0267 /*
0268  * law conversion
0269  */
0270 int
0271 l1oip_alaw_to_ulaw(u8 *data, int len, u8 *result)
0272 {
0273     int i = 0;
0274 
0275     while (i < len) {
0276         *result++ = alaw_to_ulaw[*data++];
0277         i++;
0278     }
0279 
0280     return len;
0281 }
0282 
0283 int
0284 l1oip_ulaw_to_alaw(u8 *data, int len, u8 *result)
0285 {
0286     int i = 0;
0287 
0288     while (i < len) {
0289         *result++ = ulaw_to_alaw[*data++];
0290         i++;
0291     }
0292 
0293     return len;
0294 }
0295 
0296 
0297 /*
0298  * generate/free compression and decompression table
0299  */
0300 void
0301 l1oip_4bit_free(void)
0302 {
0303     vfree(table_dec);
0304     vfree(table_com);
0305     table_com = NULL;
0306     table_dec = NULL;
0307 }
0308 
0309 int
0310 l1oip_4bit_alloc(int ulaw)
0311 {
0312     int i1, i2, c, sample;
0313 
0314     /* in case, it is called again */
0315     if (table_dec)
0316         return 0;
0317 
0318     /* alloc conversion tables */
0319     table_com = vzalloc(65536);
0320     table_dec = vzalloc(512);
0321     if (!table_com || !table_dec) {
0322         l1oip_4bit_free();
0323         return -ENOMEM;
0324     }
0325     /* generate compression table */
0326     i1 = 0;
0327     while (i1 < 256) {
0328         if (ulaw)
0329             c = ulaw_to_4bit[i1];
0330         else
0331             c = alaw_to_4bit[i1];
0332         i2 = 0;
0333         while (i2 < 256) {
0334             table_com[(i1 << 8) | i2] |= (c << 4);
0335             table_com[(i2 << 8) | i1] |= c;
0336             i2++;
0337         }
0338         i1++;
0339     }
0340 
0341     /* generate decompression table */
0342     i1 = 0;
0343     while (i1 < 16) {
0344         if (ulaw)
0345             sample = _4bit_to_ulaw[i1];
0346         else
0347             sample = _4bit_to_alaw[i1];
0348         i2 = 0;
0349         while (i2 < 16) {
0350             table_dec[(i1 << 4) | i2] |= (sample << 8);
0351             table_dec[(i2 << 4) | i1] |= sample;
0352             i2++;
0353         }
0354         i1++;
0355     }
0356 
0357     return 0;
0358 }