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 | #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05) |
---|
41 | |
---|
42 | struct _aubio_source_avcodec_t { |
---|
43 | uint_t hop_size; |
---|
44 | uint_t samplerate; |
---|
45 | uint_t channels; |
---|
46 | |
---|
47 | // some data about the file |
---|
48 | char_t *path; |
---|
49 | uint_t input_samplerate; |
---|
50 | uint_t input_channels; |
---|
51 | |
---|
52 | // avcodec stuff |
---|
53 | AVFormatContext *avFormatCtx; |
---|
54 | AVCodecContext *avCodecCtx; |
---|
55 | AVFrame *avFrame; |
---|
56 | AVAudioResampleContext *avr; |
---|
57 | int16_t *output; |
---|
58 | uint_t read_samples; |
---|
59 | uint_t read_index; |
---|
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 | // try opening the file and get some info about it |
---|
79 | AVFormatContext *avFormatCtx = s->avFormatCtx; |
---|
80 | avFormatCtx = NULL; |
---|
81 | if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) { |
---|
82 | uint8_t errorstr_len = 128; |
---|
83 | char errorstr[errorstr_len]; |
---|
84 | if (av_strerror (err, errorstr, errorstr_len) == 0) { |
---|
85 | AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr); |
---|
86 | } else { |
---|
87 | AUBIO_ERR("Failed opening %s (unknown error)\n", s->path); |
---|
88 | } |
---|
89 | goto beach; |
---|
90 | } |
---|
91 | |
---|
92 | // try to make sure max_analyze_duration is big enough for most songs |
---|
93 | avFormatCtx->max_analyze_duration *= 100; |
---|
94 | |
---|
95 | // retrieve stream information |
---|
96 | if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) { |
---|
97 | uint8_t errorstr_len = 128; |
---|
98 | char errorstr[errorstr_len]; |
---|
99 | if (av_strerror (err, errorstr, errorstr_len) == 0) { |
---|
100 | AUBIO_ERR("Could not find stream information for %s (%s)\n", s->path, errorstr); |
---|
101 | } else { |
---|
102 | AUBIO_ERR("Could not find stream information for %s (unknown error)\n", s->path); |
---|
103 | } |
---|
104 | goto beach; |
---|
105 | } |
---|
106 | |
---|
107 | // Dump information about file onto standard error |
---|
108 | //av_dump_format(avFormatCtx, 0, s->path, 0); |
---|
109 | |
---|
110 | // look for the first audio stream, printing a warning if more than one is found |
---|
111 | uint_t i; |
---|
112 | sint_t selected_stream = -1; |
---|
113 | for (i = 0; i < avFormatCtx->nb_streams; i++) { |
---|
114 | if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { |
---|
115 | if (selected_stream == -1) { |
---|
116 | selected_stream = i; |
---|
117 | } else { |
---|
118 | AUBIO_WRN("More than one audio stream in %s, taking the first one\n", s->path); |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | if (selected_stream == -1) { |
---|
123 | AUBIO_ERR("No audio stream in %s\n", s->path); |
---|
124 | goto beach; |
---|
125 | } |
---|
126 | //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path); |
---|
127 | |
---|
128 | AVCodecContext *avCodecCtx = s->avCodecCtx; |
---|
129 | avCodecCtx = avFormatCtx->streams[selected_stream]->codec; |
---|
130 | AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id); |
---|
131 | if (codec == NULL) { |
---|
132 | AUBIO_ERR("Could not find decoder for %s", s->path); |
---|
133 | goto beach; |
---|
134 | } |
---|
135 | |
---|
136 | if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) { |
---|
137 | uint8_t errorstr_len = 128; |
---|
138 | char errorstr[errorstr_len]; |
---|
139 | if (av_strerror (err, errorstr, errorstr_len) == 0) { |
---|
140 | AUBIO_ERR("Could not load codec for %s (%s)\n", s->path, errorstr); |
---|
141 | } else { |
---|
142 | AUBIO_ERR("Could not load codec for %s (unknown error)\n", s->path); |
---|
143 | } |
---|
144 | goto beach; |
---|
145 | } |
---|
146 | |
---|
147 | /* get input specs */ |
---|
148 | s->input_samplerate = avCodecCtx->sample_rate; |
---|
149 | s->input_channels = avCodecCtx->channels; |
---|
150 | //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate); |
---|
151 | //AUBIO_DBG("input_channels: %d\n", s->input_channels); |
---|
152 | |
---|
153 | if (samplerate == 0) { |
---|
154 | samplerate = s->input_samplerate; |
---|
155 | //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate); |
---|
156 | } |
---|
157 | s->samplerate = samplerate; |
---|
158 | |
---|
159 | int64_t input_layout = av_get_default_channel_layout(s->input_channels); |
---|
160 | int64_t mono_layout = av_get_default_channel_layout(1); |
---|
161 | |
---|
162 | AVAudioResampleContext *avr = s->avr; |
---|
163 | avr = avresample_alloc_context(); |
---|
164 | av_opt_set_int(avr, "in_channel_layout", input_layout, 0); |
---|
165 | av_opt_set_int(avr, "out_channel_layout", mono_layout, 0); |
---|
166 | av_opt_set_int(avr, "in_sample_rate", s->input_samplerate, 0); |
---|
167 | av_opt_set_int(avr, "out_sample_rate", s->samplerate, 0); |
---|
168 | av_opt_set_int(avr, "in_sample_fmt", avCodecCtx->sample_fmt, 0); |
---|
169 | av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); |
---|
170 | if ( ( err = avresample_open(avr) ) < 0) { |
---|
171 | uint8_t errorstr_len = 128; |
---|
172 | char errorstr[errorstr_len]; |
---|
173 | if (av_strerror (err, errorstr, errorstr_len) == 0) { |
---|
174 | AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n", s->path, errorstr); |
---|
175 | } else { |
---|
176 | AUBIO_ERR("Could not open AVAudioResampleContext for %s (unknown error)\n", s->path); |
---|
177 | } |
---|
178 | goto beach; |
---|
179 | } |
---|
180 | |
---|
181 | AVFrame *avFrame = s->avFrame; |
---|
182 | avFrame = avcodec_alloc_frame(); |
---|
183 | if (!avFrame) { |
---|
184 | AUBIO_ERR("Could not allocate frame for (%s)\n", s->path); |
---|
185 | } |
---|
186 | |
---|
187 | /* allocate output for avr */ |
---|
188 | s->output = (int16_t *)av_malloc(AUBIO_AVCODEC_MIN_BUFFER_SIZE * sizeof(int16_t)); |
---|
189 | |
---|
190 | s->read_samples = 0; |
---|
191 | s->read_index = 0; |
---|
192 | |
---|
193 | s->avFormatCtx = avFormatCtx; |
---|
194 | s->avCodecCtx = avCodecCtx; |
---|
195 | s->avFrame = avFrame; |
---|
196 | s->avr = avr; |
---|
197 | |
---|
198 | //av_log_set_level(AV_LOG_QUIET); |
---|
199 | |
---|
200 | return s; |
---|
201 | |
---|
202 | beach: |
---|
203 | AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n", |
---|
204 | s->path, s->samplerate, s->hop_size); |
---|
205 | del_aubio_source_avcodec(s); |
---|
206 | return NULL; |
---|
207 | } |
---|
208 | |
---|
209 | void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) { |
---|
210 | AVFormatContext *avFormatCtx = s->avFormatCtx; |
---|
211 | AVCodecContext *avCodecCtx = s->avCodecCtx; |
---|
212 | AVFrame *avFrame = s->avFrame; |
---|
213 | AVPacket avPacket; |
---|
214 | av_init_packet (&avPacket); |
---|
215 | AVAudioResampleContext *avr = s->avr; |
---|
216 | int16_t *output = s->output; |
---|
217 | |
---|
218 | int err = av_read_frame (avFormatCtx, &avPacket); |
---|
219 | if (err != 0) { |
---|
220 | //AUBIO_ERR("Could not read frame for (%s)\n", s->path); |
---|
221 | *read_samples = 0; |
---|
222 | return; |
---|
223 | } |
---|
224 | |
---|
225 | int got_frame = 0; |
---|
226 | int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket); |
---|
227 | |
---|
228 | if (len < 0) { |
---|
229 | AUBIO_ERR("Error while decoding %s\n", s->path); |
---|
230 | return; |
---|
231 | } |
---|
232 | if (got_frame == 0) { |
---|
233 | AUBIO_ERR("Could not get frame for (%s)\n", s->path); |
---|
234 | } /* else { |
---|
235 | int data_size = |
---|
236 | av_samples_get_buffer_size(NULL, |
---|
237 | avCodecCtx->channels, avFrame->nb_samples, |
---|
238 | avCodecCtx->sample_fmt, 1); |
---|
239 | AUBIO_WRN("Got data_size %d frame for (%s)\n", data_size, s->path); |
---|
240 | } */ |
---|
241 | |
---|
242 | int in_samples = avFrame->nb_samples; |
---|
243 | int in_plane_size = 0; //avFrame->linesize[0]; |
---|
244 | int out_plane_size = 0; //sizeof(float); //in_samples * sizeof(float); |
---|
245 | int max_out_samples = AUBIO_AVCODEC_MIN_BUFFER_SIZE; |
---|
246 | if (avresample_convert ( avr, |
---|
247 | (uint8_t **)&output, out_plane_size, max_out_samples, |
---|
248 | (uint8_t **)avFrame->data, in_plane_size, in_samples) < 0) { |
---|
249 | AUBIO_ERR("Could not convert frame (%s)\n", s->path); |
---|
250 | } |
---|
251 | //AUBIO_ERR("Got in_plane_size %d frame for (%s)\n", in_plane_size, s->path); |
---|
252 | //AUBIO_WRN("Delay is %d for %s\n", avresample_get_delay(avr), s->path); |
---|
253 | //AUBIO_WRN("max_out_samples is %d for AUBIO_AVCODEC_MIN_BUFFER_SIZE %d\n", |
---|
254 | // max_out_samples, AUBIO_AVCODEC_MIN_BUFFER_SIZE); |
---|
255 | |
---|
256 | uint_t out_samples = avresample_available(avr) + (avresample_get_delay(avr) |
---|
257 | + in_samples) * s->samplerate / s->input_samplerate; |
---|
258 | //AUBIO_WRN("Converted %d to %d samples\n", in_samples, out_samples); |
---|
259 | //for (i = 0; i < out_samples; i ++) { |
---|
260 | // AUBIO_DBG("%f\n", SHORT_TO_FLOAT(output[i])); |
---|
261 | //} |
---|
262 | av_free_packet(&avPacket); |
---|
263 | s->avFormatCtx = avFormatCtx; |
---|
264 | s->avCodecCtx = avCodecCtx; |
---|
265 | s->avFrame = avFrame; |
---|
266 | s->avr = avr; |
---|
267 | s->output = output; |
---|
268 | |
---|
269 | *read_samples = out_samples; |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){ |
---|
274 | uint_t i; |
---|
275 | //AUBIO_DBG("entering 'do' with %d, %d\n", s->read_samples, s->read_index); |
---|
276 | if (s->read_samples < s->hop_size) { |
---|
277 | // write the end of the buffer to the beginning of read_data |
---|
278 | uint_t partial = s->read_samples; |
---|
279 | for (i = 0; i < partial; i++) { |
---|
280 | read_data->data[i] = SHORT_TO_FLOAT(s->output[i + s->read_index]); |
---|
281 | } |
---|
282 | // get more data |
---|
283 | uint_t avcodec_read = 0; |
---|
284 | aubio_source_avcodec_readframe(s, &avcodec_read); |
---|
285 | s->read_samples = avcodec_read; |
---|
286 | s->read_index = 0; |
---|
287 | // write the beginning of the buffer to the end of read_data |
---|
288 | uint_t end = MIN(s->hop_size, s->read_samples); |
---|
289 | if (avcodec_read == 0) { |
---|
290 | end = partial; |
---|
291 | } |
---|
292 | for (i = partial; i < end; i++) { |
---|
293 | read_data->data[i] = SHORT_TO_FLOAT(s->output[i - partial + s->read_index]); |
---|
294 | } |
---|
295 | if (end < s->hop_size) { |
---|
296 | for (i = end; i < s->hop_size; i++) { |
---|
297 | read_data->data[i] = 0.; |
---|
298 | } |
---|
299 | } |
---|
300 | s->read_index += end - partial; |
---|
301 | s->read_samples -= end - partial; |
---|
302 | *read = end; |
---|
303 | } else { |
---|
304 | for (i = 0; i < s->hop_size; i++) { |
---|
305 | read_data->data[i] = SHORT_TO_FLOAT(s->output[i + s->read_index]); |
---|
306 | } |
---|
307 | s->read_index += s->hop_size; |
---|
308 | s->read_samples -= s->hop_size; |
---|
309 | *read = s->hop_size; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){ |
---|
314 | //uint_t i,j, input_channels = s->input_channels; |
---|
315 | } |
---|
316 | |
---|
317 | uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) { |
---|
318 | return s->samplerate; |
---|
319 | } |
---|
320 | |
---|
321 | uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) { |
---|
322 | return s->input_channels; |
---|
323 | } |
---|
324 | |
---|
325 | uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) { |
---|
326 | //uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate); |
---|
327 | return 0; //sf_seek (s->handle, resampled_pos, SEEK_SET); |
---|
328 | } |
---|
329 | |
---|
330 | void del_aubio_source_avcodec(aubio_source_avcodec_t * s){ |
---|
331 | if (!s) return; |
---|
332 | if (s->output != NULL) { |
---|
333 | av_free(s->output); |
---|
334 | } |
---|
335 | if (s->avr != NULL) { |
---|
336 | avresample_close( s->avr ); |
---|
337 | av_free ( s->avr ); |
---|
338 | } |
---|
339 | s->avr = NULL; |
---|
340 | if (s->avFrame != NULL) { |
---|
341 | avcodec_free_frame( &(s->avFrame) ); |
---|
342 | } |
---|
343 | s->avFrame = NULL; |
---|
344 | if (s->avCodecCtx != NULL) { |
---|
345 | avcodec_close ( s->avCodecCtx ); |
---|
346 | } |
---|
347 | s->avCodecCtx = NULL; |
---|
348 | if (s->avFormatCtx != NULL) { |
---|
349 | avformat_close_input ( &(s->avFormatCtx) ); |
---|
350 | } |
---|
351 | s->avFrame = NULL; |
---|
352 | s->avFormatCtx = NULL; |
---|
353 | AUBIO_FREE(s); |
---|
354 | } |
---|
355 | |
---|
356 | #endif /* HAVE_SNDFILE */ |
---|