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