-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.cpp
150 lines (127 loc) · 3.29 KB
/
tools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "tools.h"
#include <stdio.h>
#include <string.h>
/*
* 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. */
#define BIAS (0x84) /* Bias for linear code. */
static short seg_end[8] = { 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF };
static int search(int val, short *table, int size) {
int i;
for (i = 0; i < size; i++) {
if (val <= *table++)
return (i);
}
return (size);
}
static 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));
}
/*
* linear2ulaw() - Convert a linear PCM value to u-law
*
*/
static 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);
}
}
Tools::Tools()
{
}
int Tools::g711u_decode(short amp[], const unsigned char g711u_data[], int g711u_bytes)
{
int i;
int samples;
unsigned char code;
int sl;
for (samples = i = 0;;) {
if (i >= g711u_bytes)
break;
code = g711u_data[i++];
sl = ulaw2linear(code);
amp[samples++] = (short) sl;
}
return samples * 2;
}
int Tools::g711u_encode(unsigned char g711_data[], const short amp[], int len)
{
int i;
for (i = 0; i < len; i++) {
g711_data[i] = linear2ulaw(amp[i]);
}
return len;
}
QByteArray Tools::hexStringToArray(QString hex)
{
QByteArray tmp=hex.toLatin1();
QByteArray array;
char *h=tmp.data();
char *p=h;
int v;
while(p<h+tmp.length()){
if(*p==' '){
p++;
continue;
}
// printf("%.2s",p);
sscanf(p,"%02X", &v);
array.append(v);
p+=2;
}
return array;
}
QString Tools::arrayToHexString(QByteArray &array)
{
QString hex;
for(int i=0;i < array.length();i++){
hex+= QString().sprintf("%02X ", (0xFF&array[i]));
}
return hex;
}
QString Tools::arrayToASCIIString(QByteArray &array)
{
QString hex;
for(int i=0;i < array.length();i++){
if((0xFF&array[i])> 32 && (0xFF&array[i])<126)
hex+= QString().sprintf("%c", (0xFF&array[i]));
else
hex+=" ";
}
return hex;
}