-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathffmpeg.cpp
393 lines (335 loc) · 12.6 KB
/
ffmpeg.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
Vrok - smokin' audio
(C) 2013 Madura A. released under GPL 2.0 All following copyrights
hold. This notice must be retained.
See LICENSE for details.
Based on,
http://stackoverflow.com/questions/9799560/decode-audio-using-libavcodec-and-play-using-libao
http://dranger.com/ffmpeg/tutorial05.html
http://dranger.com/ffmpeg/tutorial04.html
*/
#include "ffmpeg.h"
#include "buffer.h"
#include "metadata.h"
#include <cstdint>
#include <limits>
#define RET_ON_ERR(ret) \
if (ret < 0) { \
DBG(0, "die on " << __LINE__); \
return false; \
}
#define SHORTTOFL (1.0f / 32768.0f)
#define INT8TOFL (1.0f / 127.0f)
#define INT32TOFL (1.0f / 2147483647.0f)
#define INT64TOFL (1.0f / 9223372036854775807.0f)
#define SEEK_MAX 0xFFFFFFFFFFFFFFFFL
vrok::DecoderFFMPEG::DecoderFFMPEG() : ctx(nullptr), fmt_ctx(nullptr) {
static long s = 0;
if (s == 0) {
avformat_network_init();
// av_register_all();
}
s++;
fmt_ctx = avformat_alloc_context();
_ringbuffer = new Ringbuffer<real_t>(2 * FFMPEG_MAX_BUF_SIZE + 2 * AV_INPUT_BUFFER_PADDING_SIZE);
_done = false;
}
vrok::DecoderFFMPEG::~DecoderFFMPEG() {
Close();
delete _ringbuffer;
}
bool vrok::DecoderFFMPEG::Open(vrok::Resource *resource) {
fmt_ctx = nullptr;
ctx = nullptr;
_ringbuffer->Clear();
audio_stream_id = -1;
metadata_stream_id = -1;
if (avformat_open_input(&fmt_ctx, resource->_filename.c_str(), NULL, NULL) < 0) {
WARN(9, "Can't open file " << resource->_filename);
Close();
return false;
}
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
WARN(9, "Stream info load failed");
return false;
}
INFO("number of streams: " << fmt_ctx->nb_streams);
unsigned int i;
for (i = 0; i < fmt_ctx->nb_streams; i++) {
if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_stream_id = i;
} else if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
INFO("meta stream found!");
metadata_stream_id = i;
}
}
if (audio_stream_id == -1) {
WARN(9, "No audio stream");
Close();
return false;
}
av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
audio_st = fmt_ctx->streams[audio_stream_id];
// ctx=container->streams[audio_stream_id]->codec;
ctx = avcodec_alloc_context3(codec);
av_opt_set_int(ctx, "refcounted_frames", 0, 0);
// codec=avcodec_find_decoder(ctx->codec_id);
DBG(1, "codec: " << codec->long_name);
if (codec == NULL) {
WARN(9, "Cannot find codec");
Close();
return false;
}
if (avcodec_parameters_to_context(ctx, fmt_ctx->streams[audio_stream_id]->codecpar) != 0) {
// Something went wrong. Cleaning up...
avcodec_close(ctx);
avcodec_free_context(&ctx);
avformat_close_input(&fmt_ctx);
return false;
}
if (avcodec_open2(ctx, codec, NULL) < 0) {
WARN(9, "Codec cannot be opened");
Close();
return false;
}
sfmt = ctx->sample_fmt;
duration_in_seconds = fmt_ctx->duration / AV_TIME_BASE;
got_frame = 0;
temp_write = 0;
packet = av_packet_alloc();
frame = av_frame_alloc();
current_in_seconds = 0;
DBG(1, "opend");
_done = false;
_seek_req = false;
return true;
}
bool vrok::DecoderFFMPEG::GetBufferConfig(BufferConfig *config) {
config->channels = audio_st->codecpar->ch_layout.nb_channels;
config->samplerate = ctx->sample_rate;
DBG(1, "p " << config->channels);
DBG(1, "p " << config->samplerate);
return true;
}
bool vrok::DecoderFFMPEG::Close() {
if (ctx)
avcodec_close(ctx);
ctx = nullptr;
if (fmt_ctx) {
avformat_close_input(&fmt_ctx);
avformat_free_context(fmt_ctx);
}
fmt_ctx = nullptr;
return true;
}
/* Notes:
*
* Everyone forgets, the original author does too. So some notes about the
* routine follows,
*
* As every call should guarantee a filled buffer we first try to empty the
* ringbuffer, if the ringbuffer seems to be empty it'll be filled and a buffer
* will be taken from it.
*
* FFmpeg has audio and video packets and might even happen to have more types
* of packets we are only interested in audio packets for now, therefore we
* eat up other types of packets and only decode audio.
*
*/
bool vrok::DecoderFFMPEG::DecoderRun(Buffer *buffer, BufferConfig *config) {
/* when done drain the ring buffer */
if (_done) {
int used = _ringbuffer->Used();
int bufsize = config->channels * config->frames;
DBG(1, "used ringbuffer " << used);
if (used == 0) {
return false;
} else if (used < bufsize) {
_ringbuffer->Read(buffer->GetData(), used);
for (int i = used; i < bufsize; i++) {
buffer->GetData()[i] = 0.0;
}
return true;
} else {
_ringbuffer->Read(buffer->GetData(), bufsize);
return true;
}
}
if (_seek_req) {
DBG(1, "do seek");
int ret = avformat_seek_file(fmt_ctx, -1, INT64_MIN, _seek_to, INT64_MAX, 0);
if (ret < 0) {
WARN(0, "seek failed");
return false;
}
_seek_req = false;
}
while (!_ringbuffer->Read(buffer->GetData(), config->channels * config->frames)) {
int ret = 0, read_ok = 0, read_frame_done = 0;
got_frame = 0;
while (got_frame == 0) {
if (read_frame_done == 0) {
read_ok = av_read_frame(fmt_ctx, packet);
if (read_ok < 0) {
_done = true;
return true;
}
read_frame_done = packet->stream_index == audio_stream_id;
// eat up video packets!
while (packet->stream_index != audio_stream_id) {
av_packet_unref(packet);
ret = av_read_frame(fmt_ctx, packet);
if (ret < 0) {
_done = true;
return true;
}
}
if (fmt_ctx->event_flags == AVFMT_EVENT_FLAG_METADATA_UPDATED) {
AVDictionaryEntry *tag = nullptr;
Metadata *metadata = Metadata::Create();
while (tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)) {
metadata->SetMetadata(tag->key, tag->value);
}
_metadata.push_back(metadata);
fmt_ctx->event_flags = 0;
}
read_frame_done = 1;
}
if (read_frame_done == 1) {
ret = avcodec_send_packet(ctx, packet);
av_packet_unref(packet);
RET_ON_ERR(ret);
read_frame_done = 0;
}
ret = avcodec_receive_frame(ctx, frame);
got_frame = (ret == 0);
RET_ON_ERR(ret);
}
int nb_channels = audio_st->codecpar->ch_layout.nb_channels;
ret = av_samples_get_buffer_size(&plane_size, nb_channels, frame->nb_samples, ctx->sample_fmt, 1);
RET_ON_ERR(ret);
temp_write = 0;
if (got_frame) {
current_in_seconds = (audio_st->time_base.num * frame->pts) / audio_st->time_base.den *
audio_st->time_base.num;
switch (sfmt) {
case AV_SAMPLE_FMT_S16P:
for (size_t nb = 0; nb < plane_size / sizeof(int16_t); nb++) {
for (int ch = 0; ch < nb_channels; ch++) {
temp[temp_write] = ((short *)frame->extended_data[ch])[nb] * SHORTTOFL;
temp_write++;
}
}
break;
case AV_SAMPLE_FMT_S32P:
for (size_t nb = 0; nb < plane_size / sizeof(int32_t); nb++) {
for (int ch = 0; ch < nb_channels; ch++) {
temp[temp_write] = ((int32_t *)frame->extended_data[ch])[nb] * INT32TOFL;
temp_write++;
}
}
break;
case AV_SAMPLE_FMT_S64P:
for (size_t nb = 0; nb < plane_size / sizeof(int64_t); nb++) {
for (int ch = 0; ch < nb_channels; ch++) {
temp[temp_write] = ((int64_t *)frame->extended_data[ch])[nb] * INT64TOFL;
temp_write++;
}
}
break;
case AV_SAMPLE_FMT_FLTP:
for (size_t nb = 0; nb < plane_size / sizeof(float); nb++) {
for (int ch = 0; ch < nb_channels; ch++) {
temp[temp_write] = ((float *)frame->extended_data[ch])[nb];
temp_write++;
}
}
break;
case AV_SAMPLE_FMT_DBLP:
for (size_t nb = 0; nb < plane_size / sizeof(double); nb++) {
for (int ch = 0; ch < nb_channels; ch++) {
temp[temp_write] = ((double *)frame->extended_data[ch])[nb];
temp_write++;
}
}
break;
case AV_SAMPLE_FMT_S16:
for (size_t nb = 0; nb < plane_size / sizeof(short); nb++) {
temp[temp_write] = ((short *)frame->extended_data[0])[nb] * SHORTTOFL;
temp_write++;
}
break;
case AV_SAMPLE_FMT_S32:
for (size_t nb = 0; nb < plane_size / sizeof(int32_t); nb++) {
temp[temp_write] = ((int32_t *)frame->extended_data[0])[nb] * INT32TOFL;
temp_write++;
}
break;
case AV_SAMPLE_FMT_S64:
for (size_t nb = 0; nb < plane_size / sizeof(int64_t); nb++) {
temp[temp_write] = ((int64_t *)frame->extended_data[0])[nb] * INT64TOFL;
temp_write++;
}
break;
case AV_SAMPLE_FMT_FLT:
for (size_t nb = 0; nb < plane_size / sizeof(float); nb++) {
temp[temp_write] = ((float *)frame->extended_data[0])[nb];
temp_write++;
}
break;
case AV_SAMPLE_FMT_DBL:
for (size_t nb = 0; nb < plane_size / sizeof(double); nb++) {
temp[temp_write] = ((double *)frame->extended_data[0])[nb];
temp_write++;
}
break;
case AV_SAMPLE_FMT_U8P:
for (size_t nb = 0; nb < plane_size / sizeof(uint8_t); nb++) {
for (int ch = 0; ch < nb_channels; ch++) {
temp[temp_write] = ((((uint8_t *)frame->extended_data[ch])[nb] - 127)) * INT8TOFL;
temp_write++;
}
}
break;
case AV_SAMPLE_FMT_U8:
for (size_t nb = 0; nb < plane_size / sizeof(uint8_t); nb++) {
temp[temp_write] = ((((uint8_t *)frame->extended_data[0])[nb] - 127)) * INT8TOFL;
temp_write++;
}
break;
default: {
WARN(9, "PCM type not supported : " << av_get_sample_fmt_name(sfmt));
return false;
}
}
} else {
WARN(5, "frame failed");
return false;
}
if (!_ringbuffer->Write(temp, temp_write)) {
WARN(9, "Write buffer not enough!");
return false;
}
av_frame_unref(frame);
//av_packet_unref(packet);
}
return true;
}
uint64_t vrok::DecoderFFMPEG::GetDurationInSeconds() {
return duration_in_seconds;
}
uint64_t vrok::DecoderFFMPEG::GetPositionInSeconds() {
return current_in_seconds;
}
void vrok::DecoderFFMPEG::SetPositionInSeconds(uint64_t seconds) {
_seek_req = true;
_seek_to = (int64_t)AV_TIME_BASE * seconds;
}
vrok::Metadata *vrok::DecoderFFMPEG::PopMetadataEvent() {
if (!_metadata.empty()) {
Metadata *metadata = _metadata.front();
_metadata.pop_front();
return metadata;
}
return nullptr;
}