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