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