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_MIN_BUFFER_SIZE FF_MIN_BUFFER_SIZE
40 #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
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;
57 AVAudioResampleContext *avr;
63 aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplerate, uint_t hop_size) {
64 aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
68 AUBIO_ERR("Aborted opening null path\n");
72 s->hop_size = hop_size;
76 // try opening the file and get some info about it
77 // register all formats and codecs
81 AVFormatContext *avFormatCtx = s->avFormatCtx;
83 if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
84 uint8_t errorstr_len = 128;
85 char errorstr[errorstr_len];
86 if (av_strerror (err, errorstr, errorstr_len) == 0) {
87 AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr);
89 AUBIO_ERR("Failed opening %s (unknown error)\n", s->path);
94 // try to make sure max_analyze_duration is big enough for most songs
95 avFormatCtx->max_analyze_duration *= 100;
97 // retrieve stream information
98 if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
99 uint8_t errorstr_len = 128;
100 char errorstr[errorstr_len];
101 if (av_strerror (err, errorstr, errorstr_len) == 0) {
102 AUBIO_ERR("Could not find stream information for %s (%s)\n", s->path, errorstr);
104 AUBIO_ERR("Could not find stream information for %s (unknown error)\n", s->path);
109 // Dump information about file onto standard error
110 //av_dump_format(avFormatCtx, 0, s->path, 0);
113 sint_t selected_stream = -1;
114 for (i = 0; i < avFormatCtx->nb_streams; i++) {
115 if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
116 if (selected_stream == -1) {
119 AUBIO_WRN("More than one audio stream in %s, taking the first one\n", s->path);
123 if (selected_stream == -1) {
124 AUBIO_ERR("No audio stream in %s\n", s->path);
128 //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
130 AVCodecContext *avCodecCtx = s->avCodecCtx;
131 avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
132 AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id);
134 AUBIO_ERR("Could not find decoder for %s", s->path);
138 if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
139 uint8_t errorstr_len = 128;
140 char errorstr[errorstr_len];
141 if (av_strerror (err, errorstr, errorstr_len) == 0) {
142 AUBIO_ERR("Could not load codec for %s (%s)\n", s->path, errorstr);
144 AUBIO_ERR("Could not load codec for %s (unknown error)\n", s->path);
149 /* get input specs */
150 s->input_samplerate = avCodecCtx->sample_rate;
151 s->input_channels = avCodecCtx->channels;
152 //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate);
153 //AUBIO_DBG("input_channels: %d\n", s->input_channels);
155 if (samplerate == 0) {
156 samplerate = s->input_samplerate;
157 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
159 s->samplerate = samplerate;
161 int64_t input_layout = av_get_default_channel_layout(s->input_channels);
162 int64_t mono_layout = av_get_default_channel_layout(1);
164 AVAudioResampleContext *avr = s->avr;
165 avr = avresample_alloc_context();
166 av_opt_set_int(avr, "in_channel_layout", input_layout, 0);
167 av_opt_set_int(avr, "out_channel_layout", mono_layout, 0);
168 av_opt_set_int(avr, "in_sample_rate", s->input_samplerate, 0);
169 av_opt_set_int(avr, "out_sample_rate", s->samplerate, 0);
170 av_opt_set_int(avr, "in_sample_fmt", avCodecCtx->sample_fmt, 0);
171 av_opt_set_int(avr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
172 if ( ( err = avresample_open(avr) ) < 0) {
173 uint8_t errorstr_len = 128;
174 char errorstr[errorstr_len];
175 if (av_strerror (err, errorstr, errorstr_len) == 0) {
176 AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n", s->path, errorstr);
178 AUBIO_ERR("Could not open AVAudioResampleContext for %s (unknown error)\n", s->path);
183 AVFrame *avFrame = s->avFrame;
184 avFrame = avcodec_alloc_frame();
186 AUBIO_ERR("Could not allocate frame for (%s)\n", s->path);
188 AVPacket avPacket = s->avPacket;
189 av_init_packet(&avPacket);
191 /* allocate output for avr */
192 s->output = (int16_t *)av_malloc(AUBIO_AVCODEC_MIN_BUFFER_SIZE * sizeof(int16_t));
197 s->avFormatCtx = avFormatCtx;
198 s->avCodecCtx = avCodecCtx;
199 s->avFrame = avFrame;
200 s->avPacket = avPacket;
203 //av_log_set_level(AV_LOG_QUIET);
208 AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
209 s->path, s->samplerate, s->hop_size);
210 del_aubio_source_avcodec(s);
214 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
215 AVFormatContext *avFormatCtx = s->avFormatCtx;
216 AVCodecContext *avCodecCtx = s->avCodecCtx;
217 AVFrame *avFrame = s->avFrame;
218 AVPacket avPacket = s->avPacket;
219 AVAudioResampleContext *avr = s->avr;
220 int16_t *output = s->output;
223 int err = av_read_frame (avFormatCtx, &avPacket);
225 //AUBIO_ERR("Could not read frame for (%s)\n", s->path);
231 int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
234 AUBIO_ERR("Error while decoding %s\n", s->path);
237 if (got_frame == 0) {
238 AUBIO_ERR("Could not get frame for (%s)\n", s->path);
241 av_samples_get_buffer_size(NULL,
242 avCodecCtx->channels, avFrame->nb_samples,
243 avCodecCtx->sample_fmt, 1);
244 AUBIO_WRN("Got data_size %d frame for (%s)\n", data_size, s->path);
247 int in_samples = avFrame->nb_samples;
248 int in_plane_size = 0; //avFrame->linesize[0];
249 int out_plane_size = 0; //sizeof(float); //in_samples * sizeof(float);
250 int max_out_samples = AUBIO_AVCODEC_MIN_BUFFER_SIZE;
251 if (avresample_convert ( avr,
252 (uint8_t **)&output, out_plane_size, max_out_samples,
253 (uint8_t **)avFrame->data, in_plane_size, in_samples) < 0) {
254 AUBIO_ERR("Could not convert frame (%s)\n", s->path);
256 //AUBIO_ERR("Got in_plane_size %d frame for (%s)\n", in_plane_size, s->path);
257 //AUBIO_WRN("Delay is %d for %s\n", avresample_get_delay(avr), s->path);
258 //AUBIO_WRN("max_out_samples is %d for AUBIO_AVCODEC_MIN_BUFFER_SIZE %d\n",
259 // max_out_samples, AUBIO_AVCODEC_MIN_BUFFER_SIZE);
261 uint_t out_samples = avresample_available(avr) + (avresample_get_delay(avr)
262 + in_samples) * s->samplerate / s->input_samplerate;
263 //AUBIO_WRN("Converted %d to %d samples\n", in_samples, out_samples);
264 //for (i = 0; i < out_samples; i ++) {
265 // AUBIO_DBG("%f\n", SHORT_TO_FLOAT(output[i]));
267 s->avFormatCtx = avFormatCtx;
268 s->avCodecCtx = avCodecCtx;
269 s->avFrame = avFrame;
270 s->avPacket = avPacket;
274 *read_samples = out_samples;
278 void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
280 //AUBIO_DBG("entering 'do' with %d, %d\n", s->read_samples, s->read_index);
282 if (s->read_samples == 0) {
283 uint_t avcodec_read = 0;
284 aubio_source_avcodec_readframe(s, &avcodec_read);
285 s->read_samples += avcodec_read;
288 if (s->read_samples < s->hop_size) {
289 // write the end of the buffer to the beginning of read_data
290 uint_t partial = s->read_samples;
291 for (i = 0; i < partial; i++) {
292 read_data->data[i] = SHORT_TO_FLOAT(s->output[i + s->read_index]);
297 uint_t avcodec_read = 0;
298 aubio_source_avcodec_readframe(s, &avcodec_read);
299 s->read_samples += avcodec_read;
301 // write the beginning of the buffer to the end of read_data
302 uint_t end = MIN(s->hop_size, s->read_samples);
303 if (avcodec_read == 0) {
306 for (i = partial; i < end; i++) {
307 read_data->data[i] = SHORT_TO_FLOAT(s->output[i - partial + s->read_index]);
309 if (end < s->hop_size) {
310 for (i = end; i < s->hop_size; i++) {
311 read_data->data[i] = 0.;
314 s->read_index += partial;
315 s->read_samples -= partial;
318 for (i = 0; i < s->hop_size; i++) {
319 read_data->data[i] = SHORT_TO_FLOAT(s->output[i + s->read_index]);
321 s->read_index += s->hop_size;
322 s->read_samples -= s->hop_size;
327 void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
328 //uint_t i,j, input_channels = s->input_channels;
331 uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) {
332 return s->samplerate;
335 uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) {
336 return s->input_channels;
339 uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
340 //uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate);
341 return 0; //sf_seek (s->handle, resampled_pos, SEEK_SET);
344 void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
346 if (s->output != NULL) {
349 if (s->avr != NULL) {
350 avresample_close( s->avr );
354 if (s->avFrame != NULL) {
355 avcodec_free_frame( &(s->avFrame) );
358 if ( &(s->avPacket) != NULL) {
359 av_free_packet( &(s->avPacket) );
361 if (s->avCodecCtx != NULL) {
362 avcodec_close ( s->avCodecCtx );
364 s->avCodecCtx = NULL;
365 if (s->avFormatCtx != NULL) {
366 avformat_close_input ( &(s->avFormatCtx) );
369 s->avFormatCtx = NULL;
373 #endif /* HAVE_SNDFILE */