2 Copyright (C) 2013 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
26 // determine whether we use libavformat from ffmpe or libav
27 #define FFMPEG_LIBAVFORMAT (LIBAVFORMAT_VERSION_MICRO > 99)
29 #include <libavcodec/avcodec.h>
30 #include <libavformat/avformat.h>
31 #include <libavresample/avresample.h>
32 #include <libavutil/opt.h>
35 #include "aubio_priv.h"
38 #include "source_avcodec.h"
40 #define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
42 struct _aubio_source_avcodec_t {
47 // some data about the file
49 uint_t input_samplerate;
50 uint_t input_channels;
53 AVFormatContext *avFormatCtx;
54 AVCodecContext *avCodecCtx;
56 AVAudioResampleContext *avr;
60 sint_t selected_stream;
65 // hack to create or re-create the context the first time _do or _do_multi is called
66 void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi);
67 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples);
69 aubio_source_avcodec_t * new_aubio_source_avcodec(const char_t * path, uint_t samplerate, uint_t hop_size) {
70 aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
73 AUBIO_ERR("source_avcodec: Aborted opening null path\n");
76 if ((sint_t)samplerate < 0) {
77 AUBIO_ERR("source_avcodec: Can not open %s with samplerate %d\n", path, samplerate);
80 if ((sint_t)hop_size <= 0) {
81 AUBIO_ERR("source_avcodec: Can not open %s with hop_size %d\n", path, hop_size);
85 s->hop_size = hop_size;
88 if (s->path) AUBIO_FREE(s->path);
89 s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
90 strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
92 // register all formats and codecs
96 //avformat_network_init();
98 // try opening the file and get some info about it
99 AVFormatContext *avFormatCtx = s->avFormatCtx;
101 if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
103 av_strerror (err, errorstr, sizeof(errorstr));
104 AUBIO_ERR("source_avcodec: Failed opening %s (%s)\n", s->path, errorstr);
108 // try to make sure max_analyze_duration is big enough for most songs
109 #if FFMPEG_LIBAVFORMAT
110 avFormatCtx->max_analyze_duration2 *= 100;
112 avFormatCtx->max_analyze_duration *= 100;
115 // retrieve stream information
116 if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
118 av_strerror (err, errorstr, sizeof(errorstr));
119 AUBIO_ERR("source_avcodec: Could not find stream information " "for %s (%s)\n", s->path,
124 // dump information about file onto standard error
125 //av_dump_format(avFormatCtx, 0, s->path, 0);
127 // look for the first audio stream
129 sint_t selected_stream = -1;
130 for (i = 0; i < avFormatCtx->nb_streams; i++) {
131 if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
132 if (selected_stream == -1) {
135 AUBIO_WRN("source_avcodec: More than one audio stream in %s, "
136 "taking the first one\n", s->path);
140 if (selected_stream == -1) {
141 AUBIO_ERR("source_avcodec: No audio stream in %s\n", s->path);
144 //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
145 s->selected_stream = selected_stream;
147 AVCodecContext *avCodecCtx = s->avCodecCtx;
148 avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
149 AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id);
151 AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path);
155 if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
157 av_strerror (err, errorstr, sizeof(errorstr));
158 AUBIO_ERR("source_avcodec: Could not load codec for %s (%s)\n", s->path, errorstr);
162 /* get input specs */
163 s->input_samplerate = avCodecCtx->sample_rate;
164 s->input_channels = avCodecCtx->channels;
165 //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate);
166 //AUBIO_DBG("input_channels: %d\n", s->input_channels);
168 if (samplerate == 0) {
169 s->samplerate = s->input_samplerate;
171 s->samplerate = samplerate;
174 if (s->samplerate > s->input_samplerate) {
175 AUBIO_WRN("source_avcodec: upsampling %s from %d to %d\n", s->path,
176 s->input_samplerate, s->samplerate);
179 AVFrame *avFrame = s->avFrame;
180 avFrame = av_frame_alloc();
182 AUBIO_ERR("source_avcodec: Could not allocate frame for (%s)\n", s->path);
185 /* allocate output for avr */
186 s->output = (float *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(float));
191 s->avFormatCtx = avFormatCtx;
192 s->avCodecCtx = avCodecCtx;
193 s->avFrame = avFrame;
195 // default to mono output
196 aubio_source_avcodec_reset_resampler(s, 0);
201 //av_log_set_level(AV_LOG_QUIET);
206 //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
207 // s->path, s->samplerate, s->hop_size);
208 del_aubio_source_avcodec(s);
212 void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi) {
213 if ( (multi != s->multi) || (s->avr == NULL) ) {
214 int64_t input_layout = av_get_default_channel_layout(s->input_channels);
215 uint_t output_channels = multi ? s->input_channels : 1;
216 int64_t output_layout = av_get_default_channel_layout(output_channels);
217 if (s->avr != NULL) {
218 avresample_close( s->avr );
222 AVAudioResampleContext *avr = s->avr;
223 avr = avresample_alloc_context();
225 av_opt_set_int(avr, "in_channel_layout", input_layout, 0);
226 av_opt_set_int(avr, "out_channel_layout", output_layout, 0);
227 av_opt_set_int(avr, "in_sample_rate", s->input_samplerate, 0);
228 av_opt_set_int(avr, "out_sample_rate", s->samplerate, 0);
229 av_opt_set_int(avr, "in_sample_fmt", s->avCodecCtx->sample_fmt, 0);
230 av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
232 if ( ( err = avresample_open(avr) ) < 0) {
234 av_strerror (err, errorstr, sizeof(errorstr));
235 AUBIO_ERR("source_avcodec: Could not open AVAudioResampleContext for %s (%s)\n",
245 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
246 AVFormatContext *avFormatCtx = s->avFormatCtx;
247 AVCodecContext *avCodecCtx = s->avCodecCtx;
248 AVFrame *avFrame = s->avFrame;
250 av_init_packet (&avPacket);
251 AVAudioResampleContext *avr = s->avr;
252 float *output = s->output;
257 int err = av_read_frame (avFormatCtx, &avPacket);
258 if (err == AVERROR_EOF) {
264 av_strerror (err, errorstr, sizeof(errorstr));
265 AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr);
268 } while (avPacket.stream_index != s->selected_stream);
271 int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
274 AUBIO_ERR("Error while decoding %s\n", s->path);
277 if (got_frame == 0) {
278 //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
283 av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
284 avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
285 int in_samples = avFrame->nb_samples;
286 int out_linesize = 0;
287 int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
288 int out_samples = avresample_convert ( avr,
289 (uint8_t **)&output, out_linesize, max_out_samples,
290 (uint8_t **)avFrame->data, in_linesize, in_samples);
291 if (out_samples <= 0) {
292 //AUBIO_ERR("No sample found while converting frame (%s)\n", s->path);
296 *read_samples = out_samples;
299 s->avFormatCtx = avFormatCtx;
300 s->avCodecCtx = avCodecCtx;
301 s->avFrame = avFrame;
305 av_free_packet(&avPacket);
308 void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
309 if (s->multi == 1) aubio_source_avcodec_reset_resampler(s, 0);
312 uint_t total_wrote = 0;
313 while (total_wrote < s->hop_size) {
314 end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
315 for (i = 0; i < end; i++) {
316 read_data->data[i + total_wrote] = s->output[i + s->read_index];
319 if (total_wrote < s->hop_size) {
320 uint_t avcodec_read = 0;
321 aubio_source_avcodec_readframe(s, &avcodec_read);
322 s->read_samples = avcodec_read;
328 s->read_index += end;
331 if (total_wrote < s->hop_size) {
332 for (i = end; i < s->hop_size; i++) {
333 read_data->data[i] = 0.;
339 void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
340 if (s->multi == 0) aubio_source_avcodec_reset_resampler(s, 1);
343 uint_t total_wrote = 0;
344 while (total_wrote < s->hop_size) {
345 end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
346 for (j = 0; j < read_data->height; j++) {
347 for (i = 0; i < end; i++) {
348 read_data->data[j][i + total_wrote] =
349 s->output[(i + s->read_index) * s->input_channels + j];
353 if (total_wrote < s->hop_size) {
354 uint_t avcodec_read = 0;
355 aubio_source_avcodec_readframe(s, &avcodec_read);
356 s->read_samples = avcodec_read;
362 s->read_index += end;
365 if (total_wrote < s->hop_size) {
366 for (j = 0; j < read_data->height; j++) {
367 for (i = end; i < s->hop_size; i++) {
368 read_data->data[j][i] = 0.;
375 uint_t aubio_source_avcodec_get_samplerate(const aubio_source_avcodec_t * s) {
376 return s->samplerate;
379 uint_t aubio_source_avcodec_get_channels(const aubio_source_avcodec_t * s) {
380 return s->input_channels;
383 uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
384 int64_t resampled_pos = (uint_t)ROUND(pos * (s->input_samplerate * 1. / s->samplerate));
385 int64_t min_ts = MAX(resampled_pos - 2000, 0);
386 int64_t max_ts = MIN(resampled_pos + 2000, INT64_MAX);
387 int seek_flags = AVSEEK_FLAG_FRAME | AVSEEK_FLAG_ANY;
388 int ret = avformat_seek_file(s->avFormatCtx, s->selected_stream,
389 min_ts, resampled_pos, max_ts, seek_flags);
391 AUBIO_ERR("Failed seeking to %d in file %s", pos, s->path);
397 // reset the AVAudioResampleContext
398 avresample_close(s->avr);
399 avresample_open(s->avr);
403 uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) {
404 if (s->avr != NULL) {
405 avresample_close( s->avr );
409 if (s->avCodecCtx != NULL) {
410 avcodec_close ( s->avCodecCtx );
412 s->avCodecCtx = NULL;
413 if (s->avFormatCtx != NULL) {
414 avformat_close_input ( &(s->avFormatCtx) );
416 s->avFormatCtx = NULL;
420 void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
422 aubio_source_avcodec_close(s);
423 if (s->output != NULL) {
427 if (s->avFrame != NULL) {
428 av_frame_free( &(s->avFrame) );
430 if (s->path) AUBIO_FREE(s->path);
435 #endif /* HAVE_LIBAV */