g711解码音频,能够播放但是带有很强的哒哒的音声,请高手指教,这是什么原因?谢谢

cyg19860205 2011-01-24 03:40:55
如题:谢谢各位~
...全文
3909 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyg19860205 2011-06-04
  • 打赏
  • 举报
回复 3
确实如楼上所说,去掉语音数据的帧头数据去掉就可以了,我当时去的是4个字节的帧头请教前辈解决的~~
expp 2011-05-16
  • 打赏
  • 举报
回复 2
今天刚好碰到同样的问题,google, baidu均无解,无奈自己研究,是因为语音数据里包含了RTP包头的原因,自己写了个转换工具,把12Bytes的RTP包头去掉,哒哒声就没了
cyg19860205 2011-01-27
  • 打赏
  • 举报
回复
谢谢~,正在努力研究,先结贴了
tufaqing 2011-01-25
  • 打赏
  • 举报
回复
你这个就不能确定是解码算法的问题呢,你的g711编码文件是哪里来的,有可能不是真正的g711 alaw,说不定是海思自己编码的变形形式。windows下带有解码器的,你将g711打包成wav就可以用windows media player播放了,就知道你的声音对不对了。alaw的formattag = 6
cyg19860205 2011-01-24
  • 打赏
  • 举报
回复
TO:tufaqing

现在解码出来音频播放有问题,所以来确定音频解码是否正确,所以将解码后的数据写成wav文件在windows下用千千静听和Media Player播放效果一样而且跟我在linux下面打开音频设备进行播放效果也一样(播放时音乐歌声清晰,但是有哒哒的噪音,即使没有歌声也是出现哒哒的声音)

关于在windows下解码能够正常播放不是用的我提供的算法在windows系统下解码后播放的,是通过海思的解码SDK进行解码能够正常播放的。

tufaqing,非常感谢你热心的帮助。
tufaqing 2011-01-24
  • 打赏
  • 举报
回复
建议在windows下转换成wav用其它播放器播放吧,你这样可能是其它方面的原因。
1. g711音频源对不对,你说windows下播放正常,播放器不会直接识别g711源数据的,要打包成wav或avi等才能播放,不知道你说的能正常播放是指什么。如果你打包的是其它格式,确定和windows进入解码器的g711数据是一样的么?
2. 你自己写播放声音,要有可能是你的播放控制过程没有写好。
cyg19860205 2011-01-24
  • 打赏
  • 举报
回复
TO:tufaqing
比较郁闷了
还是不行。。。
刚接触音视频解码,视频是用ffmpeg解码SDL播放的,音频总是搞不定。。。

tufaqing 2011-01-24
  • 打赏
  • 举报
回复
你的音频比较特别吗?我这个是ffmpeg的解码,测试了好多alaw音频都是好的。不知道是不是转换越界的原因
你的解码:out_data[i] = (short)alaw2linear((unsigned char)pBuffer[i]);
换成:int v = alaw2linear((unsigned char)pBuffer[i]);
out_data[i] = v < -32768 ? -32768 : v > 32767 ? 32767 : v;
cyg19860205 2011-01-24
  • 打赏
  • 举报
回复
TO:tufaqing

试过了,解出来的音频播放出现音质不清楚了,也有吱吱的噪音
原来的算法是音质清楚,但是有哒哒的声音。
我觉得是算法的原因吧,是在网上找到的算法进行解码后效果都一样(音质清楚,但是有哒哒的噪音声)

非常感谢你的帮助
tufaqing 2011-01-24
  • 打赏
  • 举报
回复
你的alaw2linear这个函数可能有点问题,t的越界计算好像不一样,请替换成我的代码看看。
cyg19860205 2011-01-24
  • 打赏
  • 举报
回复
在函数外进行了强制类型转换了

int G711Decode(char* pRawData,const unsigned char* pBuffer, int nBufferSize)
{
short *out_data = (short*)pRawData;
int i = 0;
for(; i<nBufferSize; i++)
{
out_data[i] = (short)alaw2linear((unsigned char)pBuffer[i]);
}
return nBufferSize * 2;
}

tufaqing 2011-01-24
  • 打赏
  • 举报
回复
看看你的解码代码和保存wav的代码。这个函数转换的16bit的PCM,而你的函数转换出来的是int,要转换成short写文件。
cyg19860205 2011-01-24
  • 打赏
  • 举报
回复
谢谢楼上回复,用的下面的算法,但是我还是有疑问
/*
* This source code is a product of Sun Microsystems, Inc. and is provided
* for unrestricted use. Users may copy or modify this source code without
* charge.
*
* SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
* THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* Sun source code is provided with no support and without any obligation on
* the part of Sun Microsystems, Inc. to assist in its use, correction,
* modification or enhancement.
*
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
* OR ANY PART THEREOF.
*
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Sun has been advised of the possibility of such damages.
*
* Sun Microsystems, Inc.
* 2550 Garcia Avenue
* Mountain View, California 94043
*/

/*
* g711.c
*
* u-law, A-law and linear PCM conversions.
*/
#include "codec.h"

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
*
*/
//modify by cyg 1.24
int alaw2linear(
//static short 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)));
}

实现方法是读取一帧解一帧的,不明白你说的开始需要初始化,希望指教,谢谢~
tufaqing 2011-01-24
  • 打赏
  • 举报
回复
你这只是一个初始化建table函数
tufaqing 2011-01-24
  • 打赏
  • 举报
回复
不是这么简单就直接转换的,开始要初始化,参考下面链接,里面有我回复的代码:
http://topic.csdn.net/u/20101101/09/7883c764-135c-44cd-8b1a-8952e9a933ac.html
cyg19860205 2011-01-24
  • 打赏
  • 举报
回复
是在linux下面进行解码并打开音频设备播放。
音频数据通过g711解码后写成wav文件,在windows下使用播放器播放同我写入音频设备播放是同样的效果,都带有哒哒的声音。
录像文件确实是g711压缩的,因为在windows下通过解码并播放成功。

cyg19860205 2011-01-24
  • 打赏
  • 举报
回复
使用在网上找到的g711解码算法:
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);
}

2,554

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧