[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 | |
---|
| 21 | |
---|
| 22 | #include "config.h" |
---|
| 23 | |
---|
| 24 | #ifdef HAVE_AVCODEC |
---|
| 25 | |
---|
| 26 | #include <sndfile.h> |
---|
| 27 | #include <libavcodec/avcodec.h> |
---|
| 28 | #include <libavformat/avformat.h> |
---|
| 29 | #include <libavresample/avresample.h> |
---|
| 30 | #include <libavutil/opt.h> |
---|
| 31 | #include <stdlib.h> |
---|
| 32 | |
---|
| 33 | #include "aubio_priv.h" |
---|
| 34 | #include "fvec.h" |
---|
| 35 | #include "fmat.h" |
---|
| 36 | #include "source_avcodec.h" |
---|
| 37 | |
---|
[0af9003] | 38 | #define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE |
---|
[ba0ba10] | 39 | |
---|
| 40 | struct _aubio_source_avcodec_t { |
---|
| 41 | uint_t hop_size; |
---|
| 42 | uint_t samplerate; |
---|
| 43 | uint_t channels; |
---|
| 44 | |
---|
| 45 | // some data about the file |
---|
| 46 | char_t *path; |
---|
[d3b9fe4] | 47 | uint_t input_samplerate; |
---|
| 48 | uint_t input_channels; |
---|
[ba0ba10] | 49 | |
---|
| 50 | // avcodec stuff |
---|
| 51 | AVFormatContext *avFormatCtx; |
---|
| 52 | AVCodecContext *avCodecCtx; |
---|
| 53 | AVFrame *avFrame; |
---|
| 54 | AVAudioResampleContext *avr; |
---|
[3d5cddf] | 55 | float *output; |
---|
[d3b9fe4] | 56 | uint_t read_samples; |
---|
| 57 | uint_t read_index; |
---|
[7760b40] | 58 | sint_t selected_stream; |
---|
[eadd8d5] | 59 | uint_t eof; |
---|
[fcd963a] | 60 | uint_t multi; |
---|
[ba0ba10] | 61 | }; |
---|
| 62 | |
---|
[fcd963a] | 63 | /* convenience function to recreate the AVAudioResampleContext the first time _do |
---|
| 64 | * or _do_multi is called */ |
---|
| 65 | void aubio_source_avcodec_set_multi(aubio_source_avcodec_t * s, uint_t multi); |
---|
| 66 | |
---|
[ba0ba10] | 67 | aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplerate, uint_t hop_size) { |
---|
| 68 | aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t); |
---|
| 69 | int err; |
---|
| 70 | if (path == NULL) { |
---|
| 71 | AUBIO_ERR("Aborted opening null path\n"); |
---|
[b294b3e] | 72 | goto beach; |
---|
| 73 | } |
---|
| 74 | if ((sint_t)samplerate < 0) { |
---|
| 75 | AUBIO_ERR("Can not open %s with samplerate %d\n", path, samplerate); |
---|
| 76 | goto beach; |
---|
| 77 | } |
---|
| 78 | if ((sint_t)hop_size <= 0) { |
---|
| 79 | AUBIO_ERR("Can not open %s with hop_size %d\n", path, hop_size); |
---|
| 80 | goto beach; |
---|
[ba0ba10] | 81 | } |
---|
| 82 | |
---|
| 83 | s->hop_size = hop_size; |
---|
| 84 | s->channels = 1; |
---|
| 85 | s->path = path; |
---|
| 86 | |
---|
| 87 | // register all formats and codecs |
---|
| 88 | av_register_all(); |
---|
| 89 | |
---|
[eadd8d5] | 90 | // if path[0] != '/' |
---|
| 91 | //avformat_network_init(); |
---|
| 92 | |
---|
[d75e2d5] | 93 | // try opening the file and get some info about it |
---|
[ba0ba10] | 94 | AVFormatContext *avFormatCtx = s->avFormatCtx; |
---|
| 95 | avFormatCtx = NULL; |
---|
| 96 | if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) { |
---|
[0af9003] | 97 | char errorstr[256]; |
---|
| 98 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
| 99 | AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr); |
---|
[ba0ba10] | 100 | goto beach; |
---|
| 101 | } |
---|
| 102 | |
---|
[6abb4de] | 103 | // try to make sure max_analyze_duration is big enough for most songs |
---|
| 104 | avFormatCtx->max_analyze_duration *= 100; |
---|
| 105 | |
---|
[ba0ba10] | 106 | // retrieve stream information |
---|
| 107 | if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) { |
---|
[0af9003] | 108 | char errorstr[256]; |
---|
| 109 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
| 110 | AUBIO_ERR("Could not find stream information " "for %s (%s)\n", s->path, |
---|
| 111 | errorstr); |
---|
[ba0ba10] | 112 | goto beach; |
---|
| 113 | } |
---|
| 114 | |
---|
[1fe3ac2] | 115 | // dump information about file onto standard error |
---|
[ba0ba10] | 116 | //av_dump_format(avFormatCtx, 0, s->path, 0); |
---|
| 117 | |
---|
[1fe3ac2] | 118 | // look for the first audio stream |
---|
[ba0ba10] | 119 | uint_t i; |
---|
| 120 | sint_t selected_stream = -1; |
---|
| 121 | for (i = 0; i < avFormatCtx->nb_streams; i++) { |
---|
| 122 | if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { |
---|
| 123 | if (selected_stream == -1) { |
---|
| 124 | selected_stream = i; |
---|
| 125 | } else { |
---|
[1fe3ac2] | 126 | AUBIO_WRN("More than one audio stream in %s, " |
---|
| 127 | "taking the first one\n", s->path); |
---|
[ba0ba10] | 128 | } |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | if (selected_stream == -1) { |
---|
| 132 | AUBIO_ERR("No audio stream in %s\n", s->path); |
---|
| 133 | goto beach; |
---|
[d3b9fe4] | 134 | } |
---|
[eadd8d5] | 135 | //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path); |
---|
[7760b40] | 136 | s->selected_stream = selected_stream; |
---|
[ba0ba10] | 137 | |
---|
| 138 | AVCodecContext *avCodecCtx = s->avCodecCtx; |
---|
[d3b9fe4] | 139 | avCodecCtx = avFormatCtx->streams[selected_stream]->codec; |
---|
[ba0ba10] | 140 | AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id); |
---|
| 141 | if (codec == NULL) { |
---|
| 142 | AUBIO_ERR("Could not find decoder for %s", s->path); |
---|
| 143 | goto beach; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) { |
---|
[0af9003] | 147 | char errorstr[256]; |
---|
| 148 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
| 149 | AUBIO_ERR("Could not load codec for %s (%s)\n", s->path, errorstr); |
---|
[ba0ba10] | 150 | goto beach; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /* get input specs */ |
---|
| 154 | s->input_samplerate = avCodecCtx->sample_rate; |
---|
| 155 | s->input_channels = avCodecCtx->channels; |
---|
| 156 | //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate); |
---|
| 157 | //AUBIO_DBG("input_channels: %d\n", s->input_channels); |
---|
| 158 | |
---|
| 159 | if (samplerate == 0) { |
---|
| 160 | samplerate = s->input_samplerate; |
---|
| 161 | //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate); |
---|
| 162 | } |
---|
| 163 | s->samplerate = samplerate; |
---|
| 164 | |
---|
| 165 | AVFrame *avFrame = s->avFrame; |
---|
| 166 | avFrame = avcodec_alloc_frame(); |
---|
| 167 | if (!avFrame) { |
---|
| 168 | AUBIO_ERR("Could not allocate frame for (%s)\n", s->path); |
---|
| 169 | } |
---|
| 170 | |
---|
[d3b9fe4] | 171 | /* allocate output for avr */ |
---|
[0af9003] | 172 | s->output = (float *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(float)); |
---|
[ba0ba10] | 173 | |
---|
| 174 | s->read_samples = 0; |
---|
| 175 | s->read_index = 0; |
---|
| 176 | |
---|
| 177 | s->avFormatCtx = avFormatCtx; |
---|
| 178 | s->avCodecCtx = avCodecCtx; |
---|
| 179 | s->avFrame = avFrame; |
---|
[fcd963a] | 180 | |
---|
| 181 | // default to mono output |
---|
| 182 | aubio_source_avcodec_set_multi(s, 0); |
---|
[ba0ba10] | 183 | |
---|
[eadd8d5] | 184 | s->eof = 0; |
---|
[fcd963a] | 185 | s->multi = 0; |
---|
[eadd8d5] | 186 | |
---|
[ba0ba10] | 187 | //av_log_set_level(AV_LOG_QUIET); |
---|
| 188 | |
---|
| 189 | return s; |
---|
| 190 | |
---|
| 191 | beach: |
---|
[fcd963a] | 192 | //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n", |
---|
| 193 | // s->path, s->samplerate, s->hop_size); |
---|
[ba0ba10] | 194 | del_aubio_source_avcodec(s); |
---|
| 195 | return NULL; |
---|
| 196 | } |
---|
| 197 | |
---|
[d3b9fe4] | 198 | void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) { |
---|
[ba0ba10] | 199 | AVFormatContext *avFormatCtx = s->avFormatCtx; |
---|
| 200 | AVCodecContext *avCodecCtx = s->avCodecCtx; |
---|
| 201 | AVFrame *avFrame = s->avFrame; |
---|
[f3b93c6] | 202 | AVPacket avPacket; |
---|
| 203 | av_init_packet (&avPacket); |
---|
[ba0ba10] | 204 | AVAudioResampleContext *avr = s->avr; |
---|
[3d5cddf] | 205 | float *output = s->output; |
---|
[0af9003] | 206 | *read_samples = 0; |
---|
[ba0ba10] | 207 | |
---|
[7760b40] | 208 | do |
---|
| 209 | { |
---|
| 210 | int err = av_read_frame (avFormatCtx, &avPacket); |
---|
[0af9003] | 211 | if (err == AVERROR_EOF) { |
---|
| 212 | s->eof = 1; |
---|
| 213 | goto beach; |
---|
| 214 | } |
---|
[7760b40] | 215 | if (err != 0) { |
---|
[0af9003] | 216 | char errorstr[256]; |
---|
| 217 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
| 218 | AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr); |
---|
| 219 | goto beach; |
---|
[7760b40] | 220 | } |
---|
| 221 | } while (avPacket.stream_index != s->selected_stream); |
---|
[ba0ba10] | 222 | |
---|
| 223 | int got_frame = 0; |
---|
| 224 | int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket); |
---|
| 225 | |
---|
| 226 | if (len < 0) { |
---|
| 227 | AUBIO_ERR("Error while decoding %s\n", s->path); |
---|
[0af9003] | 228 | goto beach; |
---|
[ba0ba10] | 229 | } |
---|
| 230 | if (got_frame == 0) { |
---|
[eadd8d5] | 231 | //AUBIO_ERR("Could not get frame for (%s)\n", s->path); |
---|
[0af9003] | 232 | goto beach; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | int in_linesize = 0; |
---|
| 236 | av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels, |
---|
| 237 | avFrame->nb_samples, avCodecCtx->sample_fmt, 1); |
---|
[ba0ba10] | 238 | int in_samples = avFrame->nb_samples; |
---|
[0af9003] | 239 | int out_linesize = 0; |
---|
| 240 | int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE; |
---|
[0044b43] | 241 | int out_samples = avresample_convert ( avr, |
---|
[0af9003] | 242 | (uint8_t **)&output, out_linesize, max_out_samples, |
---|
| 243 | (uint8_t **)avFrame->data, in_linesize, in_samples); |
---|
| 244 | if (out_samples <= 0) { |
---|
| 245 | AUBIO_ERR("No sample found while converting frame (%s)\n", s->path); |
---|
| 246 | goto beach; |
---|
[ba0ba10] | 247 | } |
---|
| 248 | |
---|
[0af9003] | 249 | *read_samples = out_samples; |
---|
| 250 | |
---|
| 251 | beach: |
---|
[ba0ba10] | 252 | s->avFormatCtx = avFormatCtx; |
---|
| 253 | s->avCodecCtx = avCodecCtx; |
---|
| 254 | s->avFrame = avFrame; |
---|
| 255 | s->avr = avr; |
---|
| 256 | s->output = output; |
---|
| 257 | |
---|
[0af9003] | 258 | av_free_packet(&avPacket); |
---|
[ba0ba10] | 259 | } |
---|
| 260 | |
---|
[fcd963a] | 261 | void aubio_source_avcodec_set_multi(aubio_source_avcodec_t * s, uint_t multi) { |
---|
| 262 | if ( (multi != s->multi) || (s->avr == NULL) ) { |
---|
| 263 | int64_t input_layout = av_get_default_channel_layout(s->input_channels); |
---|
| 264 | int64_t mono_layout = av_get_default_channel_layout(1); |
---|
| 265 | if (s->avr != NULL) { |
---|
| 266 | avresample_close( s->avr ); |
---|
| 267 | av_free ( s->avr ); |
---|
| 268 | s->avr = NULL; |
---|
| 269 | } |
---|
| 270 | AVAudioResampleContext *avr = s->avr; |
---|
| 271 | avr = avresample_alloc_context(); |
---|
| 272 | |
---|
| 273 | av_opt_set_int(avr, "in_channel_layout", input_layout, 0); |
---|
| 274 | if (multi) { |
---|
| 275 | av_opt_set_int(avr, "out_channel_layout", input_layout, 0); |
---|
| 276 | } else { |
---|
| 277 | av_opt_set_int(avr, "out_channel_layout", mono_layout, 0); |
---|
| 278 | } |
---|
| 279 | av_opt_set_int(avr, "in_sample_rate", s->input_samplerate, 0); |
---|
| 280 | av_opt_set_int(avr, "out_sample_rate", s->samplerate, 0); |
---|
| 281 | av_opt_set_int(avr, "in_sample_fmt", s->avCodecCtx->sample_fmt, 0); |
---|
| 282 | av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0); |
---|
| 283 | int err; |
---|
| 284 | if ( ( err = avresample_open(avr) ) < 0) { |
---|
| 285 | char errorstr[256]; |
---|
| 286 | av_strerror (err, errorstr, sizeof(errorstr)); |
---|
| 287 | AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n", |
---|
| 288 | s->path, errorstr); |
---|
| 289 | //goto beach; |
---|
| 290 | return; |
---|
| 291 | } |
---|
| 292 | s->avr = avr; |
---|
| 293 | s->multi = multi; |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | |
---|
[ba0ba10] | 297 | void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){ |
---|
[fcd963a] | 298 | if (s->multi == 1) aubio_source_avcodec_set_multi(s, 0); |
---|
[ba0ba10] | 299 | uint_t i; |
---|
[eadd8d5] | 300 | uint_t end = 0; |
---|
| 301 | uint_t total_wrote = 0; |
---|
| 302 | while (total_wrote < s->hop_size) { |
---|
| 303 | end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote); |
---|
| 304 | for (i = 0; i < end; i++) { |
---|
[3d5cddf] | 305 | read_data->data[i + total_wrote] = s->output[i + s->read_index]; |
---|
[ba0ba10] | 306 | } |
---|
[eadd8d5] | 307 | total_wrote += end; |
---|
| 308 | if (total_wrote < s->hop_size) { |
---|
| 309 | uint_t avcodec_read = 0; |
---|
| 310 | aubio_source_avcodec_readframe(s, &avcodec_read); |
---|
| 311 | s->read_samples = avcodec_read; |
---|
| 312 | s->read_index = 0; |
---|
| 313 | if (s->eof) { |
---|
| 314 | break; |
---|
[ba0ba10] | 315 | } |
---|
[eadd8d5] | 316 | } else { |
---|
| 317 | s->read_index += end; |
---|
[ba0ba10] | 318 | } |
---|
[eadd8d5] | 319 | } |
---|
| 320 | if (total_wrote < s->hop_size) { |
---|
| 321 | for (i = end; i < s->hop_size; i++) { |
---|
| 322 | read_data->data[i] = 0.; |
---|
[ba0ba10] | 323 | } |
---|
| 324 | } |
---|
[eadd8d5] | 325 | *read = total_wrote; |
---|
[ba0ba10] | 326 | } |
---|
| 327 | |
---|
| 328 | void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){ |
---|
[fcd963a] | 329 | if (s->multi == 0) aubio_source_avcodec_set_multi(s, 1); |
---|
| 330 | uint_t i,j; |
---|
| 331 | uint_t end = 0; |
---|
| 332 | uint_t total_wrote = 0; |
---|
| 333 | while (total_wrote < s->hop_size) { |
---|
| 334 | end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote); |
---|
| 335 | for (j = 0; j < read_data->height; j++) { |
---|
| 336 | for (i = 0; i < end; i++) { |
---|
| 337 | read_data->data[j][i + total_wrote] = |
---|
| 338 | s->output[(i + s->read_index) * s->input_channels + j]; |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | total_wrote += end; |
---|
| 342 | if (total_wrote < s->hop_size) { |
---|
| 343 | uint_t avcodec_read = 0; |
---|
| 344 | aubio_source_avcodec_readframe(s, &avcodec_read); |
---|
| 345 | s->read_samples = avcodec_read; |
---|
| 346 | s->read_index = 0; |
---|
| 347 | if (s->eof) { |
---|
| 348 | break; |
---|
| 349 | } |
---|
| 350 | } else { |
---|
| 351 | s->read_index += end; |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | if (total_wrote < s->hop_size) { |
---|
| 355 | for (j = 0; j < read_data->height; j++) { |
---|
| 356 | for (i = end; i < s->hop_size; i++) { |
---|
| 357 | read_data->data[j][i] = 0.; |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | *read = total_wrote; |
---|
[ba0ba10] | 362 | } |
---|
| 363 | |
---|
| 364 | uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) { |
---|
| 365 | return s->samplerate; |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) { |
---|
| 369 | return s->input_channels; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) { |
---|
| 373 | //uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate); |
---|
[fcd963a] | 374 | AUBIO_ERR("Not implemented, _seek(%d) file %s", pos, s->path); |
---|
| 375 | return -1; //sf_seek (s->handle, resampled_pos, SEEK_SET); |
---|
[ba0ba10] | 376 | } |
---|
| 377 | |
---|
| 378 | void del_aubio_source_avcodec(aubio_source_avcodec_t * s){ |
---|
| 379 | if (!s) return; |
---|
| 380 | if (s->output != NULL) { |
---|
| 381 | av_free(s->output); |
---|
| 382 | } |
---|
| 383 | if (s->avr != NULL) { |
---|
| 384 | avresample_close( s->avr ); |
---|
| 385 | av_free ( s->avr ); |
---|
| 386 | } |
---|
| 387 | s->avr = NULL; |
---|
| 388 | if (s->avFrame != NULL) { |
---|
| 389 | avcodec_free_frame( &(s->avFrame) ); |
---|
| 390 | } |
---|
| 391 | s->avFrame = NULL; |
---|
| 392 | if (s->avCodecCtx != NULL) { |
---|
| 393 | avcodec_close ( s->avCodecCtx ); |
---|
| 394 | } |
---|
| 395 | s->avCodecCtx = NULL; |
---|
| 396 | if (s->avFormatCtx != NULL) { |
---|
| 397 | avformat_close_input ( &(s->avFormatCtx) ); |
---|
| 398 | } |
---|
| 399 | s->avFrame = NULL; |
---|
| 400 | s->avFormatCtx = NULL; |
---|
| 401 | AUBIO_FREE(s); |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | #endif /* HAVE_SNDFILE */ |
---|