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/>.
27 #include <libavcodec/avcodec.h>
28 #include <libavformat/avformat.h>
29 #include <libavresample/avresample.h>
30 #include <libavutil/opt.h>
33 #include "aubio_priv.h"
36 #include "source_avcodec.h"
38 #define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
40 struct _aubio_source_avcodec_t {
45 // some data about the file
47 uint_t input_samplerate;
48 uint_t input_channels;
51 AVFormatContext *avFormatCtx;
52 AVCodecContext *avCodecCtx;
54 AVAudioResampleContext *avr;
58 sint_t selected_stream;
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);
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);
71 AUBIO_ERR("Aborted opening null path\n");
74 if ((sint_t)samplerate < 0) {
75 AUBIO_ERR("Can not open %s with samplerate %d\n", path, samplerate);
78 if ((sint_t)hop_size <= 0) {
79 AUBIO_ERR("Can not open %s with hop_size %d\n", path, hop_size);
83 s->hop_size = hop_size;
87 // register all formats and codecs
91 //avformat_network_init();
93 // try opening the file and get some info about it
94 AVFormatContext *avFormatCtx = s->avFormatCtx;
96 if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
98 av_strerror (err, errorstr, sizeof(errorstr));
99 AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr);
103 // try to make sure max_analyze_duration is big enough for most songs
104 avFormatCtx->max_analyze_duration *= 100;
106 // retrieve stream information
107 if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
109 av_strerror (err, errorstr, sizeof(errorstr));
110 AUBIO_ERR("Could not find stream information " "for %s (%s)\n", s->path,
115 // dump information about file onto standard error
116 //av_dump_format(avFormatCtx, 0, s->path, 0);
118 // look for the first audio stream
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) {
126 AUBIO_WRN("More than one audio stream in %s, "
127 "taking the first one\n", s->path);
131 if (selected_stream == -1) {
132 AUBIO_ERR("No audio stream in %s\n", s->path);
135 //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
136 s->selected_stream = selected_stream;
138 AVCodecContext *avCodecCtx = s->avCodecCtx;
139 avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
140 AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id);
142 AUBIO_ERR("Could not find decoder for %s", s->path);
146 if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
148 av_strerror (err, errorstr, sizeof(errorstr));
149 AUBIO_ERR("Could not load codec for %s (%s)\n", s->path, errorstr);
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);
159 if (samplerate == 0) {
160 samplerate = s->input_samplerate;
161 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
163 s->samplerate = samplerate;
165 AVFrame *avFrame = s->avFrame;
166 avFrame = avcodec_alloc_frame();
168 AUBIO_ERR("Could not allocate frame for (%s)\n", s->path);
171 /* allocate output for avr */
172 s->output = (float *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(float));
177 s->avFormatCtx = avFormatCtx;
178 s->avCodecCtx = avCodecCtx;
179 s->avFrame = avFrame;
181 // default to mono output
182 aubio_source_avcodec_set_multi(s, 0);
187 //av_log_set_level(AV_LOG_QUIET);
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);
194 del_aubio_source_avcodec(s);
198 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
199 AVFormatContext *avFormatCtx = s->avFormatCtx;
200 AVCodecContext *avCodecCtx = s->avCodecCtx;
201 AVFrame *avFrame = s->avFrame;
203 av_init_packet (&avPacket);
204 AVAudioResampleContext *avr = s->avr;
205 float *output = s->output;
210 int err = av_read_frame (avFormatCtx, &avPacket);
211 if (err == AVERROR_EOF) {
217 av_strerror (err, errorstr, sizeof(errorstr));
218 AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr);
221 } while (avPacket.stream_index != s->selected_stream);
224 int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
227 AUBIO_ERR("Error while decoding %s\n", s->path);
230 if (got_frame == 0) {
231 //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
236 av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
237 avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
238 int in_samples = avFrame->nb_samples;
239 int out_linesize = 0;
240 int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
241 int out_samples = avresample_convert ( avr,
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);
249 *read_samples = out_samples;
252 s->avFormatCtx = avFormatCtx;
253 s->avCodecCtx = avCodecCtx;
254 s->avFrame = avFrame;
258 av_free_packet(&avPacket);
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 );
270 AVAudioResampleContext *avr = s->avr;
271 avr = avresample_alloc_context();
273 av_opt_set_int(avr, "in_channel_layout", input_layout, 0);
275 av_opt_set_int(avr, "out_channel_layout", input_layout, 0);
277 av_opt_set_int(avr, "out_channel_layout", mono_layout, 0);
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);
284 if ( ( err = avresample_open(avr) ) < 0) {
286 av_strerror (err, errorstr, sizeof(errorstr));
287 AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n",
297 void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
298 if (s->multi == 1) aubio_source_avcodec_set_multi(s, 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++) {
305 read_data->data[i + total_wrote] = s->output[i + s->read_index];
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;
317 s->read_index += end;
320 if (total_wrote < s->hop_size) {
321 for (i = end; i < s->hop_size; i++) {
322 read_data->data[i] = 0.;
328 void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
329 if (s->multi == 0) aubio_source_avcodec_set_multi(s, 1);
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];
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;
351 s->read_index += end;
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.;
364 uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) {
365 return s->samplerate;
368 uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) {
369 return s->input_channels;
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);
374 AUBIO_ERR("Not implemented, _seek(%d) file %s", pos, s->path);
375 return -1; //sf_seek (s->handle, resampled_pos, SEEK_SET);
378 void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
380 if (s->output != NULL) {
383 if (s->avr != NULL) {
384 avresample_close( s->avr );
388 if (s->avFrame != NULL) {
389 avcodec_free_frame( &(s->avFrame) );
392 if (s->avCodecCtx != NULL) {
393 avcodec_close ( s->avCodecCtx );
395 s->avCodecCtx = NULL;
396 if (s->avFormatCtx != NULL) {
397 avformat_close_input ( &(s->avFormatCtx) );
400 s->avFormatCtx = NULL;
404 #endif /* HAVE_SNDFILE */