source: src/io/source_avcodec.c @ 3da8187

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

src/io/source_avcodec.c: add _multi, building AVAudioResampleContext the first time _do or _do_multi is run

  • Property mode set to 100644
File size: 11.4 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  uint_t multi;
61};
62
63/* convenience function to recreate the AVAudioResampleContext the first time _do
64 * or _do_multi is called */
65void aubio_source_avcodec_set_multi(aubio_source_avcodec_t * s, uint_t multi);
66
67aubio_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");
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;
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
90  // if path[0] != '/'
91  //avformat_network_init();
92
93  // try opening the file and get some info about it
94  AVFormatContext *avFormatCtx = s->avFormatCtx;
95  avFormatCtx = NULL;
96  if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
97    char errorstr[256];
98    av_strerror (err, errorstr, sizeof(errorstr));
99    AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr);
100    goto beach;
101  }
102
103  // try to make sure max_analyze_duration is big enough for most songs
104  avFormatCtx->max_analyze_duration *= 100;
105
106  // retrieve stream information
107  if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
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);
112    goto beach;
113  }
114
115  // dump information about file onto standard error
116  //av_dump_format(avFormatCtx, 0, s->path, 0);
117
118  // look for the first audio stream
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 {
126        AUBIO_WRN("More than one audio stream in %s, "
127            "taking the first one\n", s->path);
128      }
129    }
130  }
131  if (selected_stream == -1) {
132    AUBIO_ERR("No audio stream in %s\n", s->path);
133    goto beach;
134  }
135  //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
136  s->selected_stream = selected_stream;
137
138  AVCodecContext *avCodecCtx = s->avCodecCtx;
139  avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
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) {
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);
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
171  /* allocate output for avr */
172  s->output = (float *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(float));
173
174  s->read_samples = 0;
175  s->read_index = 0;
176
177  s->avFormatCtx = avFormatCtx;
178  s->avCodecCtx = avCodecCtx;
179  s->avFrame = avFrame;
180
181  // default to mono output
182  aubio_source_avcodec_set_multi(s, 0);
183
184  s->eof = 0;
185  s->multi = 0;
186
187  //av_log_set_level(AV_LOG_QUIET);
188
189  return s;
190
191beach:
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);
195  return NULL;
196}
197
198void 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;
202  AVPacket avPacket;
203  av_init_packet (&avPacket);
204  AVAudioResampleContext *avr = s->avr;
205  float *output = s->output;
206  *read_samples = 0;
207
208  do
209  {
210    int err = av_read_frame (avFormatCtx, &avPacket);
211    if (err == AVERROR_EOF) {
212      s->eof = 1;
213      goto beach;
214    }
215    if (err != 0) {
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;
220    }
221  } while (avPacket.stream_index != s->selected_stream);
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);
228    goto beach;
229  }
230  if (got_frame == 0) {
231    //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
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);
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);
246    goto beach;
247  }
248
249  *read_samples = out_samples;
250
251beach:
252  s->avFormatCtx = avFormatCtx;
253  s->avCodecCtx = avCodecCtx;
254  s->avFrame = avFrame;
255  s->avr = avr;
256  s->output = output;
257
258  av_free_packet(&avPacket);
259}
260
261void 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
297void 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);
299  uint_t i;
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++) {
305      read_data->data[i + total_wrote] = s->output[i + s->read_index];
306    }
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;
315      }
316    } else {
317      s->read_index += end;
318    }
319  }
320  if (total_wrote < s->hop_size) {
321    for (i = end; i < s->hop_size; i++) {
322      read_data->data[i] = 0.;
323    }
324  }
325  *read = total_wrote;
326}
327
328void 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);
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;
362}
363
364uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) {
365  return s->samplerate;
366}
367
368uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) {
369  return s->input_channels;
370}
371
372uint_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);
376}
377
378void 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 */
Note: See TracBrowser for help on using the repository browser.