-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFFMpegExtractor.cpp
366 lines (302 loc) · 11.7 KB
/
FFMpegExtractor.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// Created by asalehin on 10/26/20.
//
#include "FFMpegExtractor.h"
#include "../utils/Utils.h"
constexpr int kInternalBufferSize = 1152;
int read(void *data, uint8_t *buf, int buf_size) {
auto *hctx = (FFMpegExtractor*)data;
size_t len = fread(buf, 1, buf_size, hctx->fp.get());
if (len == 0) {
// Let FFmpeg know that we have reached EOF, or do something else
return AVERROR_EOF;
}
return (int)len;
}
int64_t seek(void *data, int64_t pos, int whence) {
auto *hctx = (FFMpegExtractor*)data;
if (whence == AVSEEK_SIZE) {
return getSizeOfFile(hctx->fp.get());
}
int rs = fseek(hctx->fp.get(), (long)pos, whence);
if (rs != 0) {
return -1;
}
long fpos = ftell(hctx->fp.get()); // int64_t is usually long long
return (int64_t)fpos;
}
bool FFMpegExtractor::createAVIOContext(uint8_t *buffer, uint32_t bufferSize,
AVIOContext **avioContext) {
constexpr int isBufferWriteable = 0;
*avioContext = avio_alloc_context(
buffer, // internal buffer for FFmpeg to use
bufferSize, // For optimal decoding speed this should be the protocol block size
isBufferWriteable,
(void*)this, // Will be passed to our callback functions as a (void *)
read, // Read callback function
nullptr, // Write callback function (not used)
seek); // Seek callback function
if (!*avioContext){
LOGE("Failed to create AVIO context");
return false;
} else {
return true;
}
}
bool
FFMpegExtractor::createAVFormatContext(AVIOContext *avioContext, AVFormatContext **avFormatContext) {
*avFormatContext = avformat_alloc_context();
(*avFormatContext)->pb = avioContext;
if (!*avFormatContext){
LOGE("Failed to create AVFormatContext");
return false;
} else {
return true;
}
}
bool FFMpegExtractor::openAVFormatContext(AVFormatContext *avFormatContext) {
int result = avformat_open_input(&avFormatContext,
"", /* URL is left empty because we're providing our own I/O */
nullptr /* AVInputFormat *fmt */,
nullptr /* AVDictionary **options */
);
if (result == 0) {
return true;
} else {
LOGE("Failed to open file. Error code %s", av_err2str(result));
return false;
}
}
bool FFMpegExtractor::getStreamInfo(AVFormatContext *avFormatContext) {
int result = avformat_find_stream_info(avFormatContext, nullptr);
if (result == 0 ){
return true;
} else {
LOGE("Failed to find stream info. Error code %s", av_err2str(result));
return false;
}
}
AVStream *FFMpegExtractor::getBestAudioStream(AVFormatContext *avFormatContext) {
int streamIndex = av_find_best_stream(avFormatContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
if (streamIndex < 0){
LOGE("Could not find stream");
return nullptr;
} else {
return avFormatContext->streams[streamIndex];
}
}
double FFMpegExtractor::getDuration(int fd) {
int returnValue = -1; // -1 indicates error
// Create a buffer for FFmpeg to use for decoding (freed in the custom deleter below)
auto buffer = reinterpret_cast<uint8_t*>(av_malloc(kInternalBufferSize));
{
FILE *tmp;
tmp = fdopen(fd, "rb");
fp.reset(move(tmp));
}
std::unique_ptr<AVIOContext, void(*)(AVIOContext *)> ioContext {
nullptr,
[](AVIOContext *c) {
if (c->buffer) av_free(c->buffer);
avio_context_free(&c);
}
};
{
AVIOContext *tmp = nullptr;
if (!createAVIOContext(buffer, kInternalBufferSize, &tmp)) {
LOGE("Could not create an AVIOContext");
return returnValue;
}
ioContext.reset(tmp);
}
std::unique_ptr<AVFormatContext, void(*)(AVFormatContext *)> formatContext {
nullptr,
[](AVFormatContext *f) {
if (f->iformat) {
avformat_free_context(f);
}
}
};
{
AVFormatContext *tmp;
if (!createAVFormatContext(ioContext.get(), &tmp)) return returnValue;
formatContext.reset(tmp);
}
if (!openAVFormatContext(formatContext.get())) return returnValue;
if (!getStreamInfo(formatContext.get())) return returnValue;
AVStream *stream = getBestAudioStream(formatContext.get());
if (stream == nullptr || stream->codecpar == nullptr){
LOGE("Could not find a suitable audio stream to decode");
return returnValue;
}
double divideFactor = (double) 1 / av_q2d(stream->time_base);
double durationOfAudio = (double) stream->duration / divideFactor;
return durationOfAudio;
}
int64_t FFMpegExtractor::decode(int fd, uint8_t* targetData, AudioProperties targetProperties) {
int returnValue = -1; // -1 indicates error
// Create a buffer for FFmpeg to use for decoding (freed in the custom deleter below)
auto buffer = reinterpret_cast<uint8_t*>(av_malloc(kInternalBufferSize));
{
FILE *tmp;
tmp = fdopen(fd, "rb");
fp.reset(move(tmp));
}
std::unique_ptr<AVIOContext, void(*)(AVIOContext *)> ioContext {
nullptr,
[](AVIOContext *c) {
if (c->buffer) av_free(c->buffer);
avio_context_free(&c);
}
};
{
AVIOContext *tmp = nullptr;
if (!createAVIOContext(buffer, kInternalBufferSize, &tmp)) {
LOGE("Could not create an AVIOContext");
return returnValue;
}
ioContext.reset(tmp);
}
std::unique_ptr<AVFormatContext, decltype(&avformat_free_context)> formatContext {
nullptr,
&avformat_free_context
};
{
AVFormatContext *tmp;
if (!createAVFormatContext(ioContext.get(), &tmp)) return returnValue;
formatContext.reset(tmp);
}
if (!openAVFormatContext(formatContext.get())) return returnValue;
if (!getStreamInfo(formatContext.get())) return returnValue;
AVStream *stream = getBestAudioStream(formatContext.get());
if (stream == nullptr || stream->codecpar == nullptr){
LOGE("Could not find a suitable audio stream to decode");
return returnValue;
}
printCodecParameters(stream->codecpar);
AVCodec *codec = avcodec_find_decoder(stream->codecpar->codec_id);
if (!codec){
LOGE("Could not find codec with ID: %d", stream->codecpar->codec_id);
return returnValue;
}
std::unique_ptr<AVCodecContext, void(*)(AVCodecContext *)> codecContext {
nullptr,
[](AVCodecContext *c) {
avcodec_free_context(&c);
}
};
{
AVCodecContext *tmp = avcodec_alloc_context3(codec);
if (!tmp){
LOGE("Failed to allocate codec context");
return returnValue;
}
codecContext.reset(tmp);
}
if (avcodec_parameters_to_context(codecContext.get(), stream->codecpar) < 0){
LOGE("Failed to copy codec parameters to codec context");
return returnValue;
}
// Open the codec
if (avcodec_open2(codecContext.get(), codec, nullptr) < 0){
LOGE("Could not open codec");
return returnValue;
}
// prepare resampler
int32_t outChannelLayout = (1 << targetProperties.channelCount) - 1;
LOGD("Channel layout %d", outChannelLayout);
SwrContext *swr = swr_alloc();
av_opt_set_int(swr, "in_channel_count", stream->codecpar->channels, 0);
av_opt_set_int(swr, "out_channel_count", targetProperties.channelCount, 0);
av_opt_set_int(swr, "in_channel_layout", stream->codecpar->channel_layout, 0);
av_opt_set_int(swr, "out_channel_layout", outChannelLayout, 0);
av_opt_set_int(swr, "in_sample_rate", stream->codecpar->sample_rate, 0);
av_opt_set_int(swr, "out_sample_rate", targetProperties.sampleRate, 0);
av_opt_set_int(swr, "in_sample_fmt", stream->codecpar->format, 0);
av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
av_opt_set_int(swr, "force_resampling", 1, 0);
// Check that resampler has been inited
int result = swr_init(swr);
if (result != 0){
LOGE("swr_init failed. Error: %s", av_err2str(result));
return returnValue;
};
if (!swr_is_initialized(swr)) {
LOGE("swr_is_initialized is false\n");
return returnValue;
}
// Prepare to read data
int64_t bytesWritten = 0;
AVPacket avPacket;
av_init_packet(&avPacket);
AVFrame *decodedFrame = av_frame_alloc(); // Stores raw audio data
int bytesPerSample = av_get_bytes_per_sample((AVSampleFormat)stream->codecpar->format);
LOGD("Bytes per sample %d", bytesPerSample);
LOGD("DECODE START");
// While there is more data to read, read it into the avPacket
while (av_read_frame(formatContext.get(), &avPacket) == 0){
if (avPacket.stream_index == stream->index && avPacket.size > 0) {
// Pass our compressed data into the codec
result = avcodec_send_packet(codecContext.get(), &avPacket);
if (result == AVERROR(EOF) || result == AVERROR(EINVAL)) {
LOGE("avcodec_send_packet aborting with unrecoverable error: %s", av_err2str(result));
goto cleanup;
} else if (result < 0) {
LOGI("avcodec_send_packet returned error: %s", av_err2str(result));
av_packet_unref(&avPacket);
continue;
}
// Retrieve our raw data from the codec
result = avcodec_receive_frame(codecContext.get(), decodedFrame);
if (result == AVERROR(EAGAIN)) {
// The codec needs more data before it can decode
LOGI("avcodec_receive_frame returned EAGAIN");
av_packet_unref(&avPacket);
continue;
} else if (result != 0) {
LOGE("avcodec_receive_frame error: %s", av_err2str(result));
goto cleanup;
}
// DO RESAMPLING
auto dst_nb_samples = (int32_t) av_rescale_rnd(
swr_get_delay(swr, decodedFrame->sample_rate) + decodedFrame->nb_samples,
targetProperties.sampleRate,
decodedFrame->sample_rate,
AV_ROUND_UP);
short *buffer1;
av_samples_alloc(
(uint8_t **) &buffer1,
nullptr,
targetProperties.channelCount,
dst_nb_samples,
AV_SAMPLE_FMT_FLT,
0);
int frame_count = swr_convert(
swr,
(uint8_t **) &buffer1,
dst_nb_samples,
(const uint8_t **) decodedFrame->data,
decodedFrame->nb_samples);
int64_t bytesToWrite = frame_count * sizeof(float) * targetProperties.channelCount;
memcpy(targetData + bytesWritten, buffer1, (size_t)bytesToWrite);
bytesWritten += bytesToWrite;
av_freep(&buffer1);
av_packet_unref(&avPacket);
}
}
av_frame_free(&decodedFrame);
LOGD("DECODE END");
cleanup:
if (bytesWritten > 0) {
returnValue = bytesWritten;
}
return returnValue;
}
void FFMpegExtractor::printCodecParameters(AVCodecParameters *params) {
LOGD("Stream properties");
LOGD("Channels: %d", params->channels);
LOGD("Channel layout: %" PRId64, params->channel_layout);
LOGD("Sample rate: %d", params->sample_rate);
LOGD("Format: %s", av_get_sample_fmt_name((AVSampleFormat)params->format));
LOGD("Frame size: %d", params->frame_size);
}