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