#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};
static int search(int val, short *table, int size)
{
int i;
for (i = 0; i < size; i++)
{
if (val <= *table++)
return (i);
}
return (size);
}
unsigned char linear2alaw(int pcm_val)
{
int mask;
int seg;
unsigned char aval;
if (pcm_val >= 0)
{
mask = 0xD5; /* sign (7th) bit = 1 */ ,//(1).这个地方mask为什么不是0x80呢?
}
else
{
mask = 0x55; /* sign bit = 0 */ //(2).这个地方mask为什么不是0x00呢?
pcm_val = -pcm_val - 8; //(3).这个地方为什么要-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);//(4).这个地方为什么要异或mask,而不是输出0x7F或者0xFF呢?
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); //(5).这个地方为什么要异或mask,而不是直接输出呢?
}
}
此帖出自
小平头技术问答
一周热门 更多>