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