c语言 74编码,G711编解码的c语言程序?

展开全部

/*

* 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;

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;

}

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)));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: LZ77 是一种常用的无损数据压缩算法,它通过在输入数据中找到重复的子串并用短的“指针”来替换它们来实现压缩。 下面是一个简单的 C 语言程序,用于实现 LZ77 编码和解码的功能。 首先,我们需要定义一个结构体来存储每个压缩块的信息: ``` struct LZ77Block { int offset; // 子串的偏移量 int length; // 子串的长度 char next; // 下一个字符 }; ``` 然后我们可以编写一个函数来执行 LZ77 编码,它接受一个字符串作为输入,并返回一个 LZ77Block 数组,其中包含了压缩后的信息: ``` struct LZ77Block* encode(char* input) { // 为压缩块数组分配内存 struct LZ77Block* blocks = malloc(strlen(input) * sizeof(struct LZ77Block)); // 遍历输入字符串 int i = 0; while (input[i] != '\0') { // 初始化压缩块 struct LZ77Block block = {0, 0, input[i]}; // 在输入字符串中查找最长的匹配子串 int j = 1; while (i - j >= 0 && input[i - j] == input[i + block.length]) { block.length++; j++; } // 记录偏移量 block.offset = j; // 将压缩块添加到数组中 blocks[i] = block; // 移动到下一个字符 i += block.length + 1; } // 返回 ### 回答2: LZ77是一种无损数据压缩算法,可以实现对数据的编码和解码。以下是用C语言实现LZ77编码和解码的一个简单示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_WINDOW_SIZE 256 #define MAX_LOOKAHEAD_SIZE 16 void lz77_encode(const char* input, char* encoded) { int inputLength = strlen(input); int encodedIndex = 0; int currentIndex = 0; while (currentIndex < inputLength) { int matchIndex = -1; int matchLength = 0; int windowStart = (currentIndex - MAX_WINDOW_SIZE) > 0 ? (currentIndex - MAX_WINDOW_SIZE) : 0; int lookaheadEnd = (currentIndex + MAX_LOOKAHEAD_SIZE) < inputLength ? (currentIndex + MAX_LOOKAHEAD_SIZE) : inputLength; for (int i = windowStart; i < currentIndex; i++) { int currentLength = 0; while (currentIndex + currentLength < lookaheadEnd && input[i + currentLength] == input[currentIndex + currentLength]) { currentLength++; } if (currentLength > matchLength) { matchIndex = i; matchLength = currentLength; } } if (matchLength > 0) { encoded[encodedIndex++] = (currentIndex - matchIndex) & 0xFF; encoded[encodedIndex++] = matchLength & 0xFF; } else { encoded[encodedIndex++] = 0; encoded[encodedIndex++] = 0; } currentIndex += matchLength + 1; } } void lz77_decode(const char* encoded, char* decoded) { int encodedLength = strlen(encoded); int decodedIndex = 0; int encodedIndex = 0; while (encodedIndex < encodedLength) { int offset = encoded[encodedIndex++] & 0xFF; int length = encoded[encodedIndex++] & 0XFF; if (offset == 0 && length == 0) { break; } for (int i = 0; i < length; i++) { decoded[decodedIndex] = decoded[decodedIndex - offset]; decodedIndex++; } decoded[decodedIndex++] = encoded[encodedIndex++]; } decoded[decodedIndex] = '\0'; } int main() { const char* input = "LZ77 compression algorithm"; char encoded[300] = { 0 }; char decoded[300] = { 0 }; lz77_encode(input, encoded); lz77_decode(encoded, decoded); printf("Input: %s\n", input); printf("Encoded: %s\n", encoded); printf("Decoded: %s\n", decoded); return 0; } ``` 此代码实现了LZ77算法的编码和解码功能。`lz77_encode`函数负责对输入字符串进行编码,输出为编码后的字符串。`lz77_decode`函数负责对编码字符串进行解码,输出为解码后的字符串。在`main`函数中,我们使用了一个示例字符串“LZ77 compression algorithm”,并打印出编码和解码结果。 请注意,此示例代码只是一个简单的实现,可能存在一些限制和性能问题。在实际应用中,我们可能需要进行更多的错误处理和性能优化。 ### 回答3: 下面是一个用C语言编写的LZ77解码和编码程序: 解码程序: ```c #include <stdio.h> #include <string.h> #define MAX_BUFFER_SIZE 256 void LZ77Decoding(char* encodedData) { int length = strlen(encodedData); char buffer[MAX_BUFFER_SIZE]; int bufferLength = 0; int i = 0; while (i < length) { if (encodedData[i] == '0' && encodedData[i+1] == '0') { printf("%c", encodedData[i+2]); buffer[bufferLength++] = encodedData[i+2]; i += 3; } else { int distance = (encodedData[i] - '0') * 10 + (encodedData[i+1] - '0'); int length = encodedData[i+2] - '0'; for (int j = 0; j < length; j++) { printf("%c", buffer[bufferLength-distance+j]); buffer[bufferLength++] = buffer[bufferLength-distance+j]; } i += 3; } } printf("\n"); } int main() { char encodedData[] = "010000'u'033's'017000'"; LZ77Decoding(encodedData); return 0; } ``` 编码程序: ```c #include <stdio.h> #include <string.h> #define MAX_BUFFER_SIZE 256 void LZ77Encoding(char* data) { int length = strlen(data); char buffer[MAX_BUFFER_SIZE]; int bufferLength = 0; int i = 0; while (i < length) { int distance = 0; int len = 0; for (int j = bufferLength - 1; j >= 0; j--) { if (buffer[j] == data[i]) { int tempDistance = bufferLength - j; int tempLen = 1; for (int k = 1; k < length - i; k++) { if (buffer[j + k] == data[i + k]) { tempLen++; } else { break; } } if (tempLen > len) { distance = tempDistance; len = tempLen; } } } if (len > 2) { printf("%d%d%d", distance / 10, distance % 10, len); buffer[bufferLength++] = data[i++]; for (int j = 1; j < len; j++) { buffer[bufferLength++] = data[i++]; } } else { printf("00%c", data[i]); buffer[bufferLength++] = data[i++]; } } printf("\n"); } int main() { char data[] = "uuussssssssssssss"; LZ77Encoding(data); return 0; } ``` 以上为一个简单的LZ77编码和解码的C语言程序,可以根据需要输入不同的数据进行编码和解码。这个程序采用了固定大小的缓冲区来存储已经编码或解码的数据,并根据编码和解码规则进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值