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
|
#include "libavaudioloader.h"
bool libav::Audioloader::setup(const std::string &filename){
av_register_all();
frame = avcodec_alloc_frame();
if (!frame)
{
std::cout << "Error allocating the frame" << std::endl;
return false;
}
formatContext = NULL;
if (avformat_open_input(&formatContext, filename.c_str(), NULL, NULL) != 0)
{
av_free(frame);
std::cout << "Error opening the file" << std::endl;
return false;
}
if (avformat_find_stream_info(formatContext, NULL) < 0)
{
av_free(frame);
avformat_close_input(&formatContext);
std::cout << "Error finding the stream info" << std::endl;
return false;
}
audioStream = NULL;
for (unsigned int i = 0; i < formatContext->nb_streams; ++i)
{
if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
audioStream = formatContext->streams[i];
break;
}
}
if (audioStream == NULL)
{
av_free(frame);
avformat_close_input(&formatContext);
std::cout << "Could not find any audio stream in the file" << std::endl;
return false;
}
codecContext = audioStream->codec;
codecContext->codec = avcodec_find_decoder(codecContext->codec_id);
if (codecContext->codec == NULL)
{
av_free(frame);
avformat_close_input(&formatContext);
std::cout << "Couldn't find a proper decoder" << std::endl;
return false;
}
else if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0)
{
av_free(frame);
avformat_close_input(&formatContext);
std::cout << "Couldn't open the context with the decoder" << std::endl;
return false;
}
av_dump_format(formatContext, 0, 0, false); //avformat.h line 1256
int samples = ((formatContext->duration + 5000)*codecContext->sample_rate)/AV_TIME_BASE;
std::cout << "This stream has " << codecContext->channels << " channels, a sample rate of " << codecContext->sample_rate << "Hz and "<<samples <<" samples" << std::endl;
std::cout << "The data is in format " <<codecContext->sample_fmt<< " (aka "<< av_get_sample_fmt_name(codecContext->sample_fmt) << ") "<<std::endl;
av_init_packet(&packet);
//sample_processed=0;
ready=true;
return true;
}
AVFrame* libav::Audioloader::get_frame() {
if (!ready) return nullptr;
int frameFinished = 0;
while (!frameFinished) {
int ret=av_read_frame(formatContext, &packet);
if (ret<0) {
std::cerr << "finished with code "<<ret <<(ret==AVERROR_EOF?" ,EOF":"")<<std::endl;
ready=false;
return nullptr;
}
if (packet.stream_index == audioStream->index)
{
//int bytes =
avcodec_decode_audio4(codecContext, frame, &frameFinished, &packet);
// Some frames rely on multiple packets, so we have to make sure the frame is finished before
// we can use it
}
// You *must* call av_free_packet() after each call to av_read_frame() or else you'll leak memory
av_free_packet(&packet);
}
return frame;
}
AVPacket* libav::Audioloader::get_packet() {
if (!ready) return nullptr;
int ret=av_read_frame(formatContext, &packet);
if (ret<0) {
std::cerr << "finished with code "<<ret <<(ret==AVERROR_EOF?" ,EOF":"")<<std::endl;
ready=false;
return nullptr;
}
//if (packet.stream_index == audioStream->index)
//{
//int bytes =
// avcodec_decode_audio4(codecContext, frame, &frameFinished, &packet);
// Some frames rely on multiple packets, so we have to make sure the frame is finished before
// we can use it
//}
// You *must* call av_free_packet() after each call to av_read_frame() or else you'll leak memory
//av_free_packet(&packet);?????
//}
return &packet;
}
/*
// Some codecs will cause frames to be buffered up in the decoding process. If the CODEC_CAP_DELAY flag
// is set, there can be buffered up frames that need to be flushed, so we'll do that
if (codecContext->codec->capabilities & CODEC_CAP_DELAY)
{
av_init_packet(&packet);
// Decode all the remaining frames in the buffer, until the end is reached
int frameFinished = 0;
int bytes = avcodec_decode_audio4(codecContext, frame, &frameFinished, &packet);
while (bytes >= 0 && frameFinished)
{
for (auto p: processors) {
p->process_frame(frame->data[0],frame->nb_samples);
}
mutex.lock();
progress=((double)sample_processed)/samples;
mutex.unlock();
}
}
cerr << "finished processing: "<<sample_processed << " samples of "<<samples<<", "<<((double)sample_processed*100)/samples<<"%"<< std::endl;
*/
uint16_t* libav::Audioloader::get_samples(int num){ //presumes 16bpc here
//std::cerr << "request "<<num<<" samples: "<<(ready?"ready":"not ready")<<std::endl;
//if(!ready) return nullptr;
//shuffle down samples
if (sample_start>0){
for (int i=0;i<sample_end-sample_start;i++){
for (int j=0;j<channels;j++) {
buffer[(i*channels)+j]=buffer[((sample_start+i)*channels)+j];
}
}
sample_start=sample_end-sample_start;
}
sample_end=sample_start;
while (sample_end<num) {
frame=get_frame();
if (frame) {
channels=av_frame_get_channels(frame); //will always reach here 1st
if (((sample_end+std::max(num,frame->nb_samples))*channels)>buffer.size()){
int m=buffer.size();
int s=((sample_end+std::max(num,frame->nb_samples))*channels);
buffer.reserve(s);
std::cerr << "audioloader reserved buffer to " << s << std::endl;
for (int i=m;i<s;i++) buffer.push_back(0);
}
for (int i=0;i<frame->nb_samples;i++) {
for (int j=0;j<channels;j++) {
//int frame->format
//format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames, enum AVSampleFormat for audio)
int ff=frame->format;
//uint64_t frame->channel_layout
//Channel layout of the audio data.
uint64_t fcl=frame->channel_layout;
//int frame->nb_extended_buf
//Number of elements in extended_buf.
int fnb=frame->nb_extended_buf;
//int frame->decode_error_flags
//decode error flags of the frame, set to a combination of FF_DECODE_ERROR_xxx flags if the decoder produced a frame, but there were errors during the decoding.
int fde=frame->decode_error_flags;
//uint16_t s=((uint16_t*) frame->buf[j]->data)[i];
uint16_t s=((uint16_t*) frame->buf[0]->data)[j*channels+i];
//which? must be determined by format or layout of the channels
//ALSO some kind of HEINOUS memory leak??
buffer[((sample_end+i)*frame->channels)+j]=s;
//buffer[(j*frame->channels)+(sample_end+i)]= ((uint16_t*) frame->buf[j]->data)[i]; ??planar?? nope
}
}
sample_end+=frame->nb_samples;
}
else {
for (int i=sample_end;i<num;i++){
for (int j=0;j<channels;j++) {
buffer[(channels*i)+j]=0;
}
}
sample_end=num;
}
//std::cerr<<"filling buffer to "<<((sample_end+frame->nb_samples)*frame->channels)<<std::endl;
//avcodec_free_frame(&frame);
}
if (sample_end>num) {
sample_start=num;
}
else {
sample_start=0;
}
return (uint16_t*)(&buffer[0]);
}
bool libav::Audioloader::close() {
av_free(frame);
avcodec_close(codecContext);
avformat_close_input(&formatContext);
return true;
}
|