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 | #include "aubio_priv.h" |
---|
22 | |
---|
23 | #ifdef HAVE_LIBAV |
---|
24 | |
---|
25 | #include <libavcodec/avcodec.h> |
---|
26 | #include <libavformat/avformat.h> |
---|
27 | #if defined(HAVE_SWRESAMPLE) |
---|
28 | #include <libswresample/swresample.h> |
---|
29 | #elif defined(HAVE_AVRESAMPLE) |
---|
30 | #include <libavresample/avresample.h> |
---|
31 | #endif |
---|
32 | #include <libavutil/opt.h> |
---|
33 | #include <stdlib.h> |
---|
34 | |
---|
35 | // determine whether we use libavformat from ffmpeg or from libav |
---|
36 | #define FFMPEG_LIBAVFORMAT (LIBAVFORMAT_VERSION_MICRO > 99 ) |
---|
37 | // max_analyze_duration2 was used from ffmpeg libavformat 55.43.100 through 57.2.100 |
---|
38 | #define FFMPEG_LIBAVFORMAT_MAX_DUR2 FFMPEG_LIBAVFORMAT && ( \ |
---|
39 | (LIBAVFORMAT_VERSION_MAJOR == 55 && LIBAVFORMAT_VERSION_MINOR >= 43) \ |
---|
40 | || (LIBAVFORMAT_VERSION_MAJOR == 56) \ |
---|
41 | || (LIBAVFORMAT_VERSION_MAJOR == 57 && LIBAVFORMAT_VERSION_MINOR < 2) \ |
---|
42 | ) |
---|
43 | |
---|
44 | // backward compatibility with libavcodec55 |
---|
45 | #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1) |
---|
46 | #warning "libavcodec55 is deprecated" |
---|
47 | #define HAVE_AUBIO_LIBAVCODEC_DEPRECATED 1 |
---|
48 | #define av_frame_alloc avcodec_alloc_frame |
---|
49 | #define av_frame_free avcodec_free_frame |
---|
50 | #define av_packet_unref av_free_packet |
---|
51 | #endif |
---|
52 | |
---|
53 | #include "aubio_priv.h" |
---|
54 | #include "fvec.h" |
---|
55 | #include "fmat.h" |
---|
56 | #include "source_avcodec.h" |
---|
57 | |
---|
58 | #define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE |
---|
59 | |
---|
60 | #ifdef HAVE_PTHREAD_H |
---|
61 | // Global mutex to make sure avcodec_open2 is not called simultaneously in a |
---|
62 | // multithreaded environment. Comment the following define if no lock required. |
---|
63 | #define HAVE_AUBIO_AVCODEC_MUTEX |
---|
64 | #include <pthread.h> |
---|
65 | pthread_mutex_t aubio_avcodec_mutex = PTHREAD_MUTEX_INITIALIZER; |
---|
66 | #endif /* HAVE_PTHREAD_H */ |
---|
67 | |
---|
68 | struct _aubio_source_avcodec_t { |
---|
69 | uint_t hop_size; |
---|
70 | uint_t samplerate; |
---|
71 | uint_t channels; |
---|
72 | |
---|
73 | // some data about the file |
---|
74 | char_t *path; |
---|
75 | uint_t input_samplerate; |
---|
76 | uint_t input_channels; |
---|
77 | |
---|
78 | // avcodec stuff |
---|
79 | AVFormatContext *avFormatCtx; |
---|
80 | AVCodecContext *avCodecCtx; |
---|
81 | AVFrame *avFrame; |
---|
82 | AVPacket avPacket; |
---|
83 | #ifdef HAVE_AVRESAMPLE |
---|
84 | AVAudioResampleContext *avr; |
---|
85 | #elif defined(HAVE_SWRESAMPLE) |
---|
86 | SwrContext *avr; |
---|
87 | #endif |
---|
88 | smpl_t *output; |
---|
89 | uint_t read_samples; |
---|
90 | uint_t read_index; |
---|
91 | sint_t selected_stream; |
---|
92 | uint_t eof; |
---|
93 | uint_t multi; |
---|
94 | uint_t has_network_url; |
---|
95 | }; |
---|
96 | |
---|
97 | // hack to create or re-create the context the first time _do or _do_multi is called |
---|
98 | void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi); |
---|
99 | void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples); |
---|
100 | |
---|
101 | uint_t aubio_source_avcodec_has_network_url(aubio_source_avcodec_t *s); |
---|
102 | |
---|
103 | uint_t aubio_source_avcodec_has_network_url(aubio_source_avcodec_t *s) { |
---|
104 | char proto[20], authorization[256], hostname[128], uripath[256]; |
---|
105 | int proto_size = 20, authorization_size = 256, hostname_size = 128, |
---|
106 | *port_ptr = 0, path_size = 256; |
---|
107 | av_url_split(proto, proto_size, authorization, authorization_size, hostname, |
---|
108 | hostname_size, port_ptr, uripath, path_size, s->path); |
---|
109 | s->has_network_url = 0; |
---|
110 | if (strlen(proto)) { |
---|
111 | s->has_network_url = 1; |
---|
112 | } |
---|
113 | return s->has_network_url; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | aubio_source_avcodec_t * new_aubio_source_avcodec(const char_t * path, uint_t samplerate, uint_t hop_size) { |
---|
118 | aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t); |
---|
119 | int err; |
---|
120 | if (path == NULL) { |
---|
121 | AUBIO_ERR("source_avcodec: Aborted opening null path\n"); |
---|
122 | goto beach; |
---|
123 | } |
---|
124 | if ((sint_t)samplerate < 0) { |
---|
125 | AUBIO_ERR("source_avcodec: Can not open %s with samplerate %d\n", path, samplerate); |
---|
126 | goto beach; |
---|
127 | } |
---|
128 | if ((sint_t)hop_size <= 0) { |
---|
129 | AUBIO_ERR("source_avcodec: Can not open %s with hop_size %d\n", path, hop_size); |
---|
130 | goto beach; |
---|
131 | } |
---|
132 | |
---|
133 | s->hop_size = hop_size; |
---|
134 | s->channels = 1; |
---|
135 | |
---|
136 | if (s->path) AUBIO_FREE(s->path); |
---|
137 | s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1); |
---|
138 | strncpy(s->path, path, strnlen(path, PATH_MAX) + 1); |
---|
139 | |
---|
140 | #ifdef HAVE_AUBIO_AVCODEC_MUTEX |
---|
141 | pthread_mutex_lock(&aubio_avcodec_mutex); |
---|
142 | #endif /* HAVE_AUBIO_AVCODEC_MUTEX */ |
---|
143 | |
---|
144 | // register all formats and codecs |
---|
145 | av_register_all(); |
---|
146 | |
---|
147 | if (aubio_source_avcodec_has_network_url(s)) { |
---|
148 | avformat_network_init(); |
---|
149 | } |
---|
150 | |
---|
151 | // try opening the file and get some info about it |
---|
152 | AVFormatContext *avFormatCtx = s->avFormatCtx; |
---|
153 | AVDictionary *streamopts = 0; |
---|
154 | if (s->has_network_url) { |
---|
155 | if (av_dict_set(&streamopts, "timeout", "1000000", 0)) { // in microseconds |
---|
156 | AUBIO_WRN("source_avcodec: Failed setting timeout to 1000000 for %s\n", s->path); |
---|
157 | } else { |
---|
158 | AUBIO_WRN("source_avcodec: Setting timeout to 1000000 for %s\n", s->path); |
---|
159 | } |
---|
160 | } |
---|
161 | avFormatCtx = NULL; |
---|
162 | if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, &streamopts) ) < 0 ) { |
---|
163 | char errorstr[256]; |
---|
164 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
165 | AUBIO_ERR("source_avcodec: Failed opening %s (%s)\n", s->path, errorstr); |
---|
166 | goto beach_streamopts; |
---|
167 | } |
---|
168 | |
---|
169 | // try to make sure max_analyze_duration is big enough for most songs |
---|
170 | #if FFMPEG_LIBAVFORMAT_MAX_DUR2 |
---|
171 | avFormatCtx->max_analyze_duration2 *= 100; |
---|
172 | #else |
---|
173 | avFormatCtx->max_analyze_duration *= 100; |
---|
174 | #endif |
---|
175 | |
---|
176 | // retrieve stream information |
---|
177 | if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) { |
---|
178 | char errorstr[256]; |
---|
179 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
180 | AUBIO_ERR("source_avcodec: Could not find stream information " "for %s (%s)\n", s->path, |
---|
181 | errorstr); |
---|
182 | goto beach; |
---|
183 | } |
---|
184 | |
---|
185 | // dump information about file onto standard error |
---|
186 | //av_dump_format(avFormatCtx, 0, s->path, 0); |
---|
187 | |
---|
188 | // look for the first audio stream |
---|
189 | uint_t i; |
---|
190 | sint_t selected_stream = -1; |
---|
191 | for (i = 0; i < avFormatCtx->nb_streams; i++) { |
---|
192 | #if FF_API_LAVF_AVCTX |
---|
193 | if (avFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
---|
194 | #else |
---|
195 | if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { |
---|
196 | #endif |
---|
197 | if (selected_stream == -1) { |
---|
198 | selected_stream = i; |
---|
199 | } else { |
---|
200 | AUBIO_WRN("source_avcodec: More than one audio stream in %s, " |
---|
201 | "taking the first one\n", s->path); |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | if (selected_stream == -1) { |
---|
206 | AUBIO_ERR("source_avcodec: No audio stream in %s\n", s->path); |
---|
207 | goto beach; |
---|
208 | } |
---|
209 | //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path); |
---|
210 | s->selected_stream = selected_stream; |
---|
211 | |
---|
212 | AVCodecContext *avCodecCtx = s->avCodecCtx; |
---|
213 | #if FF_API_LAVF_AVCTX |
---|
214 | AVCodecParameters *codecpar = avFormatCtx->streams[selected_stream]->codecpar; |
---|
215 | if (codecpar == NULL) { |
---|
216 | AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path); |
---|
217 | goto beach; |
---|
218 | } |
---|
219 | AVCodec *codec = avcodec_find_decoder(codecpar->codec_id); |
---|
220 | |
---|
221 | /* Allocate a codec context for the decoder */ |
---|
222 | avCodecCtx = avcodec_alloc_context3(codec); |
---|
223 | if (!avCodecCtx) { |
---|
224 | AUBIO_ERR("source_avcodec: Failed to allocate the %s codec context for path %s\n", |
---|
225 | av_get_media_type_string(AVMEDIA_TYPE_AUDIO), s->path); |
---|
226 | goto beach; |
---|
227 | } |
---|
228 | #else |
---|
229 | avCodecCtx = avFormatCtx->streams[selected_stream]->codec; |
---|
230 | AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id); |
---|
231 | #endif |
---|
232 | if (codec == NULL) { |
---|
233 | AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path); |
---|
234 | goto beach; |
---|
235 | } |
---|
236 | |
---|
237 | #if FF_API_LAVF_AVCTX |
---|
238 | /* Copy codec parameters from input stream to output codec context */ |
---|
239 | if ((err = avcodec_parameters_to_context(avCodecCtx, codecpar)) < 0) { |
---|
240 | AUBIO_ERR("source_avcodec: Failed to copy %s codec parameters to decoder context for %s\n", |
---|
241 | av_get_media_type_string(AVMEDIA_TYPE_AUDIO), s->path); |
---|
242 | goto beach; |
---|
243 | } |
---|
244 | #endif |
---|
245 | |
---|
246 | if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) { |
---|
247 | char errorstr[256]; |
---|
248 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
249 | AUBIO_ERR("source_avcodec: Could not load codec for %s (%s)\n", s->path, errorstr); |
---|
250 | goto beach; |
---|
251 | } |
---|
252 | |
---|
253 | #ifdef HAVE_AUBIO_AVCODEC_MUTEX |
---|
254 | pthread_mutex_unlock(&aubio_avcodec_mutex); |
---|
255 | #endif /* HAVE_AUBIO_AVCODEC_MUTEX */ |
---|
256 | |
---|
257 | /* get input specs */ |
---|
258 | s->input_samplerate = avCodecCtx->sample_rate; |
---|
259 | s->input_channels = avCodecCtx->channels; |
---|
260 | //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate); |
---|
261 | //AUBIO_DBG("input_channels: %d\n", s->input_channels); |
---|
262 | |
---|
263 | if (samplerate == 0) { |
---|
264 | s->samplerate = s->input_samplerate; |
---|
265 | } else { |
---|
266 | s->samplerate = samplerate; |
---|
267 | } |
---|
268 | |
---|
269 | if (s->samplerate > s->input_samplerate) { |
---|
270 | AUBIO_WRN("source_avcodec: upsampling %s from %d to %d\n", s->path, |
---|
271 | s->input_samplerate, s->samplerate); |
---|
272 | } |
---|
273 | |
---|
274 | AVFrame *avFrame = s->avFrame; |
---|
275 | avFrame = av_frame_alloc(); |
---|
276 | if (!avFrame) { |
---|
277 | AUBIO_ERR("source_avcodec: Could not allocate frame for (%s)\n", s->path); |
---|
278 | } |
---|
279 | |
---|
280 | /* allocate output for avr */ |
---|
281 | s->output = (smpl_t *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(smpl_t)); |
---|
282 | |
---|
283 | s->read_samples = 0; |
---|
284 | s->read_index = 0; |
---|
285 | |
---|
286 | s->avFormatCtx = avFormatCtx; |
---|
287 | s->avCodecCtx = avCodecCtx; |
---|
288 | s->avFrame = avFrame; |
---|
289 | |
---|
290 | // default to mono output |
---|
291 | aubio_source_avcodec_reset_resampler(s, 0); |
---|
292 | |
---|
293 | s->eof = 0; |
---|
294 | s->multi = 0; |
---|
295 | |
---|
296 | //av_log_set_level(AV_LOG_QUIET); |
---|
297 | |
---|
298 | av_dict_free(&streamopts); |
---|
299 | return s; |
---|
300 | |
---|
301 | beach_streamopts: |
---|
302 | av_dict_free(&streamopts); |
---|
303 | beach: |
---|
304 | //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n", |
---|
305 | // s->path, s->samplerate, s->hop_size); |
---|
306 | #ifdef HAVE_AUBIO_AVCODEC_MUTEX |
---|
307 | pthread_mutex_unlock(&aubio_avcodec_mutex); |
---|
308 | #endif /* HAVE_AUBIO_AVCODEC_MUTEX */ |
---|
309 | del_aubio_source_avcodec(s); |
---|
310 | return NULL; |
---|
311 | } |
---|
312 | |
---|
313 | void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi) { |
---|
314 | // create or reset resampler to/from mono/multi-channel |
---|
315 | if ( (multi != s->multi) || (s->avr == NULL) ) { |
---|
316 | int64_t input_layout = av_get_default_channel_layout(s->input_channels); |
---|
317 | uint_t output_channels = multi ? s->input_channels : 1; |
---|
318 | int64_t output_layout = av_get_default_channel_layout(output_channels); |
---|
319 | #ifdef HAVE_AVRESAMPLE |
---|
320 | AVAudioResampleContext *avr = avresample_alloc_context(); |
---|
321 | AVAudioResampleContext *oldavr = s->avr; |
---|
322 | #elif defined(HAVE_SWRESAMPLE) |
---|
323 | SwrContext *avr = swr_alloc(); |
---|
324 | SwrContext *oldavr = s->avr; |
---|
325 | #endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */ |
---|
326 | |
---|
327 | av_opt_set_int(avr, "in_channel_layout", input_layout, 0); |
---|
328 | av_opt_set_int(avr, "out_channel_layout", output_layout, 0); |
---|
329 | av_opt_set_int(avr, "in_sample_rate", s->input_samplerate, 0); |
---|
330 | av_opt_set_int(avr, "out_sample_rate", s->samplerate, 0); |
---|
331 | av_opt_set_int(avr, "in_sample_fmt", s->avCodecCtx->sample_fmt, 0); |
---|
332 | #if HAVE_AUBIO_DOUBLE |
---|
333 | av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_DBL, 0); |
---|
334 | #else |
---|
335 | av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0); |
---|
336 | #endif |
---|
337 | // TODO: use planar? |
---|
338 | //av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_FLTP, 0); |
---|
339 | int err; |
---|
340 | #ifdef HAVE_AVRESAMPLE |
---|
341 | if ( ( err = avresample_open(avr) ) < 0) { |
---|
342 | #elif defined(HAVE_SWRESAMPLE) |
---|
343 | if ( ( err = swr_init(avr) ) < 0) { |
---|
344 | #endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */ |
---|
345 | char errorstr[256]; |
---|
346 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
347 | AUBIO_ERR("source_avcodec: Could not open AVAudioResampleContext for %s (%s)\n", |
---|
348 | s->path, errorstr); |
---|
349 | //goto beach; |
---|
350 | return; |
---|
351 | } |
---|
352 | s->avr = avr; |
---|
353 | if (oldavr != NULL) { |
---|
354 | #ifdef HAVE_AVRESAMPLE |
---|
355 | avresample_close( oldavr ); |
---|
356 | #elif defined(HAVE_SWRESAMPLE) |
---|
357 | swr_close ( oldavr ); |
---|
358 | #endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */ |
---|
359 | av_free ( oldavr ); |
---|
360 | oldavr = NULL; |
---|
361 | } |
---|
362 | s->multi = multi; |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) { |
---|
367 | AVFormatContext *avFormatCtx = s->avFormatCtx; |
---|
368 | AVCodecContext *avCodecCtx = s->avCodecCtx; |
---|
369 | AVFrame *avFrame = s->avFrame; |
---|
370 | AVPacket avPacket = s->avPacket; |
---|
371 | av_init_packet (&avPacket); |
---|
372 | #ifdef HAVE_AVRESAMPLE |
---|
373 | AVAudioResampleContext *avr = s->avr; |
---|
374 | #elif defined(HAVE_SWRESAMPLE) |
---|
375 | SwrContext *avr = s->avr; |
---|
376 | #endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */ |
---|
377 | smpl_t *output = s->output; |
---|
378 | *read_samples = 0; |
---|
379 | |
---|
380 | do |
---|
381 | { |
---|
382 | int err = av_read_frame (avFormatCtx, &avPacket); |
---|
383 | if (err == AVERROR_EOF) { |
---|
384 | s->eof = 1; |
---|
385 | goto beach; |
---|
386 | } |
---|
387 | if (err != 0) { |
---|
388 | char errorstr[256]; |
---|
389 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
390 | AUBIO_ERR("source_avcodec: could not read frame in %s (%s)\n", s->path, errorstr); |
---|
391 | goto beach; |
---|
392 | } |
---|
393 | } while (avPacket.stream_index != s->selected_stream); |
---|
394 | |
---|
395 | int got_frame = 0; |
---|
396 | #if FF_API_LAVF_AVCTX |
---|
397 | int ret = avcodec_send_packet(avCodecCtx, &avPacket); |
---|
398 | if (ret < 0 && ret != AVERROR_EOF) { |
---|
399 | AUBIO_ERR("source_avcodec: error when sending packet for %s\n", s->path); |
---|
400 | goto beach; |
---|
401 | } |
---|
402 | ret = avcodec_receive_frame(avCodecCtx, avFrame); |
---|
403 | if (ret >= 0) { |
---|
404 | got_frame = 1; |
---|
405 | } |
---|
406 | if (ret < 0) { |
---|
407 | if (ret == AVERROR(EAGAIN)) { |
---|
408 | AUBIO_WRN("source_avcodec: output is not available right now - user must try to send new input\n"); |
---|
409 | } else if (ret == AVERROR_EOF) { |
---|
410 | AUBIO_WRN("source_avcodec: the decoder has been fully flushed, and there will be no more output frames\n"); |
---|
411 | } else { |
---|
412 | AUBIO_ERR("source_avcodec: decoding errors on %s\n", s->path); |
---|
413 | goto beach; |
---|
414 | } |
---|
415 | } |
---|
416 | #else |
---|
417 | int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket); |
---|
418 | |
---|
419 | if (len < 0) { |
---|
420 | AUBIO_ERR("source_avcodec: error while decoding %s\n", s->path); |
---|
421 | goto beach; |
---|
422 | } |
---|
423 | #endif |
---|
424 | if (got_frame == 0) { |
---|
425 | AUBIO_WRN("source_avcodec: did not get a frame when reading %s\n", s->path); |
---|
426 | goto beach; |
---|
427 | } |
---|
428 | |
---|
429 | #ifdef HAVE_AVRESAMPLE |
---|
430 | int in_linesize = 0; |
---|
431 | av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels, |
---|
432 | avFrame->nb_samples, avCodecCtx->sample_fmt, 1); |
---|
433 | int in_samples = avFrame->nb_samples; |
---|
434 | int out_linesize = 0; |
---|
435 | int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE; |
---|
436 | int out_samples = avresample_convert ( avr, |
---|
437 | (uint8_t **)&output, out_linesize, max_out_samples, |
---|
438 | (uint8_t **)avFrame->data, in_linesize, in_samples); |
---|
439 | #elif defined(HAVE_SWRESAMPLE) |
---|
440 | int in_samples = avFrame->nb_samples; |
---|
441 | int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE / avCodecCtx->channels; |
---|
442 | int out_samples = swr_convert( avr, |
---|
443 | (uint8_t **)&output, max_out_samples, |
---|
444 | (const uint8_t **)avFrame->data, in_samples); |
---|
445 | #endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */ |
---|
446 | if (out_samples <= 0) { |
---|
447 | AUBIO_WRN("source_avcodec: no sample found while converting frame (%s)\n", s->path); |
---|
448 | goto beach; |
---|
449 | } |
---|
450 | |
---|
451 | *read_samples = out_samples; |
---|
452 | |
---|
453 | beach: |
---|
454 | s->avFormatCtx = avFormatCtx; |
---|
455 | s->avCodecCtx = avCodecCtx; |
---|
456 | s->avFrame = avFrame; |
---|
457 | #if defined(HAVE_AVRESAMPLE) || defined(HAVE_SWRESAMPLE) |
---|
458 | s->avr = avr; |
---|
459 | #endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */ |
---|
460 | s->output = output; |
---|
461 | |
---|
462 | av_packet_unref(&avPacket); |
---|
463 | } |
---|
464 | |
---|
465 | void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){ |
---|
466 | uint_t i; |
---|
467 | uint_t end = 0; |
---|
468 | uint_t total_wrote = 0; |
---|
469 | // switch from multi |
---|
470 | if (s->multi == 1) aubio_source_avcodec_reset_resampler(s, 0); |
---|
471 | while (total_wrote < s->hop_size) { |
---|
472 | end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote); |
---|
473 | for (i = 0; i < end; i++) { |
---|
474 | read_data->data[i + total_wrote] = s->output[i + s->read_index]; |
---|
475 | } |
---|
476 | total_wrote += end; |
---|
477 | if (total_wrote < s->hop_size) { |
---|
478 | uint_t avcodec_read = 0; |
---|
479 | aubio_source_avcodec_readframe(s, &avcodec_read); |
---|
480 | s->read_samples = avcodec_read; |
---|
481 | s->read_index = 0; |
---|
482 | if (s->eof) { |
---|
483 | break; |
---|
484 | } |
---|
485 | } else { |
---|
486 | s->read_index += end; |
---|
487 | } |
---|
488 | } |
---|
489 | if (total_wrote < s->hop_size) { |
---|
490 | for (i = total_wrote; i < s->hop_size; i++) { |
---|
491 | read_data->data[i] = 0.; |
---|
492 | } |
---|
493 | } |
---|
494 | *read = total_wrote; |
---|
495 | } |
---|
496 | |
---|
497 | void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){ |
---|
498 | uint_t i,j; |
---|
499 | uint_t end = 0; |
---|
500 | uint_t total_wrote = 0; |
---|
501 | // switch from mono |
---|
502 | if (s->multi == 0) aubio_source_avcodec_reset_resampler(s, 1); |
---|
503 | while (total_wrote < s->hop_size) { |
---|
504 | end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote); |
---|
505 | for (j = 0; j < read_data->height; j++) { |
---|
506 | for (i = 0; i < end; i++) { |
---|
507 | read_data->data[j][i + total_wrote] = |
---|
508 | s->output[(i + s->read_index) * s->input_channels + j]; |
---|
509 | } |
---|
510 | } |
---|
511 | total_wrote += end; |
---|
512 | if (total_wrote < s->hop_size) { |
---|
513 | uint_t avcodec_read = 0; |
---|
514 | aubio_source_avcodec_readframe(s, &avcodec_read); |
---|
515 | s->read_samples = avcodec_read; |
---|
516 | s->read_index = 0; |
---|
517 | if (s->eof) { |
---|
518 | break; |
---|
519 | } |
---|
520 | } else { |
---|
521 | s->read_index += end; |
---|
522 | } |
---|
523 | } |
---|
524 | if (total_wrote < s->hop_size) { |
---|
525 | for (j = 0; j < read_data->height; j++) { |
---|
526 | for (i = total_wrote; i < s->hop_size; i++) { |
---|
527 | read_data->data[j][i] = 0.; |
---|
528 | } |
---|
529 | } |
---|
530 | } |
---|
531 | *read = total_wrote; |
---|
532 | } |
---|
533 | |
---|
534 | uint_t aubio_source_avcodec_get_samplerate(const aubio_source_avcodec_t * s) { |
---|
535 | return s->samplerate; |
---|
536 | } |
---|
537 | |
---|
538 | uint_t aubio_source_avcodec_get_channels(const aubio_source_avcodec_t * s) { |
---|
539 | return s->input_channels; |
---|
540 | } |
---|
541 | |
---|
542 | uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) { |
---|
543 | int64_t resampled_pos = (uint_t)ROUND(pos * (s->input_samplerate * 1. / s->samplerate)); |
---|
544 | int64_t min_ts = MAX(resampled_pos - 2000, 0); |
---|
545 | int64_t max_ts = MIN(resampled_pos + 2000, INT64_MAX); |
---|
546 | int seek_flags = AVSEEK_FLAG_FRAME | AVSEEK_FLAG_ANY; |
---|
547 | int ret = AUBIO_FAIL; |
---|
548 | if (s->avFormatCtx != NULL && s->avr != NULL) { |
---|
549 | ret = AUBIO_OK; |
---|
550 | } else { |
---|
551 | AUBIO_ERR("source_avcodec: failed seeking in %s (file not opened?)", s->path); |
---|
552 | return ret; |
---|
553 | } |
---|
554 | if ((sint_t)pos < 0) { |
---|
555 | AUBIO_ERR("source_avcodec: could not seek %s at %d (seeking position" |
---|
556 | " should be >= 0)\n", s->path, pos); |
---|
557 | return AUBIO_FAIL; |
---|
558 | } |
---|
559 | ret = avformat_seek_file(s->avFormatCtx, s->selected_stream, |
---|
560 | min_ts, resampled_pos, max_ts, seek_flags); |
---|
561 | if (ret < 0) { |
---|
562 | AUBIO_ERR("source_avcodec: failed seeking to %d in file %s", pos, s->path); |
---|
563 | } |
---|
564 | // reset read status |
---|
565 | s->eof = 0; |
---|
566 | s->read_index = 0; |
---|
567 | s->read_samples = 0; |
---|
568 | #ifdef HAVE_AVRESAMPLE |
---|
569 | // reset the AVAudioResampleContext |
---|
570 | avresample_close(s->avr); |
---|
571 | avresample_open(s->avr); |
---|
572 | #elif defined(HAVE_SWRESAMPLE) |
---|
573 | swr_close(s->avr); |
---|
574 | swr_init(s->avr); |
---|
575 | #endif |
---|
576 | return ret; |
---|
577 | } |
---|
578 | |
---|
579 | uint_t aubio_source_avcodec_get_duration (aubio_source_avcodec_t * s) { |
---|
580 | if (s && &(s->avFormatCtx) != NULL) { |
---|
581 | int64_t duration = s->avFormatCtx->duration; |
---|
582 | return s->samplerate * ((uint_t)duration / 1e6 ); |
---|
583 | } |
---|
584 | return 0; |
---|
585 | } |
---|
586 | |
---|
587 | uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) { |
---|
588 | if (s->avr != NULL) { |
---|
589 | #ifdef HAVE_AVRESAMPLE |
---|
590 | avresample_close( s->avr ); |
---|
591 | #elif defined(HAVE_SWRESAMPLE) |
---|
592 | swr_close ( s->avr ); |
---|
593 | #endif |
---|
594 | av_free ( s->avr ); |
---|
595 | } |
---|
596 | s->avr = NULL; |
---|
597 | if (s->avCodecCtx != NULL) { |
---|
598 | avcodec_close ( s->avCodecCtx ); |
---|
599 | } |
---|
600 | s->avCodecCtx = NULL; |
---|
601 | if (s->avFormatCtx != NULL) { |
---|
602 | avformat_close_input(&s->avFormatCtx); |
---|
603 | #ifndef HAVE_AUBIO_LIBAVCODEC_DEPRECATED // avoid crash on old libavcodec54 |
---|
604 | avformat_free_context(s->avFormatCtx); |
---|
605 | #endif |
---|
606 | s->avFormatCtx = NULL; |
---|
607 | } |
---|
608 | av_packet_unref(&s->avPacket); |
---|
609 | return AUBIO_OK; |
---|
610 | } |
---|
611 | |
---|
612 | void del_aubio_source_avcodec(aubio_source_avcodec_t * s){ |
---|
613 | if (!s) return; |
---|
614 | aubio_source_avcodec_close(s); |
---|
615 | if (s->output != NULL) { |
---|
616 | av_free(s->output); |
---|
617 | } |
---|
618 | s->output = NULL; |
---|
619 | if (s->avFrame != NULL) { |
---|
620 | av_frame_free( &(s->avFrame) ); |
---|
621 | } |
---|
622 | s->avFrame = NULL; |
---|
623 | if (s->path) { |
---|
624 | AUBIO_FREE(s->path); |
---|
625 | } |
---|
626 | s->path = NULL; |
---|
627 | AUBIO_FREE(s); |
---|
628 | } |
---|
629 | |
---|
630 | #endif /* HAVE_LIBAV */ |
---|