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