forked from skotopes/arfarius
/
avfile.cpp
257 lines (211 loc) · 6.69 KB
/
avfile.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
#include "avfile.h"
#include "avmutex.h"
#include "avexception.h"
#include "memring.h"
extern "C" {
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
}
static AVMutex ffmpeg_init_mutex;
static volatile bool ffmpeg_init = false;
AVFile::AVFile() :
AVObject(),
formatCtx(nullptr), codecCtx(nullptr), swrCtx(nullptr),
audioStream(-1), decoding(false), _position(0), _seek_to(-1)
{
ffmpeg_init_mutex.lock();
if (!ffmpeg_init) {
av_register_all();
avcodec_register_all();
ffmpeg_init = true;
}
ffmpeg_init_mutex.unlock();
}
AVFile::~AVFile()
{
close();
}
const char * AVFile::getName() {
return "AVFile";
}
void AVFile::setSamplerate(av_sample_rate_t samplerate)
{
AVObject::setSamplerate(samplerate);
_updateSWR();
}
void AVFile::setChannels(av_channels_t channels)
{
AVObject::setChannels(channels);
_updateSWR();
}
size_t AVFile::pull(av_sample_t */*buffer_ptr*/, size_t /*buffer_size*/)
{
return 0;
}
size_t AVFile::push(av_sample_t */*buffer_ptr*/, size_t /*buffer_size*/)
{
return 0;
}
void AVFile::open(const char *url)
{
if (formatCtx)
throw AVException("Programming error: i already did it");
if (avformat_open_input(&formatCtx, url, 0, 0) < 0)
throw AVException("Unable to open media");
if (avformat_find_stream_info(formatCtx, 0) < 0)
throw AVException("Unable to find streams in media");
AVCodec *codec;
audioStream = av_find_best_stream(formatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
if (audioStream < 0)
throw AVException("No audio stream found");
codecCtx = formatCtx->streams[audioStream]->codec;
if (avcodec_open2(codecCtx, codec, 0) < 0)
throw AVException("Could not open codec");
if (!codecCtx->channel_layout)
codecCtx->channel_layout = av_get_default_channel_layout(codecCtx->channels);
_updateSWR();
}
void AVFile::close()
{
if (codecCtx) {
avcodec_close(codecCtx);
codecCtx = 0;
}
if (formatCtx) {
avformat_close_input(&formatCtx);
}
if (swrCtx) {
swr_free(&swrCtx);
}
audioStream = -1;
}
float AVFile::getDurationInSeconds() {
return (float) formatCtx->duration / AV_TIME_BASE;
}
size_t AVFile::getDurationInSamples() {
return formatCtx->duration * _sample_rate / AV_TIME_BASE;
}
float AVFile::getPositionInSeconds() {
AVStream * s = formatCtx->streams[audioStream];
return (float) _position * s->time_base.num / s->time_base.den;
}
float AVFile::getPositionInPercents()
{
return getPositionInSeconds() / getDurationInSeconds();
}
size_t AVFile::getBitrate() {
return formatCtx->bit_rate;
}
size_t AVFile::getCodecBitrate() {
return codecCtx->bit_rate;
}
int AVFile::getCodecSamplerate() {
return codecCtx->sample_rate;
}
int AVFile::getCodecChannels() {
return codecCtx->channels;
}
void AVFile::seekToPercent(float p)
{
if (0. < p && p < 1.) {
AVStream * s = formatCtx->streams[audioStream];
_seek_to = av_rescale(p * formatCtx->duration, s->time_base.den, AV_TIME_BASE * s->time_base.num);
}
}
// Protected
void AVFile::decode()
{
AVFrame frame;
int got_frame;
AVPacket packet;
int packet_size;
uint8_t *packet_data;
av_init_packet(&packet);
uint8_t * shadow = reinterpret_cast<uint8_t*>(av_malloc(192000 * 4));
decoding = true;
while (av_read_frame(formatCtx, &packet) == 0) {
if (packet.stream_index == audioStream) {
// make shure that we will be able to free it later
packet_size = packet.size;
packet_data = packet.data;
// decode frames till packet contains data
while (packet.size > 0) {
avcodec_get_frame_defaults(&frame);
int len = avcodec_decode_audio4(codecCtx, &frame, &got_frame, &packet);
if (len < 0) {
break; // probably corrupted packet
}
packet.data += len;
packet.size -= len;
if (got_frame) {
got_frame = 0;
if (swrCtx) {
uint8_t *shadow_array[] = { shadow };
const uint8_t **input_array = (const uint8_t **)frame.extended_data;
// todo: check original code^ some nasty shit inside
int ret = swr_convert(swrCtx, shadow_array, 192000, input_array, frame.nb_samples);
if (ret > 0) {
_output->push(reinterpret_cast<float *>(shadow), ret * _channels);
}
} else {
_output->push(reinterpret_cast<float *>(frame.data[0]), frame.nb_samples * _channels);
}
// update position
if (frame.pts != AV_NOPTS_VALUE) {
_position = frame.pts;
} else if (packet.pts != AV_NOPTS_VALUE) {
_position = packet.pts;
} else {
_position = 0;
}
}
// hurry up, no time to decode one more frame
if (!decoding) {
break;
}
}
// restore original size and pointer
packet.size = packet_size;
packet.data = packet_data;
}
// free packet data, reuse structure
av_free_packet(&packet);
// complete decoding thread shutdown
if (!decoding) {
break;
}
if (_seek_to > -1) {
int flags = AVSEEK_FLAG_ANY;
if (_seek_to < _position)
flags = flags | AVSEEK_FLAG_BACKWARD;
av_seek_frame(formatCtx, audioStream, _seek_to, flags);
_seek_to = -1;
}
}
av_free(shadow);
}
void AVFile::cancelDecoding()
{
decoding = false;
}
void AVFile::_updateSWR()
{
if (swrCtx) {
swr_free(&swrCtx);
}
if (!_channels || !_sample_rate || !codecCtx)
return;
if (codecCtx->channel_layout != (uint64_t)av_get_default_channel_layout(_channels) ||
codecCtx->sample_fmt != AV_SAMPLE_FMT_FLT ||
codecCtx->sample_rate != (int)_sample_rate)
{
swrCtx = swr_alloc_set_opts(0, av_get_default_channel_layout(_channels), AV_SAMPLE_FMT_FLT, _sample_rate,
codecCtx->channel_layout, codecCtx->sample_fmt, codecCtx->sample_rate,
0, 0);
if (!swrCtx)
throw AVException("Unable to allocate swresample context");
swr_init(swrCtx);
}
}