-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMZCodec.cpp
293 lines (217 loc) · 8.88 KB
/
MZCodec.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// MZCodec.cpp
// MZAudioCodec
//
// Created by mark.zhang on 25/01/2018.
// Copyright © 2018 mark.zhang. All rights reserved.
//
#include "MZCodec.h"
#include "faac.h"
#include "faad.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
//http://blog.csdn.net/gavinr/article/details/6959198
//http://blog.csdn.net/guofengpu/article/details/52026791
//http://blog.51cto.com/13136504/2056948
struct MZWavAudioFileHeader
{
char riff[4]; // 字符串 "RIFF"
uint32_t totalLength; // 文件总大小, 包括PCM 数据大小和该文件头大小
char wave[4]; // 字符串 "WAVE"
char fmt[4]; // 字符串 "fmt "
uint32_t format; // WAV 头大小, 固定为值 16
uint16_t pcm; // PCM 编码方式, 固定值为 1
uint16_t channels; // 声道数量, 为 2
uint32_t frequency; // 采样频率
uint32_t bytes_per_second; // 每秒字节数(码率), 其值=采样率x通道数x位深度/8
uint16_t bytes_by_capture; // 采样块大小
uint16_t bits_per_sample; // 采样点大小, 这里是 16 位
char data[4]; // 字符串 "data"
uint32_t bytes_in_pcmdata; // pcm 数据长度
};
/**
* nSampleRate 采样率
* nChannels 通道数
* nPCMBitSize 单位本位数(深度)
*/
int codeWAV(const char *srcFilePath, const char *destPath, unsigned long nSampleRate, unsigned long nChannels, unsigned long nPCMBitSize) {
// unsigned long nSampleRate = 44100; //采样率
// unsigned int nChannels = 2; //声道数
// unsigned int nPCMBitSize = 16; //单样本位数
unsigned long nInputSamples = 0;
unsigned long nMaxOutputBytes = 0;
int nRet;
faacEncHandle hEncoder;
faacEncConfigurationPtr pConfiguration;
size_t nBytesRead;
unsigned long nPCMBufferSize;
unsigned char *pbPCMBuffer;
unsigned char *pbAACBuffer;
FILE *fpIn; // 输入 WAV 文件
FILE *fpOut; // 输出 AAC 文件
/// 获取 faac 版本信息
{
char *version;
char *copyright;
faacEncGetVersion(&version, ©right);
printf("FAAC version: %s, copyright: %s\n", version, copyright);
}
fpIn = fopen(srcFilePath, "rb");
if (NULL == fpIn) {
return -2;
}
fpOut = fopen(destPath, "wb");
/// 1. 打开 FAAC
hEncoder = faacEncOpen(nSampleRate, nChannels, &nInputSamples, &nMaxOutputBytes);
if (NULL == hEncoder) {
printf("[ERROR] Failed to call faacEncOpen()\n");
return -1;
}
nPCMBufferSize = nInputSamples * nPCMBitSize / 8;
pbPCMBuffer = new unsigned char[nPCMBufferSize];
pbAACBuffer = new unsigned char[nMaxOutputBytes];
/// 2.1. 获取当前的编码器配置
pConfiguration = faacEncGetCurrentConfiguration(hEncoder);
pConfiguration->inputFormat = FAAC_INPUT_16BIT;
// 对象类型只有为 LOW, iOS 的 AVAudioPlayer 才能播放
pConfiguration->aacObjectType = LOW;
// 0 = Raw; 1 = ADTS
pConfiguration->outputFormat = 1;
pConfiguration->mpegVersion = MPEG4;
pConfiguration->useTns = 1;
pConfiguration->bitRate = 30;
/// 2.2. 配置编码器
nRet = faacEncSetConfiguration(hEncoder, pConfiguration);
//是wav格式, 先读取前面的
fseek(fpIn, 58, SEEK_SET);
do {
//读入的实际字节数,最大不会超过 nPCMBufferSize
nBytesRead = fread(pbPCMBuffer, 1, nPCMBufferSize, fpIn);
//输入样本数,用实际读入字节数计算
//一般只有读到文件尾时才不是 nPCMBufferSize/(nPCMBitSize/8)
nInputSamples = nBytesRead / (nPCMBitSize / 8);
/// 3. 编码
nRet = faacEncEncode(hEncoder,
(int *)pbPCMBuffer,
(unsigned int)nInputSamples,
pbAACBuffer,
(unsigned int)nMaxOutputBytes);
fwrite(pbAACBuffer, 1, nRet, fpOut);
printf("FaacEncEncode returns %d\n", nRet);
} while (nBytesRead > 0);
/// 4. 关闭 FAAC
nRet = faacEncClose(hEncoder);
delete[] pbPCMBuffer;
delete[] pbAACBuffer;
fclose(fpIn);
fclose(fpOut);
return 0;
}
/**
* 写入 wav 头数据.
*
* @param file wav 文件指针.
* @param total_samples_per_channel 每个声道的采样数.
* @param samplerate 采样率.
* @param channels 声道数.
*/
void mz_write_wav_header(FILE *file, int total_samples_per_channel, int samplerate, int channels) {
if (NULL == file) {
return;
}
if (total_samples_per_channel <= 0) {
return;
}
printf("FAAD. total_samples_per_channel: %i, samplerate: %i, channels: %i\n",
total_samples_per_channel, samplerate, channels);
struct MZWavAudioFileHeader wavHeader;
// 写入 RIFF
strcpy(wavHeader.riff, "RIFF");
wavHeader.bits_per_sample = 16;
wavHeader.totalLength = (total_samples_per_channel * channels * wavHeader.bits_per_sample/8) + sizeof(wavHeader) - 8;
// 写入 WAVE 和 fmt
strcpy(wavHeader.wave, "WAVE");
strcpy(wavHeader.fmt, "fmt ");
wavHeader.format = 16;
wavHeader.pcm = 1;
wavHeader.channels = channels;
wavHeader.frequency = samplerate;
// 每秒的字节数(码率)=采样率x通道数x位深度/8
wavHeader.bytes_per_second = wavHeader.channels * wavHeader.frequency * wavHeader.bits_per_sample/8;
wavHeader.bytes_by_capture = wavHeader.channels*wavHeader.bits_per_sample/8;
wavHeader.bytes_in_pcmdata = total_samples_per_channel * wavHeader.channels * wavHeader.bits_per_sample/8;
// 写入 data
strcpy(wavHeader.data, "data");
fwrite(&wavHeader, 1, sizeof(wavHeader), file);
}
int decodeAAC(const char *aacfile, const char *wavename) {
// 获取句柄
NeAACDecHandle faadhandle = NeAACDecOpen();
if (faadhandle) {
FILE *inputFile = fopen(aacfile, "rb");
if (NULL == inputFile) {
return -2;
}
// 移动文件的读写指针到文件末尾
fseek(inputFile, 0, SEEK_END);
// 获取文件长度
long filelen = ftell(inputFile);
// 重新移动到文件开头
fseek(inputFile, 0, SEEK_SET);
unsigned char *filebuf = (unsigned char *)malloc(filelen);
size_t len = fread(filebuf, 1, filelen, inputFile);
fclose(inputFile);
unsigned long samplerate = 0;
unsigned char channel = 0;
// 初始化解码器
long ret = NeAACDecInit(faadhandle, filebuf, len, &samplerate, &channel);
if (ret >= 0) {
printf("FAAD. aac init: sam = %lu, chn = %d\n", samplerate, channel);
NeAACDecFrameInfo frameinfo;
unsigned char *curbyte = filebuf;
unsigned long leftsize = len;
FILE *outputFile = fopen(wavename, "wb");
if (NULL == outputFile) {
return -3;
}
int wavheadsize = sizeof(struct MZWavAudioFileHeader);
printf("FAAD. The WAV file header size: %i\n", wavheadsize);
fseek(outputFile, wavheadsize, SEEK_SET);
int totalsmp_per_chl = 0;
void *out = NULL;
while (1) {
// 解码(一帧音频)
out = NeAACDecDecode(faadhandle, &frameinfo, curbyte, leftsize);
if (NULL == out) {
break;
}
printf("FAAD. decode a frame: samplerate:%ld, \
channels=%d, \
samplecount=%ld, \
obj_type=%d, \
header_type=%d, \
bytesconsumed=%ld\n",
frameinfo.samplerate,
frameinfo.channels,
frameinfo.samples,
frameinfo.object_type,
frameinfo.header_type,
frameinfo.bytesconsumed);
curbyte += frameinfo.bytesconsumed;
leftsize -= frameinfo.bytesconsumed;
fwrite(out, 1, frameinfo.samples*2, outputFile);
// 每个声道的采样总数
totalsmp_per_chl += frameinfo.samples / frameinfo.channels;
}
fseek(outputFile, 0, SEEK_SET);
mz_write_wav_header(outputFile, totalsmp_per_chl, (int)samplerate, (int)channel);
fclose(outputFile);
}
free(filebuf);
// 关闭句柄
NeAACDecClose(faadhandle);
}
return 0;
}