-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathaccompany_decoder.cpp
More file actions
329 lines (313 loc) · 10.1 KB
/
accompany_decoder.cpp
File metadata and controls
329 lines (313 loc) · 10.1 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "accompany_decoder.h"
#define LOG_TAG "AccompanyDecoder"
AccompanyDecoder::AccompanyDecoder() {
this->seek_seconds = 0.0f;
this->seek_req = false;
this->seek_resp = false;
accompanyFilePath = NULL;
}
AccompanyDecoder::~AccompanyDecoder() {
if(NULL != accompanyFilePath){
delete[] accompanyFilePath;
accompanyFilePath = NULL;
}
}
int AccompanyDecoder::getMusicMeta(const char* fileString, int * metaData) {
init(fileString);
int sampleRate = avCodecContext->sample_rate;
LOGI("sampleRate is %d", sampleRate);
int bitRate = avCodecContext->bit_rate;
LOGI("bitRate is %d", bitRate);
destroy();
metaData[0] = sampleRate;
metaData[1] = bitRate;
return 0;
}
int AccompanyDecoder::init(const char* fileString, int packetBufferSizeParam){
int ret= init(fileString);
packetBufferSize = packetBufferSizeParam;
return ret;
}
int AccompanyDecoder::init(const char* audioFile) {
LOGI("enter AccompanyDecoder::init");
audioBuffer = NULL;
position = -1.0f;
audioBufferCursor = 0;
audioBufferSize = 0;
swrContext = NULL;
swrBuffer = NULL;
swrBufferSize = 0;
seek_success_read_frame_success = true;
isNeedFirstFrameCorrectFlag = true;
firstFrameCorrectionInSecs = 0.0f;
avcodec_register_all();
av_register_all();
avFormatContext = avformat_alloc_context();
// 打开输入文件
LOGI("open accompany file %s....", audioFile);
if(NULL == accompanyFilePath){
int length = strlen(audioFile);
accompanyFilePath = new char[length + 1];
//由于最后一个是'\0' 所以memset的长度要设置为length+1
memset(accompanyFilePath, 0, length + 1);
memcpy(accompanyFilePath, audioFile, length + 1);
}
int result = avformat_open_input(&avFormatContext, audioFile, NULL, NULL);
if (result != 0) {
LOGI("can't open file %s result is %d", audioFile, result);
return -1;
} else {
LOGI("open file %s success and result is %d", audioFile, result);
}
avFormatContext->max_analyze_duration = 50000;
//检查在文件中的流的信息
result = avformat_find_stream_info(avFormatContext, NULL);
if (result < 0) {
LOGI("fail avformat_find_stream_info result is %d", result);
return -1;
} else {
LOGI("sucess avformat_find_stream_info result is %d", result);
}
stream_index = av_find_best_stream(avFormatContext, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
LOGI("stream_index is %d", stream_index);
// 没有音频
if (stream_index == -1) {
LOGI("no audio stream");
return -1;
}
//音频流
AVStream* audioStream = avFormatContext->streams[stream_index];
if (audioStream->time_base.den && audioStream->time_base.num)
timeBase = av_q2d(audioStream->time_base);
else if (audioStream->codec->time_base.den && audioStream->codec->time_base.num)
timeBase = av_q2d(audioStream->codec->time_base);
//获得音频流的解码器上下文
avCodecContext = audioStream->codec;
// 根据解码器上下文找到解码器
LOGI("avCodecContext->codec_id is %d AV_CODEC_ID_AAC is %d", avCodecContext->codec_id, AV_CODEC_ID_AAC);
AVCodec * avCodec = avcodec_find_decoder(avCodecContext->codec_id);
if (avCodec == NULL) {
LOGI("Unsupported codec ");
return -1;
}
// 打开解码器
result = avcodec_open2(avCodecContext, avCodec, NULL);
if (result < 0) {
LOGI("fail avformat_find_stream_info result is %d", result);
return -1;
} else {
LOGI("sucess avformat_find_stream_info result is %d", result);
}
//4、判断是否需要resampler
if (!audioCodecIsSupported()) {
LOGI("because of audio Codec Is Not Supported so we will init swresampler...");
/**
* 初始化resampler
* @param s Swr context, can be NULL
* @param out_ch_layout output channel layout (AV_CH_LAYOUT_*)
* @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*).
* @param out_sample_rate output sample rate (frequency in Hz)
* @param in_ch_layout input channel layout (AV_CH_LAYOUT_*)
* @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*).
* @param in_sample_rate input sample rate (frequency in Hz)
* @param log_offset logging level offset
* @param log_ctx parent logging context, can be NULL
*/
swrContext = swr_alloc_set_opts(NULL, av_get_default_channel_layout(OUT_PUT_CHANNELS), AV_SAMPLE_FMT_S16, avCodecContext->sample_rate,
av_get_default_channel_layout(avCodecContext->channels), avCodecContext->sample_fmt, avCodecContext->sample_rate, 0, NULL);
if (!swrContext || swr_init(swrContext)) {
if (swrContext)
swr_free(&swrContext);
avcodec_close(avCodecContext);
LOGI("init resampler failed...");
return -1;
}
}
LOGI("channels is %d sampleRate is %d", avCodecContext->channels, avCodecContext->sample_rate);
pAudioFrame = avcodec_alloc_frame();
// LOGI("leave AccompanyDecoder::init");
return 1;
}
bool AccompanyDecoder::audioCodecIsSupported() {
if (avCodecContext->sample_fmt == AV_SAMPLE_FMT_S16) {
return true;
}
return false;
}
AudioPacket* AccompanyDecoder::decodePacket(){
// LOGI("MadDecoder::decodePacket packetBufferSize is %d", packetBufferSize);
short* samples = new short[packetBufferSize];
// LOGI("accompanyPacket buffer's addr is %x", samples);
int stereoSampleSize = readSamples(samples, packetBufferSize);
AudioPacket* samplePacket = new AudioPacket();
if (stereoSampleSize > 0) {
//构造成一个packet
samplePacket->buffer = samples;
samplePacket->size = stereoSampleSize;
/** 这里由于每一个packet的大小不一样有可能是200ms 但是这样子position就有可能不准确了 **/
samplePacket->position = position;
} else {
samplePacket->size = -1;
}
return samplePacket;
}
int AccompanyDecoder::readSamples(short* samples, int size) {
if(seek_req){
audioBufferCursor = audioBufferSize;
}
int sampleSize = size;
while(size > 0){
if(audioBufferCursor < audioBufferSize){
int audioBufferDataSize = audioBufferSize - audioBufferCursor;
int copySize = MIN(size, audioBufferDataSize);
memcpy(samples + (sampleSize - size), audioBuffer + audioBufferCursor, copySize * 2);
size -= copySize;
audioBufferCursor += copySize;
} else{
if(readFrame() < 0){
break;
}
}
// LOGI("size is %d", size);
}
int fillSize = sampleSize - size;
if(fillSize == 0){
return -1;
}
return fillSize;
}
void AccompanyDecoder::seek_frame(){
LOGI("\n seek frame firstFrameCorrectionInSecs is %.6f, seek_seconds=%f, position=%f \n", firstFrameCorrectionInSecs, seek_seconds, position);
float targetPosition = seek_seconds;
float currentPosition = position;
float frameDuration = duration;
if(targetPosition < currentPosition){
this->destroy();
this->init(accompanyFilePath);
//TODO:这里GT的测试样本会差距25ms 不会累加
currentPosition = 0.0;
}
int readFrameCode = -1;
while (true) {
av_init_packet(&packet);
readFrameCode = av_read_frame(avFormatContext, &packet);
if (readFrameCode >= 0) {
currentPosition += frameDuration;
if (currentPosition >= targetPosition) {
break;
}
}
// LOGI("currentPosition is %.3f", currentPosition);
av_free_packet(&packet);
}
seek_resp = true;
seek_req = false;
seek_success_read_frame_success = false;
}
int AccompanyDecoder::readFrame() {
// LOGI("enter AccompanyDecoder::readFrame");
if(seek_req){
this->seek_frame();
}
int ret = 1;
av_init_packet(&packet);
int gotframe = 0;
int readFrameCode = -1;
while (true) {
readFrameCode = av_read_frame(avFormatContext, &packet);
// LOGI(" av_read_frame ...", duration);
if (readFrameCode >= 0) {
if (packet.stream_index == stream_index) {
int len = avcodec_decode_audio4(avCodecContext, pAudioFrame,
&gotframe, &packet);
// LOGI(" avcodec_decode_audio4 ...", duration);
if (len < 0) {
LOGI("decode audio error, skip packet");
}
if (gotframe) {
int numChannels = OUT_PUT_CHANNELS;
int numFrames = 0;
void * audioData;
if (swrContext) {
const int ratio = 2;
const int bufSize = av_samples_get_buffer_size(NULL,
numChannels, pAudioFrame->nb_samples * ratio,
AV_SAMPLE_FMT_S16, 1);
if (!swrBuffer || swrBufferSize < bufSize) {
swrBufferSize = bufSize;
swrBuffer = realloc(swrBuffer, swrBufferSize);
}
byte *outbuf[2] = { (byte*) swrBuffer, NULL };
numFrames = swr_convert(swrContext, outbuf,
pAudioFrame->nb_samples * ratio,
(const uint8_t **) pAudioFrame->data,
pAudioFrame->nb_samples);
if (numFrames < 0) {
LOGI("fail resample audio");
ret = -1;
break;
}
audioData = swrBuffer;
} else {
if (avCodecContext->sample_fmt != AV_SAMPLE_FMT_S16) {
LOGI("bucheck, audio format is invalid");
ret = -1;
break;
}
audioData = pAudioFrame->data[0];
numFrames = pAudioFrame->nb_samples;
}
if(isNeedFirstFrameCorrectFlag && position >= 0){
float expectedPosition = position + duration;
float actualPosition = av_frame_get_best_effort_timestamp(pAudioFrame) * timeBase;
firstFrameCorrectionInSecs = actualPosition - expectedPosition;
isNeedFirstFrameCorrectFlag = false;
}
duration = av_frame_get_pkt_duration(pAudioFrame) * timeBase;
position = av_frame_get_best_effort_timestamp(pAudioFrame) * timeBase - firstFrameCorrectionInSecs;
if (!seek_success_read_frame_success) {
LOGI("position is %.6f", position);
actualSeekPosition = position;
seek_success_read_frame_success = true;
}
audioBufferSize = numFrames * numChannels;
// LOGI(" \n duration is %.6f position is %.6f audioBufferSize is %d\n", duration, position, audioBufferSize);
audioBuffer = (short*) audioData;
audioBufferCursor = 0;
break;
}
}
} else {
ret = -1;
break;
}
}
av_free_packet(&packet);
return ret;
}
void AccompanyDecoder::destroy() {
// LOGI("start destroy!!!");
if (NULL != swrBuffer) {
free(swrBuffer);
swrBuffer = NULL;
swrBufferSize = 0;
}
if (NULL != swrContext) {
swr_free(&swrContext);
swrContext = NULL;
}
if (NULL != pAudioFrame) {
av_free (pAudioFrame);
pAudioFrame = NULL;
}
if (NULL != avCodecContext) {
avcodec_close(avCodecContext);
avCodecContext = NULL;
}
if (NULL != avFormatContext) {
LOGI("leave LiveReceiver::destory");
avformat_close_input(&avFormatContext);
avFormatContext = NULL;
}
// LOGI("end destroy!!!");
}