-
Notifications
You must be signed in to change notification settings - Fork 1
/
avfile.cpp
277 lines (226 loc) · 7.54 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "avfile.h"
#include "avmutex.h"
#include "avexception.h"
#include "memring.h"
extern "C" {
#include <libavutil/opt.h>
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
}
#include <QDebug>
#define AVFILE_SHADOW_BUFFER_SIZE 192000
AVFile::AVFile() :
AVObject(),
formatCtx(nullptr), codecCtx(nullptr), swrCtx(nullptr),
audioStream(-1), decoding(false), _position(0), _seek_to(-1)
{
}
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, nullptr, nullptr) < 0)
throw AVException("Unable to open media");
if (avformat_find_stream_info(formatCtx, nullptr) < 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 = nullptr;
}
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 percent)
{
if (0. < percent && percent < 1. && formatCtx->duration > 0) {
AVStream * s = formatCtx->streams[audioStream];
_seek_to = av_rescale(percent * formatCtx->duration, s->time_base.den, AV_TIME_BASE * s->time_base.num);
}
}
void AVFile::seekToSecond(float second)
{
if (getDurationInSeconds() > 0) seekToPercent(second/getDurationInSeconds());
}
void AVFile::seekBackward(float seconds)
{
seekToSecond(getPositionInSeconds() - seconds);
}
void AVFile::seekForward(float seconds)
{
seekToSecond(getPositionInSeconds() + seconds);
}
// 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(AVFILE_SHADOW_BUFFER_SIZE * sizeof(float)));
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) {
memset(&frame, 0, sizeof(AVFrame));
av_frame_unref(&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) {
if (!frame.extended_data[0]) {
qDebug() << this << "decode(): frame extended data is empty";
break;
}
uint8_t* shadow_array[] = { shadow };
// todo: check original code^ some nasty shit inside
int ret = swr_convert(swrCtx, shadow_array, AVFILE_SHADOW_BUFFER_SIZE, const_cast<const uint8_t**>(frame.extended_data), 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_packet_unref(&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)
{
qDebug() << this << "_updateSWR():"
<< "out:" << av_get_default_channel_layout(_channels) << AV_SAMPLE_FMT_FLT << _sample_rate
<< "in:"<< codecCtx->channel_layout << codecCtx->sample_fmt << codecCtx->sample_rate;
swrCtx = swr_alloc_set_opts(nullptr, av_get_default_channel_layout(_channels), AV_SAMPLE_FMT_FLT, _sample_rate,
codecCtx->channel_layout, codecCtx->sample_fmt, codecCtx->sample_rate,
0, nullptr);
// av_opt_set_int(swrCtx, "resampler", SWR_ENGINE_SWR, 0);
av_opt_set_int(swrCtx, "resampler", SWR_ENGINE_SOXR, 0);
if (!swrCtx)
throw AVException("Unable to allocate swresample context");
swr_init(swrCtx);
}
}