source: src/io/source_avcodec.c @ b294b3e

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since b294b3e was b294b3e, checked in by Paul Brossier <piem@piem.org>, 10 years ago

src/io/source_avcodec.c: check new_ arguments are valid

  • Property mode set to 100644
File size: 9.8 KB
Line 
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
38#define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
39
40struct _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;
47  uint_t input_samplerate;
48  uint_t input_channels;
49
50  // avcodec stuff
51  AVFormatContext *avFormatCtx;
52  AVCodecContext *avCodecCtx;
53  AVFrame *avFrame;
54  AVAudioResampleContext *avr;
55  float *output;
56  uint_t read_samples;
57  uint_t read_index;
58  sint_t selected_stream;
59  uint_t eof;
60};
61
62aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplerate, uint_t hop_size) {
63  aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
64  int err;
65  if (path == NULL) {
66    AUBIO_ERR("Aborted opening null path\n");
67    goto beach;
68  }
69  if ((sint_t)samplerate < 0) {
70    AUBIO_ERR("Can not open %s with samplerate %d\n", path, samplerate);
71    goto beach;
72  }
73  if ((sint_t)hop_size <= 0) {
74    AUBIO_ERR("Can not open %s with hop_size %d\n", path, hop_size);
75    goto beach;
76  }
77
78  s->hop_size = hop_size;
79  s->channels = 1;
80  s->path = path;
81
82  // register all formats and codecs
83  av_register_all();
84
85  // if path[0] != '/'
86  //avformat_network_init();
87
88  // try opening the file and get some info about it
89  AVFormatContext *avFormatCtx = s->avFormatCtx;
90  avFormatCtx = NULL;
91  if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
92    char errorstr[256];
93    av_strerror (err, errorstr, sizeof(errorstr));
94    AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr);
95    goto beach;
96  }
97
98  // try to make sure max_analyze_duration is big enough for most songs
99  avFormatCtx->max_analyze_duration *= 100;
100
101  // retrieve stream information
102  if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
103    char errorstr[256];
104    av_strerror (err, errorstr, sizeof(errorstr));
105    AUBIO_ERR("Could not find stream information " "for %s (%s)\n", s->path,
106        errorstr);
107    goto beach;
108  }
109
110  // dump information about file onto standard error
111  //av_dump_format(avFormatCtx, 0, s->path, 0);
112
113  // look for the first audio stream
114  uint_t i;
115  sint_t selected_stream = -1;
116  for (i = 0; i < avFormatCtx->nb_streams; i++) {
117    if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
118      if (selected_stream == -1) {
119        selected_stream = i;
120      } else {
121        AUBIO_WRN("More than one audio stream in %s, "
122            "taking the first one\n", s->path);
123      }
124    }
125  }
126  if (selected_stream == -1) {
127    AUBIO_ERR("No audio stream in %s\n", s->path);
128    goto beach;
129  }
130  //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
131  s->selected_stream = selected_stream;
132
133  AVCodecContext *avCodecCtx = s->avCodecCtx;
134  avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
135  AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id);
136  if (codec == NULL) {
137    AUBIO_ERR("Could not find decoder for %s", s->path);
138    goto beach;
139  }
140
141  if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
142    char errorstr[256];
143    av_strerror (err, errorstr, sizeof(errorstr));
144    AUBIO_ERR("Could not load codec for %s (%s)\n", s->path, errorstr);
145    goto beach;
146  }
147
148  /* get input specs */
149  s->input_samplerate = avCodecCtx->sample_rate;
150  s->input_channels   = avCodecCtx->channels;
151  //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate);
152  //AUBIO_DBG("input_channels: %d\n", s->input_channels);
153
154  if (samplerate == 0) {
155    samplerate = s->input_samplerate;
156    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
157  }
158  s->samplerate = samplerate;
159
160  int64_t input_layout = av_get_default_channel_layout(s->input_channels);
161  int64_t mono_layout = av_get_default_channel_layout(1);
162
163  AVAudioResampleContext *avr = s->avr;
164  avr = avresample_alloc_context();
165  av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
166  av_opt_set_int(avr, "out_channel_layout", mono_layout,            0);
167  av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,    0);
168  av_opt_set_int(avr, "out_sample_rate",    s->samplerate,          0);
169  av_opt_set_int(avr, "in_sample_fmt",      avCodecCtx->sample_fmt, 0);
170  av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLTP,     0);
171  if ( ( err = avresample_open(avr) ) < 0) {
172    char errorstr[256];
173    av_strerror (err, errorstr, sizeof(errorstr));
174    AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n",
175        s->path, errorstr);
176    goto beach;
177  }
178
179  AVFrame *avFrame = s->avFrame;
180  avFrame = avcodec_alloc_frame();
181  if (!avFrame) {
182    AUBIO_ERR("Could not allocate frame for (%s)\n", s->path);
183  }
184
185  /* allocate output for avr */
186  s->output = (float *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(float));
187
188  s->read_samples = 0;
189  s->read_index = 0;
190
191  s->avFormatCtx = avFormatCtx;
192  s->avCodecCtx = avCodecCtx;
193  s->avFrame = avFrame;
194  s->avr = avr;
195
196  s->eof = 0;
197
198  //av_log_set_level(AV_LOG_QUIET);
199
200  return s;
201
202beach:
203  AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
204      s->path, s->samplerate, s->hop_size);
205  del_aubio_source_avcodec(s);
206  return NULL;
207}
208
209void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
210  AVFormatContext *avFormatCtx = s->avFormatCtx;
211  AVCodecContext *avCodecCtx = s->avCodecCtx;
212  AVFrame *avFrame = s->avFrame;
213  AVPacket avPacket;
214  av_init_packet (&avPacket);
215  AVAudioResampleContext *avr = s->avr;
216  float *output = s->output;
217  *read_samples = 0;
218
219  do
220  {
221    int err = av_read_frame (avFormatCtx, &avPacket);
222    if (err == AVERROR_EOF) {
223      s->eof = 1;
224      goto beach;
225    }
226    if (err != 0) {
227      char errorstr[256];
228      av_strerror (err, errorstr, sizeof(errorstr));
229      AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr);
230      goto beach;
231    }
232  } while (avPacket.stream_index != s->selected_stream);
233
234  int got_frame = 0;
235  int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
236
237  if (len < 0) {
238    AUBIO_ERR("Error while decoding %s\n", s->path);
239    goto beach;
240  }
241  if (got_frame == 0) {
242    //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
243    goto beach;
244  }
245
246  int in_linesize = 0;
247  av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
248      avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
249  //AUBIO_WRN("Got data_size %d in_linesize %d frame for (%s)\n",
250  //    data_size, in_linesize, s->path);
251
252  int in_samples = avFrame->nb_samples;
253  int out_linesize = 0;
254  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
255  int out_samples = avresample_convert ( avr,
256        (uint8_t **)&output, out_linesize, max_out_samples,
257        (uint8_t **)avFrame->data, in_linesize, in_samples);
258  if (out_samples <= 0) {
259    AUBIO_ERR("No sample found while converting frame (%s)\n", s->path);
260    goto beach;
261  }
262
263  *read_samples = out_samples;
264
265beach:
266  s->avFormatCtx = avFormatCtx;
267  s->avCodecCtx = avCodecCtx;
268  s->avFrame = avFrame;
269  s->avr = avr;
270  s->output = output;
271
272  av_free_packet(&avPacket);
273}
274
275void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
276  uint_t i;
277  uint_t end = 0;
278  uint_t total_wrote = 0;
279  while (total_wrote < s->hop_size) {
280    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
281    for (i = 0; i < end; i++) {
282      read_data->data[i + total_wrote] = s->output[i + s->read_index];
283    }
284    total_wrote += end;
285    if (total_wrote < s->hop_size) {
286      uint_t avcodec_read = 0;
287      aubio_source_avcodec_readframe(s, &avcodec_read);
288      s->read_samples = avcodec_read;
289      s->read_index = 0;
290      if (s->eof) {
291        break;
292      }
293    } else {
294      s->read_index += end;
295    }
296  }
297  if (total_wrote < s->hop_size) {
298    for (i = end; i < s->hop_size; i++) {
299      read_data->data[i] = 0.;
300    }
301  }
302  *read = total_wrote;
303}
304
305void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
306  //uint_t i,j, input_channels = s->input_channels;
307}
308
309uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) {
310  return s->samplerate;
311}
312
313uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) {
314  return s->input_channels;
315}
316
317uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
318  //uint_t resampled_pos = (uint_t)ROUND(pos * s->input_samplerate * 1. / s->samplerate);
319  return 0; //sf_seek (s->handle, resampled_pos, SEEK_SET);
320}
321
322void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
323  if (!s) return;
324  if (s->output != NULL) {
325    av_free(s->output);
326  }
327  if (s->avr != NULL) {
328    avresample_close( s->avr );
329    av_free ( s->avr );
330  }
331  s->avr = NULL;
332  if (s->avFrame != NULL) {
333    avcodec_free_frame( &(s->avFrame) );
334  }
335  s->avFrame = NULL;
336  if (s->avCodecCtx != NULL) {
337    avcodec_close ( s->avCodecCtx );
338  }
339  s->avCodecCtx = NULL;
340  if (s->avFormatCtx != NULL) {
341    avformat_close_input ( &(s->avFormatCtx) );
342  }
343  s->avFrame = NULL;
344  s->avFormatCtx = NULL;
345  AUBIO_FREE(s);
346}
347
348#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.