本次安装基于系统CentOS Linux release 6.0 (Final)
1.下载源码
ftp下载地址: ftp://ftp.mars.org/pub/mpeg/
2.解压源码
$ tar zxvf libmad-0.15.0b.tar.gz
3.编译前配置
$ cd libmad-0.15.0b
$ ./configure --prefix=$HOME/local/prior
4.编译源码
$ make
出现错误
gcc -DHAVE_CONFIG_H -I. -I. -I. -DFPM_INTEL -DASO_ZEROCHECK -Wall -march=i486 -g -O -fforce-mem -fforce-addr -fthread-jumps -fcse-follow-jumps -fcse-skip-blocks -fexpensive-optimizations -fregmove -fschedule-insns2 -fstrength-reduce -MT version.lo -MD -MP -MF .deps/version.Tpo -c version.c -fPIC -DPIC -o .libs/version.lo
cc1: error: unrecognized command line option "-fforce-mem"
make[2]: *** [version.lo] Error 1
make[2]: Leaving directory `/home/mad/downloads/libmad-0.15.0b'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/mad/downloads/libmad-0.15.0b'
make: *** [all] Error 2
5.重新编译
打开Makefile,找到-fforce-mem并删除
$ make
6.安装libmad
$ make install
7.修改minimad.c
添加全局变量
FILE *pcm_fp;
修改output函数
enum mad_flow output(void *data,
struct mad_header const *header,
struct mad_pcm *pcm)
{
unsigned int nchannels, nsamples;
mad_fixed_t const *left_ch, *right_ch;
char tmpchar;
/* pcm->samplerate contains the sampling frequency */
nchannels = pcm->channels;
nsamples = pcm->length;
left_ch = pcm->samples[0];
right_ch = pcm->samples[1];
while (nsamples--) {
signed int sample;
/* output sample(s) in 16-bit signed little-endian PCM */
sample = scale(*left_ch++);
tmpchar = ((sample >> 0) & 0xff);
fwrite(&tmpchar, 1, 1, pcm_fp);
tmpchar = ((sample >> 8) & 0xff);
fwrite(&tmpchar, 1, 1, pcm_fp);
//putchar((sample >> 0) & 0xff);
//putchar((sample >> 8) & 0xff);
if (nchannels == 2) {
sample = scale(*right_ch++);
tmpchar = ((sample >> 0) & 0xff);
fwrite(&tmpchar, 1, 1, pcm_fp);
tmpchar = ((sample >> 8) & 0xff);
fwrite(&tmpchar, 1, 1, pcm_fp);
//putchar((sample >> 0) & 0xff);
//putchar((sample >> 8) & 0xff);
}
}
return MAD_FLOW_CONTINUE;
}
修改主函数
int main(int argc, char *argv[])
{
struct stat stat;
char file[2][256];
void *fdm;
int fd;
if (argc != 3)
return 1;
memset(file, 0x00, sizeof(file));
strncpy(file[0], argv[1], 255);
strncpy(file[1], argv[2], 255);
printf("%s\n", file[0]);
pcm_fp = fopen(file[1], "wb");
fd = open(file[0], O_RDONLY);
if (fstat(fd, &stat) == -1 ||
stat.st_size == 0)
return 2;
fdm = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (fdm == MAP_FAILED)
return 3;
printf("decode begin\n");
decode(fdm, stat.st_size);
printf("decode end\n");
if (munmap(fdm, stat.st_size) == -1)
return 4;
fclose(pcm_fp);
return 0;
}
编译minimad
$ gcc -o minimad minimad.c -L$HOME/local/prior/lib -lmad
8.测试运行
$ ./minimad 3.mp3 3.pcm
成功生成解码后的3.pcm文件, 下面报错属正常现象
decoding error 0x0101 (lost synchronization) at byte offset 0
decoding error 0x0101 (lost synchronization) at byte offset 3548123
用Audacity能正常播放