source: src/io/source_avcodec.c @ d3b9fe4

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since d3b9fe4 was d3b9fe4, checked in by Paul Brossier <piem@piem.org>, 6 years ago

src/io/source_avcodec.c: tidying up

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