18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci * l1oip_codec.c  generic codec using lookup table
58c2ecf20Sopenharmony_ci *  -> conversion from a-Law to u-Law
68c2ecf20Sopenharmony_ci *  -> conversion from u-Law to a-Law
78c2ecf20Sopenharmony_ci *  -> compression by reducing the number of sample resolution to 4
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * NOTE: It is not compatible with any standard codec like ADPCM.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Author	Andreas Eversberg (jolly@eversberg.eu)
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci  How the codec works:
198c2ecf20Sopenharmony_ci  --------------------
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci  The volume is increased to increase the dynamic range of the audio signal.
228c2ecf20Sopenharmony_ci  Each sample is converted to a-LAW with only 16 steps of level resolution.
238c2ecf20Sopenharmony_ci  A pair of two samples are stored in one byte.
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci  The first byte is stored in the upper bits, the second byte is stored in the
268c2ecf20Sopenharmony_ci  lower bits.
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci  To speed up compression and decompression, two lookup tables are formed:
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci  - 16 bits index for two samples (law encoded) with 8 bit compressed result.
318c2ecf20Sopenharmony_ci  - 8 bits index for one compressed data with 16 bits decompressed result.
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci  NOTE: The bytes are handled as they are law-encoded.
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci*/
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
388c2ecf20Sopenharmony_ci#include <linux/mISDNif.h>
398c2ecf20Sopenharmony_ci#include <linux/in.h>
408c2ecf20Sopenharmony_ci#include "core.h"
418c2ecf20Sopenharmony_ci#include "l1oip.h"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* definitions of codec. don't use calculations, code may run slower. */
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic u8 *table_com;
468c2ecf20Sopenharmony_cistatic u16 *table_dec;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* alaw -> ulaw */
508c2ecf20Sopenharmony_cistatic u8 alaw_to_ulaw[256] =
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	0xab, 0x2b, 0xe3, 0x63, 0x8b, 0x0b, 0xc9, 0x49,
538c2ecf20Sopenharmony_ci	0xba, 0x3a, 0xf6, 0x76, 0x9b, 0x1b, 0xd7, 0x57,
548c2ecf20Sopenharmony_ci	0xa3, 0x23, 0xdd, 0x5d, 0x83, 0x03, 0xc1, 0x41,
558c2ecf20Sopenharmony_ci	0xb2, 0x32, 0xeb, 0x6b, 0x93, 0x13, 0xcf, 0x4f,
568c2ecf20Sopenharmony_ci	0xaf, 0x2f, 0xe7, 0x67, 0x8f, 0x0f, 0xcd, 0x4d,
578c2ecf20Sopenharmony_ci	0xbe, 0x3e, 0xfe, 0x7e, 0x9f, 0x1f, 0xdb, 0x5b,
588c2ecf20Sopenharmony_ci	0xa7, 0x27, 0xdf, 0x5f, 0x87, 0x07, 0xc5, 0x45,
598c2ecf20Sopenharmony_ci	0xb6, 0x36, 0xef, 0x6f, 0x97, 0x17, 0xd3, 0x53,
608c2ecf20Sopenharmony_ci	0xa9, 0x29, 0xe1, 0x61, 0x89, 0x09, 0xc7, 0x47,
618c2ecf20Sopenharmony_ci	0xb8, 0x38, 0xf2, 0x72, 0x99, 0x19, 0xd5, 0x55,
628c2ecf20Sopenharmony_ci	0xa1, 0x21, 0xdc, 0x5c, 0x81, 0x01, 0xbf, 0x3f,
638c2ecf20Sopenharmony_ci	0xb0, 0x30, 0xe9, 0x69, 0x91, 0x11, 0xce, 0x4e,
648c2ecf20Sopenharmony_ci	0xad, 0x2d, 0xe5, 0x65, 0x8d, 0x0d, 0xcb, 0x4b,
658c2ecf20Sopenharmony_ci	0xbc, 0x3c, 0xfa, 0x7a, 0x9d, 0x1d, 0xd9, 0x59,
668c2ecf20Sopenharmony_ci	0xa5, 0x25, 0xde, 0x5e, 0x85, 0x05, 0xc3, 0x43,
678c2ecf20Sopenharmony_ci	0xb4, 0x34, 0xed, 0x6d, 0x95, 0x15, 0xd1, 0x51,
688c2ecf20Sopenharmony_ci	0xac, 0x2c, 0xe4, 0x64, 0x8c, 0x0c, 0xca, 0x4a,
698c2ecf20Sopenharmony_ci	0xbb, 0x3b, 0xf8, 0x78, 0x9c, 0x1c, 0xd8, 0x58,
708c2ecf20Sopenharmony_ci	0xa4, 0x24, 0xde, 0x5e, 0x84, 0x04, 0xc2, 0x42,
718c2ecf20Sopenharmony_ci	0xb3, 0x33, 0xec, 0x6c, 0x94, 0x14, 0xd0, 0x50,
728c2ecf20Sopenharmony_ci	0xb0, 0x30, 0xe8, 0x68, 0x90, 0x10, 0xce, 0x4e,
738c2ecf20Sopenharmony_ci	0xbf, 0x3f, 0xfe, 0x7e, 0xa0, 0x20, 0xdc, 0x5c,
748c2ecf20Sopenharmony_ci	0xa8, 0x28, 0xe0, 0x60, 0x88, 0x08, 0xc6, 0x46,
758c2ecf20Sopenharmony_ci	0xb7, 0x37, 0xf0, 0x70, 0x98, 0x18, 0xd4, 0x54,
768c2ecf20Sopenharmony_ci	0xaa, 0x2a, 0xe2, 0x62, 0x8a, 0x0a, 0xc8, 0x48,
778c2ecf20Sopenharmony_ci	0xb9, 0x39, 0xf4, 0x74, 0x9a, 0x1a, 0xd6, 0x56,
788c2ecf20Sopenharmony_ci	0xa2, 0x22, 0xdd, 0x5d, 0x82, 0x02, 0xc0, 0x40,
798c2ecf20Sopenharmony_ci	0xb1, 0x31, 0xea, 0x6a, 0x92, 0x12, 0xcf, 0x4f,
808c2ecf20Sopenharmony_ci	0xae, 0x2e, 0xe6, 0x66, 0x8e, 0x0e, 0xcc, 0x4c,
818c2ecf20Sopenharmony_ci	0xbd, 0x3d, 0xfc, 0x7c, 0x9e, 0x1e, 0xda, 0x5a,
828c2ecf20Sopenharmony_ci	0xa6, 0x26, 0xdf, 0x5f, 0x86, 0x06, 0xc4, 0x44,
838c2ecf20Sopenharmony_ci	0xb5, 0x35, 0xee, 0x6e, 0x96, 0x16, 0xd2, 0x52
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* ulaw -> alaw */
878c2ecf20Sopenharmony_cistatic u8 ulaw_to_alaw[256] =
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	0xab, 0x55, 0xd5, 0x15, 0x95, 0x75, 0xf5, 0x35,
908c2ecf20Sopenharmony_ci	0xb5, 0x45, 0xc5, 0x05, 0x85, 0x65, 0xe5, 0x25,
918c2ecf20Sopenharmony_ci	0xa5, 0x5d, 0xdd, 0x1d, 0x9d, 0x7d, 0xfd, 0x3d,
928c2ecf20Sopenharmony_ci	0xbd, 0x4d, 0xcd, 0x0d, 0x8d, 0x6d, 0xed, 0x2d,
938c2ecf20Sopenharmony_ci	0xad, 0x51, 0xd1, 0x11, 0x91, 0x71, 0xf1, 0x31,
948c2ecf20Sopenharmony_ci	0xb1, 0x41, 0xc1, 0x01, 0x81, 0x61, 0xe1, 0x21,
958c2ecf20Sopenharmony_ci	0x59, 0xd9, 0x19, 0x99, 0x79, 0xf9, 0x39, 0xb9,
968c2ecf20Sopenharmony_ci	0x49, 0xc9, 0x09, 0x89, 0x69, 0xe9, 0x29, 0xa9,
978c2ecf20Sopenharmony_ci	0xd7, 0x17, 0x97, 0x77, 0xf7, 0x37, 0xb7, 0x47,
988c2ecf20Sopenharmony_ci	0xc7, 0x07, 0x87, 0x67, 0xe7, 0x27, 0xa7, 0xdf,
998c2ecf20Sopenharmony_ci	0x9f, 0x7f, 0xff, 0x3f, 0xbf, 0x4f, 0xcf, 0x0f,
1008c2ecf20Sopenharmony_ci	0x8f, 0x6f, 0xef, 0x2f, 0x53, 0x13, 0x73, 0x33,
1018c2ecf20Sopenharmony_ci	0xb3, 0x43, 0xc3, 0x03, 0x83, 0x63, 0xe3, 0x23,
1028c2ecf20Sopenharmony_ci	0xa3, 0x5b, 0xdb, 0x1b, 0x9b, 0x7b, 0xfb, 0x3b,
1038c2ecf20Sopenharmony_ci	0xbb, 0xbb, 0x4b, 0x4b, 0xcb, 0xcb, 0x0b, 0x0b,
1048c2ecf20Sopenharmony_ci	0x8b, 0x8b, 0x6b, 0x6b, 0xeb, 0xeb, 0x2b, 0x2b,
1058c2ecf20Sopenharmony_ci	0xab, 0x54, 0xd4, 0x14, 0x94, 0x74, 0xf4, 0x34,
1068c2ecf20Sopenharmony_ci	0xb4, 0x44, 0xc4, 0x04, 0x84, 0x64, 0xe4, 0x24,
1078c2ecf20Sopenharmony_ci	0xa4, 0x5c, 0xdc, 0x1c, 0x9c, 0x7c, 0xfc, 0x3c,
1088c2ecf20Sopenharmony_ci	0xbc, 0x4c, 0xcc, 0x0c, 0x8c, 0x6c, 0xec, 0x2c,
1098c2ecf20Sopenharmony_ci	0xac, 0x50, 0xd0, 0x10, 0x90, 0x70, 0xf0, 0x30,
1108c2ecf20Sopenharmony_ci	0xb0, 0x40, 0xc0, 0x00, 0x80, 0x60, 0xe0, 0x20,
1118c2ecf20Sopenharmony_ci	0x58, 0xd8, 0x18, 0x98, 0x78, 0xf8, 0x38, 0xb8,
1128c2ecf20Sopenharmony_ci	0x48, 0xc8, 0x08, 0x88, 0x68, 0xe8, 0x28, 0xa8,
1138c2ecf20Sopenharmony_ci	0xd6, 0x16, 0x96, 0x76, 0xf6, 0x36, 0xb6, 0x46,
1148c2ecf20Sopenharmony_ci	0xc6, 0x06, 0x86, 0x66, 0xe6, 0x26, 0xa6, 0xde,
1158c2ecf20Sopenharmony_ci	0x9e, 0x7e, 0xfe, 0x3e, 0xbe, 0x4e, 0xce, 0x0e,
1168c2ecf20Sopenharmony_ci	0x8e, 0x6e, 0xee, 0x2e, 0x52, 0x12, 0x72, 0x32,
1178c2ecf20Sopenharmony_ci	0xb2, 0x42, 0xc2, 0x02, 0x82, 0x62, 0xe2, 0x22,
1188c2ecf20Sopenharmony_ci	0xa2, 0x5a, 0xda, 0x1a, 0x9a, 0x7a, 0xfa, 0x3a,
1198c2ecf20Sopenharmony_ci	0xba, 0xba, 0x4a, 0x4a, 0xca, 0xca, 0x0a, 0x0a,
1208c2ecf20Sopenharmony_ci	0x8a, 0x8a, 0x6a, 0x6a, 0xea, 0xea, 0x2a, 0x2a
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* alaw -> 4bit compression */
1248c2ecf20Sopenharmony_cistatic u8 alaw_to_4bit[256] = {
1258c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1268c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
1278c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1288c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
1298c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1308c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
1318c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1328c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
1338c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1348c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
1358c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0d, 0x02,
1368c2ecf20Sopenharmony_ci	0x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
1378c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1388c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
1398c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1408c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
1418c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1428c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
1438c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1448c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
1458c2ecf20Sopenharmony_ci	0x0e, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
1468c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x08, 0x07, 0x0f, 0x01, 0x0a, 0x05,
1478c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1488c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x09, 0x07, 0x0f, 0x00, 0x0b, 0x04,
1498c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1508c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
1518c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1528c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
1538c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1548c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x08, 0x07, 0x0f, 0x00, 0x0b, 0x04,
1558c2ecf20Sopenharmony_ci	0x0e, 0x01, 0x0a, 0x05, 0x0f, 0x00, 0x0c, 0x03,
1568c2ecf20Sopenharmony_ci	0x0d, 0x02, 0x09, 0x06, 0x0f, 0x00, 0x0b, 0x04,
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* 4bit -> alaw decompression */
1608c2ecf20Sopenharmony_cistatic u8 _4bit_to_alaw[16] = {
1618c2ecf20Sopenharmony_ci	0x5d, 0x51, 0xd9, 0xd7, 0x5f, 0x53, 0xa3, 0x4b,
1628c2ecf20Sopenharmony_ci	0x2a, 0x3a, 0x22, 0x2e, 0x26, 0x56, 0x20, 0x2c,
1638c2ecf20Sopenharmony_ci};
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/* ulaw -> 4bit compression */
1668c2ecf20Sopenharmony_cistatic u8 ulaw_to_4bit[256] = {
1678c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1688c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1698c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1708c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1718c2ecf20Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1728c2ecf20Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
1738c2ecf20Sopenharmony_ci	0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
1748c2ecf20Sopenharmony_ci	0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
1758c2ecf20Sopenharmony_ci	0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
1768c2ecf20Sopenharmony_ci	0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04,
1778c2ecf20Sopenharmony_ci	0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
1788c2ecf20Sopenharmony_ci	0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
1798c2ecf20Sopenharmony_ci	0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
1808c2ecf20Sopenharmony_ci	0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
1818c2ecf20Sopenharmony_ci	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
1828c2ecf20Sopenharmony_ci	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08,
1838c2ecf20Sopenharmony_ci	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
1848c2ecf20Sopenharmony_ci	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
1858c2ecf20Sopenharmony_ci	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
1868c2ecf20Sopenharmony_ci	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
1878c2ecf20Sopenharmony_ci	0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
1888c2ecf20Sopenharmony_ci	0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
1898c2ecf20Sopenharmony_ci	0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
1908c2ecf20Sopenharmony_ci	0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d,
1918c2ecf20Sopenharmony_ci	0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
1928c2ecf20Sopenharmony_ci	0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b,
1938c2ecf20Sopenharmony_ci	0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
1948c2ecf20Sopenharmony_ci	0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a,
1958c2ecf20Sopenharmony_ci	0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
1968c2ecf20Sopenharmony_ci	0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
1978c2ecf20Sopenharmony_ci	0x09, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
1988c2ecf20Sopenharmony_ci	0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
1998c2ecf20Sopenharmony_ci};
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/* 4bit -> ulaw decompression */
2028c2ecf20Sopenharmony_cistatic u8 _4bit_to_ulaw[16] = {
2038c2ecf20Sopenharmony_ci	0x11, 0x21, 0x31, 0x40, 0x4e, 0x5c, 0x68, 0x71,
2048c2ecf20Sopenharmony_ci	0xfe, 0xef, 0xe7, 0xdb, 0xcd, 0xbf, 0xaf, 0x9f,
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci/*
2098c2ecf20Sopenharmony_ci * Compresses data to the result buffer
2108c2ecf20Sopenharmony_ci * The result size must be at least half of the input buffer.
2118c2ecf20Sopenharmony_ci * The number of samples also must be even!
2128c2ecf20Sopenharmony_ci */
2138c2ecf20Sopenharmony_ciint
2148c2ecf20Sopenharmony_cil1oip_law_to_4bit(u8 *data, int len, u8 *result, u32 *state)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	int ii, i = 0, o = 0;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	if (!len)
2198c2ecf20Sopenharmony_ci		return 0;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* send saved byte and first input byte */
2228c2ecf20Sopenharmony_ci	if (*state) {
2238c2ecf20Sopenharmony_ci		*result++ = table_com[(((*state) << 8) & 0xff00) | (*data++)];
2248c2ecf20Sopenharmony_ci		len--;
2258c2ecf20Sopenharmony_ci		o++;
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	ii = len >> 1;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	while (i < ii) {
2318c2ecf20Sopenharmony_ci		*result++ = table_com[(data[0]<<8) | (data[1])];
2328c2ecf20Sopenharmony_ci		data += 2;
2338c2ecf20Sopenharmony_ci		i++;
2348c2ecf20Sopenharmony_ci		o++;
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	/* if len has an odd number, we save byte for next call */
2388c2ecf20Sopenharmony_ci	if (len & 1)
2398c2ecf20Sopenharmony_ci		*state = 0x100 + *data;
2408c2ecf20Sopenharmony_ci	else
2418c2ecf20Sopenharmony_ci		*state = 0;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	return o;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/* Decompress data to the result buffer
2478c2ecf20Sopenharmony_ci * The result size must be the number of sample in packet. (2 * input data)
2488c2ecf20Sopenharmony_ci * The number of samples in the result are even!
2498c2ecf20Sopenharmony_ci */
2508c2ecf20Sopenharmony_ciint
2518c2ecf20Sopenharmony_cil1oip_4bit_to_law(u8 *data, int len, u8 *result)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	int i = 0;
2548c2ecf20Sopenharmony_ci	u16 r;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	while (i < len) {
2578c2ecf20Sopenharmony_ci		r = table_dec[*data++];
2588c2ecf20Sopenharmony_ci		*result++ = r >> 8;
2598c2ecf20Sopenharmony_ci		*result++ = r;
2608c2ecf20Sopenharmony_ci		i++;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return len << 1;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/*
2688c2ecf20Sopenharmony_ci * law conversion
2698c2ecf20Sopenharmony_ci */
2708c2ecf20Sopenharmony_ciint
2718c2ecf20Sopenharmony_cil1oip_alaw_to_ulaw(u8 *data, int len, u8 *result)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	int i = 0;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	while (i < len) {
2768c2ecf20Sopenharmony_ci		*result++ = alaw_to_ulaw[*data++];
2778c2ecf20Sopenharmony_ci		i++;
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	return len;
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ciint
2848c2ecf20Sopenharmony_cil1oip_ulaw_to_alaw(u8 *data, int len, u8 *result)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	int i = 0;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	while (i < len) {
2898c2ecf20Sopenharmony_ci		*result++ = ulaw_to_alaw[*data++];
2908c2ecf20Sopenharmony_ci		i++;
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	return len;
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/*
2988c2ecf20Sopenharmony_ci * generate/free compression and decompression table
2998c2ecf20Sopenharmony_ci */
3008c2ecf20Sopenharmony_civoid
3018c2ecf20Sopenharmony_cil1oip_4bit_free(void)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	vfree(table_dec);
3048c2ecf20Sopenharmony_ci	vfree(table_com);
3058c2ecf20Sopenharmony_ci	table_com = NULL;
3068c2ecf20Sopenharmony_ci	table_dec = NULL;
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ciint
3108c2ecf20Sopenharmony_cil1oip_4bit_alloc(int ulaw)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	int i1, i2, c, sample;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/* in case, it is called again */
3158c2ecf20Sopenharmony_ci	if (table_dec)
3168c2ecf20Sopenharmony_ci		return 0;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	/* alloc conversion tables */
3198c2ecf20Sopenharmony_ci	table_com = vzalloc(65536);
3208c2ecf20Sopenharmony_ci	table_dec = vzalloc(512);
3218c2ecf20Sopenharmony_ci	if (!table_com || !table_dec) {
3228c2ecf20Sopenharmony_ci		l1oip_4bit_free();
3238c2ecf20Sopenharmony_ci		return -ENOMEM;
3248c2ecf20Sopenharmony_ci	}
3258c2ecf20Sopenharmony_ci	/* generate compression table */
3268c2ecf20Sopenharmony_ci	i1 = 0;
3278c2ecf20Sopenharmony_ci	while (i1 < 256) {
3288c2ecf20Sopenharmony_ci		if (ulaw)
3298c2ecf20Sopenharmony_ci			c = ulaw_to_4bit[i1];
3308c2ecf20Sopenharmony_ci		else
3318c2ecf20Sopenharmony_ci			c = alaw_to_4bit[i1];
3328c2ecf20Sopenharmony_ci		i2 = 0;
3338c2ecf20Sopenharmony_ci		while (i2 < 256) {
3348c2ecf20Sopenharmony_ci			table_com[(i1 << 8) | i2] |= (c << 4);
3358c2ecf20Sopenharmony_ci			table_com[(i2 << 8) | i1] |= c;
3368c2ecf20Sopenharmony_ci			i2++;
3378c2ecf20Sopenharmony_ci		}
3388c2ecf20Sopenharmony_ci		i1++;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/* generate decompression table */
3428c2ecf20Sopenharmony_ci	i1 = 0;
3438c2ecf20Sopenharmony_ci	while (i1 < 16) {
3448c2ecf20Sopenharmony_ci		if (ulaw)
3458c2ecf20Sopenharmony_ci			sample = _4bit_to_ulaw[i1];
3468c2ecf20Sopenharmony_ci		else
3478c2ecf20Sopenharmony_ci			sample = _4bit_to_alaw[i1];
3488c2ecf20Sopenharmony_ci		i2 = 0;
3498c2ecf20Sopenharmony_ci		while (i2 < 16) {
3508c2ecf20Sopenharmony_ci			table_dec[(i1 << 4) | i2] |= (sample << 8);
3518c2ecf20Sopenharmony_ci			table_dec[(i2 << 4) | i1] |= sample;
3528c2ecf20Sopenharmony_ci			i2++;
3538c2ecf20Sopenharmony_ci		}
3548c2ecf20Sopenharmony_ci		i1++;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	return 0;
3588c2ecf20Sopenharmony_ci}
359