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