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