1 | /* |
---|
2 | Copyright (C) 2013 Paul Brossier <piem@aubio.org> |
---|
3 | |
---|
4 | This file is part of aubio. |
---|
5 | |
---|
6 | aubio is free software: you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation, either version 3 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | aubio is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | #include "config.h" |
---|
23 | |
---|
24 | #ifdef HAVE_AVCODEC |
---|
25 | |
---|
26 | #include <sndfile.h> |
---|
27 | #include <libavcodec/avcodec.h> |
---|
28 | #include <libavformat/avformat.h> |
---|
29 | #include <libavresample/avresample.h> |
---|
30 | #include <libavutil/opt.h> |
---|
31 | #include <stdlib.h> |
---|
32 | |
---|
33 | #include "aubio_priv.h" |
---|
34 | #include "fvec.h" |
---|
35 | #include "fmat.h" |
---|
36 | #include "source_avcodec.h" |
---|
37 | |
---|
38 | #define AUBIO_AVCODEC_MIN_BUFFER_SIZE FF_MIN_BUFFER_SIZE |
---|
39 | |
---|
40 | struct _aubio_source_avcodec_t { |
---|
41 | uint_t hop_size; |
---|
42 | uint_t samplerate; |
---|
43 | uint_t channels; |
---|
44 | |
---|
45 | // some data about the file |
---|
46 | char_t *path; |
---|
47 | uint_t input_samplerate; |
---|
48 | uint_t input_channels; |
---|
49 | |
---|
50 | // avcodec stuff |
---|
51 | AVFormatContext *avFormatCtx; |
---|
52 | AVCodecContext *avCodecCtx; |
---|
53 | AVFrame *avFrame; |
---|
54 | AVAudioResampleContext *avr; |
---|
55 | float *output; |
---|
56 | uint_t read_samples; |
---|
57 | uint_t read_index; |
---|
58 | sint_t selected_stream; |
---|
59 | uint_t eof; |
---|
60 | }; |
---|
61 | |
---|
62 | aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplerate, uint_t hop_size) { |
---|
63 | aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t); |
---|
64 | int err; |
---|
65 | |
---|
66 | if (path == NULL) { |
---|
67 | AUBIO_ERR("Aborted opening null path\n"); |
---|
68 | return NULL; |
---|
69 | } |
---|
70 | |
---|
71 | s->hop_size = hop_size; |
---|
72 | s->channels = 1; |
---|
73 | s->path = path; |
---|
74 | |
---|
75 | // register all formats and codecs |
---|
76 | av_register_all(); |
---|
77 | |
---|
78 | // if path[0] != '/' |
---|
79 | //avformat_network_init(); |
---|
80 | |
---|
81 | // try opening the file and get some info about it |
---|
82 | AVFormatContext *avFormatCtx = s->avFormatCtx; |
---|
83 | avFormatCtx = NULL; |
---|
84 | if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) { |
---|
85 | uint8_t errorstr_len = 128; |
---|
86 | char errorstr[errorstr_len]; |
---|
87 | if (av_strerror (err, errorstr, errorstr_len) == 0) { |
---|
88 | AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr); |
---|
89 | } else { |
---|
90 | AUBIO_ERR("Failed opening %s (unknown error)\n", s->path); |
---|
91 | } |
---|
92 | goto beach; |
---|
93 | } |
---|
94 | |
---|
95 | // try to make sure max_analyze_duration is big enough for most songs |
---|
96 | avFormatCtx->max_analyze_duration *= 100; |
---|
97 | |
---|
98 | // retrieve stream information |
---|
99 | if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) { |
---|
100 | uint8_t errorstr_len = 128; |
---|
101 | char errorstr[errorstr_len]; |
---|
102 | if (av_strerror (err, errorstr, errorstr_len) == 0) { |
---|
103 | AUBIO_ERR("Could not find stream information " |
---|
104 | "for %s (%s)\n", s->path, errorstr); |
---|
105 | } else { |
---|
106 | AUBIO_ERR("Could not find stream information " |
---|
107 | "for %s (unknown error)\n", s->path); |
---|
108 | } |
---|
109 | goto beach; |
---|
110 | } |
---|
111 | |
---|
112 | // dump information about file onto standard error |
---|
113 | //av_dump_format(avFormatCtx, 0, s->path, 0); |
---|
114 | |
---|
115 | // look for the first audio stream |
---|
116 | uint_t i; |
---|
117 | sint_t selected_stream = -1; |
---|
118 | for (i = 0; i < avFormatCtx->nb_streams; i++) { |
---|
119 | if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { |
---|
120 | if (selected_stream == -1) { |
---|
121 | selected_stream = i; |
---|
122 | } else { |
---|
123 | AUBIO_WRN("More than one audio stream in %s, " |
---|
124 | "taking the first one\n", s->path); |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | if (selected_stream == -1) { |
---|
129 | AUBIO_ERR("No audio stream in %s\n", s->path); |
---|
130 | goto beach; |
---|
131 | } |
---|
132 | //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path); |
---|
133 | s->selected_stream = selected_stream; |
---|
134 | |
---|
135 | AVCodecContext *avCodecCtx = s->avCodecCtx; |
---|
136 | avCodecCtx = avFormatCtx->streams[selected_stream]->codec; |
---|
137 | AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id); |
---|
138 | if (codec == NULL) { |
---|
139 | AUBIO_ERR("Could not find decoder for %s", s->path); |
---|
140 | goto beach; |
---|
141 | } |
---|
142 | |
---|
143 | if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) { |
---|
144 | uint8_t errorstr_len = 128; |
---|
145 | char errorstr[errorstr_len]; |
---|
146 | if (av_strerror (err, errorstr, errorstr_len) == 0) { |
---|
147 | AUBIO_ERR("Could not load codec for %s (%s)\n", s->path, errorstr); |
---|
148 | } else { |
---|
149 | AUBIO_ERR("Could not load codec for %s (unknown error)\n", s->path); |
---|
150 | } |
---|
151 | goto beach; |
---|
152 | } |
---|
153 | |
---|
154 | /* get input specs */ |
---|
155 | s->input_samplerate = avCodecCtx->sample_rate; |
---|
156 | s->input_channels = avCodecCtx->channels; |
---|
157 | //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate); |
---|
158 | //AUBIO_DBG("input_channels: %d\n", s->input_channels); |
---|
159 | |
---|
160 | if (samplerate == 0) { |
---|
161 | samplerate = s->input_samplerate; |
---|
162 | //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate); |
---|
163 | } |
---|
164 | s->samplerate = samplerate; |
---|
165 | |
---|
166 | int64_t input_layout = av_get_default_channel_layout(s->input_channels); |
---|
167 | int64_t mono_layout = av_get_default_channel_layout(1); |
---|
168 | |
---|
169 | AVAudioResampleContext *avr = s->avr; |
---|
170 | avr = avresample_alloc_context(); |
---|
171 | av_opt_set_int(avr, "in_channel_layout", input_layout, 0); |
---|
172 | av_opt_set_int(avr, "out_channel_layout", mono_layout, 0); |
---|
173 | av_opt_set_int(avr, "in_sample_rate", s->input_samplerate, 0); |
---|
174 | av_opt_set_int(avr, "out_sample_rate", s->samplerate, 0); |
---|
175 | av_opt_set_int(avr, "in_sample_fmt", avCodecCtx->sample_fmt, 0); |
---|
176 | av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_FLTP, 0); |
---|
177 | if ( ( err = avresample_open(avr) ) < 0) { |
---|
178 | uint8_t errorstr_len = 128; |
---|
179 | char errorstr[errorstr_len]; |
---|
180 | if (av_strerror (err, errorstr, errorstr_len) == 0) { |
---|
181 | AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n", |
---|
182 | s->path, errorstr); |
---|
183 | } else { |
---|
184 | AUBIO_ERR("Could not open AVAudioResampleContext for %s (unknown error)\n", |
---|
185 | s->path); |
---|
186 | } |
---|
187 | goto beach; |
---|
188 | } |
---|
189 | |
---|
190 | AVFrame *avFrame = s->avFrame; |
---|
191 | avFrame = avcodec_alloc_frame(); |
---|
192 | if (!avFrame) { |
---|
193 | AUBIO_ERR("Could not allocate frame for (%s)\n", s->path); |
---|
194 | } |
---|
195 | |
---|
196 | /* allocate output for avr */ |
---|
197 | s->output = (float *)av_malloc(AUBIO_AVCODEC_MIN_BUFFER_SIZE * sizeof(float)); |
---|
198 | |
---|
199 | s->read_samples = 0; |
---|
200 | s->read_index = 0; |
---|
201 | |
---|
202 | s->avFormatCtx = avFormatCtx; |
---|
203 | s->avCodecCtx = avCodecCtx; |
---|
204 | s->avFrame = avFrame; |
---|
205 | s->avr = avr; |
---|
206 | |
---|
207 | s->eof = 0; |
---|
208 | |
---|
209 | //av_log_set_level(AV_LOG_QUIET); |
---|
210 | |
---|
211 | return s; |
---|
212 | |
---|
213 | beach: |
---|
214 | AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n", |
---|
215 | s->path, s->samplerate, s->hop_size); |
---|
216 | del_aubio_source_avcodec(s); |
---|
217 | return NULL; |
---|
218 | } |
---|
219 | |
---|
220 | void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) { |
---|
221 | AVFormatContext *avFormatCtx = s->avFormatCtx; |
---|
222 | AVCodecContext *avCodecCtx = s->avCodecCtx; |
---|
223 | AVFrame *avFrame = s->avFrame; |
---|
224 | AVPacket avPacket; |
---|
225 | av_init_packet (&avPacket); |
---|
226 | AVAudioResampleContext *avr = s->avr; |
---|
227 | float *output = s->output; |
---|
228 | |
---|
229 | do |
---|
230 | { |
---|
231 | int err = av_read_frame (avFormatCtx, &avPacket); |
---|
232 | if (err != 0) { |
---|
233 | if (err == AVERROR_EOF) { |
---|
234 | s->eof = 1; |
---|
235 | *read_samples = 0; |
---|
236 | return; |
---|
237 | } |
---|
238 | uint8_t errorstr_len = 128; |
---|
239 | char errorstr[errorstr_len]; |
---|
240 | if (av_strerror (err, errorstr, errorstr_len) == 0) { |
---|
241 | AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr); |
---|
242 | } else { |
---|
243 | AUBIO_ERR("Could not read frame in %s (unknown error)\n", s->path); |
---|
244 | } |
---|
245 | *read_samples = 0; |
---|
246 | return; |
---|
247 | } |
---|
248 | } while (avPacket.stream_index != s->selected_stream); |
---|
249 | |
---|
250 | int got_frame = 0; |
---|
251 | int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket); |
---|
252 | |
---|
253 | if (len < 0) { |
---|
254 | av_free_packet(&avPacket); |
---|
255 | AUBIO_ERR("Error while decoding %s\n", s->path); |
---|
256 | return; |
---|
257 | } |
---|
258 | if (got_frame == 0) { |
---|
259 | av_free_packet(&avPacket); |
---|
260 | //AUBIO_ERR("Could not get frame for (%s)\n", s->path); |
---|
261 | *read_samples = 0; |
---|
262 | return; |
---|
263 | } /* else { |
---|
264 | int data_size = |
---|
265 | av_samples_get_buffer_size(NULL, |
---|
266 | avCodecCtx->channels, avFrame->nb_samples, |
---|
267 | avCodecCtx->sample_fmt, 1); |
---|
268 | AUBIO_WRN("Got data_size %d frame for (%s)\n", data_size, s->path); |
---|
269 | } */ |
---|
270 | |
---|
271 | int in_samples = avFrame->nb_samples; |
---|
272 | int in_plane_size = 0; //avFrame->linesize[0]; |
---|
273 | int out_plane_size = 0; //sizeof(float); //in_samples * sizeof(float); |
---|
274 | int max_out_samples = AUBIO_AVCODEC_MIN_BUFFER_SIZE; |
---|
275 | int out_samples = avresample_convert ( avr, |
---|
276 | (uint8_t **)&output, out_plane_size, max_out_samples, |
---|
277 | (uint8_t **)avFrame->data, in_plane_size, in_samples); |
---|
278 | if (out_samples < 0) { |
---|
279 | AUBIO_ERR("Could not convert frame (%s)\n", s->path); |
---|
280 | *read_samples = 0; |
---|
281 | } |
---|
282 | //AUBIO_ERR("Got in_plane_size %d frame for (%s)\n", in_plane_size, s->path); |
---|
283 | //AUBIO_WRN("Delay is %d for %s\n", avresample_get_delay(avr), s->path); |
---|
284 | //AUBIO_WRN("max_out_samples is %d for AUBIO_AVCODEC_MIN_BUFFER_SIZE %d\n", |
---|
285 | // max_out_samples, AUBIO_AVCODEC_MIN_BUFFER_SIZE); |
---|
286 | |
---|
287 | av_free_packet(&avPacket); |
---|
288 | s->avFormatCtx = avFormatCtx; |
---|
289 | s->avCodecCtx = avCodecCtx; |
---|
290 | s->avFrame = avFrame; |
---|
291 | s->avr = avr; |
---|
292 | s->output = output; |
---|
293 | |
---|
294 | *read_samples = out_samples; |
---|
295 | } |
---|
296 | |
---|
297 | void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){ |
---|
298 | uint_t i; |
---|
299 | uint_t end = 0; |
---|
300 | uint_t total_wrote = 0; |
---|
301 | while (total_wrote < s->hop_size) { |
---|
302 | end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote); |
---|
303 | for (i = 0; i < end; i++) { |
---|
304 | read_data->data[i + total_wrote] = s->output[i + s->read_index]; |
---|
305 | } |
---|
306 | total_wrote += end; |
---|
307 | if (total_wrote < s->hop_size) { |
---|
308 | uint_t avcodec_read = 0; |
---|
309 | aubio_source_avcodec_readframe(s, &avcodec_read); |
---|
310 | s->read_samples = avcodec_read; |
---|
311 | s->read_index = 0; |
---|
312 | if (s->eof) { |
---|
313 | break; |
---|
314 | } |
---|
315 | } else { |
---|
316 | s->read_index += end; |
---|
317 | } |
---|
318 | } |
---|
319 | if (total_wrote < s->hop_size) { |
---|
320 | for (i = end; i < s->hop_size; i++) { |
---|
321 | read_data->data[i] = 0.; |
---|
322 | } |
---|
323 | } |
---|
324 | *read = total_wrote; |
---|
325 | } |
---|
326 | |
---|
327 | void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){ |
---|
328 | //uint_t i,j, input_channels = s->input_channels; |
---|
329 | } |
---|
330 | |
---|
331 | uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) { |
---|
332 | return s->samplerate; |
---|
333 | } |
---|
334 | |
---|
335 | uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) { |
---|
336 | return s->input_channels; |
---|
337 | } |
---|
338 | |
---|
339 | uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) { |
---|
340 | //uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate); |
---|
341 | return 0; //sf_seek (s->handle, resampled_pos, SEEK_SET); |
---|
342 | } |
---|
343 | |
---|
344 | void del_aubio_source_avcodec(aubio_source_avcodec_t * s){ |
---|
345 | if (!s) return; |
---|
346 | if (s->output != NULL) { |
---|
347 | av_free(s->output); |
---|
348 | } |
---|
349 | if (s->avr != NULL) { |
---|
350 | avresample_close( s->avr ); |
---|
351 | av_free ( s->avr ); |
---|
352 | } |
---|
353 | s->avr = NULL; |
---|
354 | if (s->avFrame != NULL) { |
---|
355 | avcodec_free_frame( &(s->avFrame) ); |
---|
356 | } |
---|
357 | s->avFrame = NULL; |
---|
358 | if (s->avCodecCtx != NULL) { |
---|
359 | avcodec_close ( s->avCodecCtx ); |
---|
360 | } |
---|
361 | s->avCodecCtx = NULL; |
---|
362 | if (s->avFormatCtx != NULL) { |
---|
363 | avformat_close_input ( &(s->avFormatCtx) ); |
---|
364 | } |
---|
365 | s->avFrame = NULL; |
---|
366 | s->avFormatCtx = NULL; |
---|
367 | AUBIO_FREE(s); |
---|
368 | } |
---|
369 | |
---|
370 | #endif /* HAVE_SNDFILE */ |
---|