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