source: src/io/source_avcodec.c @ eadd8d5

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

src/io/source_avcodec.c: rewrite _do, add eof, remove debug output

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