source: src/io/source_avcodec.c @ 61ecd1a

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

src/io/source_avcodec.c: rename hack to _reset_resampler, move up for clarity

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