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

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

src/io/source_avcodec.c: let avresample do the conversion to float

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