source: src/io/source_avcodec.c @ dba9b33

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

src/io/source_avcodec.c: update to libav10, libavcodec 55.34.1

  • Property mode set to 100644
File size: 12.3 KB
RevLine 
[ba0ba10]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
[549928e]24#ifdef HAVE_LIBAV
[ba0ba10]25
26#include <libavcodec/avcodec.h>
27#include <libavformat/avformat.h>
28#include <libavresample/avresample.h>
29#include <libavutil/opt.h>
30#include <stdlib.h>
31
32#include "aubio_priv.h"
33#include "fvec.h"
34#include "fmat.h"
35#include "source_avcodec.h"
36
[0af9003]37#define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
[ba0ba10]38
39struct _aubio_source_avcodec_t {
40  uint_t hop_size;
41  uint_t samplerate;
42  uint_t channels;
43
44  // some data about the file
45  char_t *path;
[d3b9fe4]46  uint_t input_samplerate;
47  uint_t input_channels;
[ba0ba10]48
49  // avcodec stuff
50  AVFormatContext *avFormatCtx;
51  AVCodecContext *avCodecCtx;
52  AVFrame *avFrame;
53  AVAudioResampleContext *avr;
[3d5cddf]54  float *output;
[d3b9fe4]55  uint_t read_samples;
56  uint_t read_index;
[7760b40]57  sint_t selected_stream;
[eadd8d5]58  uint_t eof;
[fcd963a]59  uint_t multi;
[ba0ba10]60};
61
[61ecd1a]62// hack to create or re-create the context the first time _do or _do_multi is called
63void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi);
[029bf4e]64void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples);
[fcd963a]65
[ba0ba10]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) {
[491e6ea]70    AUBIO_ERR("source_avcodec: Aborted opening null path\n");
[b294b3e]71    goto beach;
72  }
73  if ((sint_t)samplerate < 0) {
[491e6ea]74    AUBIO_ERR("source_avcodec: Can not open %s with samplerate %d\n", path, samplerate);
[b294b3e]75    goto beach;
76  }
77  if ((sint_t)hop_size <= 0) {
[491e6ea]78    AUBIO_ERR("source_avcodec: Can not open %s with hop_size %d\n", path, hop_size);
[b294b3e]79    goto beach;
[ba0ba10]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
[eadd8d5]89  // if path[0] != '/'
90  //avformat_network_init();
91
[d75e2d5]92  // try opening the file and get some info about it
[ba0ba10]93  AVFormatContext *avFormatCtx = s->avFormatCtx;
94  avFormatCtx = NULL;
95  if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
[0af9003]96    char errorstr[256];
97    av_strerror (err, errorstr, sizeof(errorstr));
[491e6ea]98    AUBIO_ERR("source_avcodec: Failed opening %s (%s)\n", s->path, errorstr);
[ba0ba10]99    goto beach;
100  }
101
[6abb4de]102  // try to make sure max_analyze_duration is big enough for most songs
103  avFormatCtx->max_analyze_duration *= 100;
104
[ba0ba10]105  // retrieve stream information
106  if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
[0af9003]107    char errorstr[256];
108    av_strerror (err, errorstr, sizeof(errorstr));
[491e6ea]109    AUBIO_ERR("source_avcodec: Could not find stream information " "for %s (%s)\n", s->path,
[0af9003]110        errorstr);
[ba0ba10]111    goto beach;
112  }
113
[1fe3ac2]114  // dump information about file onto standard error
[ba0ba10]115  //av_dump_format(avFormatCtx, 0, s->path, 0);
116
[1fe3ac2]117  // look for the first audio stream
[ba0ba10]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 {
[491e6ea]125        AUBIO_WRN("source_avcodec: More than one audio stream in %s, "
[1fe3ac2]126            "taking the first one\n", s->path);
[ba0ba10]127      }
128    }
129  }
130  if (selected_stream == -1) {
[491e6ea]131    AUBIO_ERR("source_avcodec: No audio stream in %s\n", s->path);
[ba0ba10]132    goto beach;
[d3b9fe4]133  }
[eadd8d5]134  //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
[7760b40]135  s->selected_stream = selected_stream;
[ba0ba10]136
137  AVCodecContext *avCodecCtx = s->avCodecCtx;
[d3b9fe4]138  avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
[ba0ba10]139  AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id);
140  if (codec == NULL) {
[491e6ea]141    AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path);
[ba0ba10]142    goto beach;
143  }
144
145  if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
[0af9003]146    char errorstr[256];
147    av_strerror (err, errorstr, sizeof(errorstr));
[491e6ea]148    AUBIO_ERR("source_avcodec: Could not load codec for %s (%s)\n", s->path, errorstr);
[ba0ba10]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
[018e511]164  if (s->samplerate >  s->input_samplerate) {
[491e6ea]165    AUBIO_WRN("source_avcodec: upsampling %s from %d to %d\n", s->path,
[018e511]166        s->input_samplerate, s->samplerate);
167  }
168
[ba0ba10]169  AVFrame *avFrame = s->avFrame;
[cd4c997]170  avFrame = av_frame_alloc();
[ba0ba10]171  if (!avFrame) {
[491e6ea]172    AUBIO_ERR("source_avcodec: Could not allocate frame for (%s)\n", s->path);
[ba0ba10]173  }
174
[d3b9fe4]175  /* allocate output for avr */
[0af9003]176  s->output = (float *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(float));
[ba0ba10]177
178  s->read_samples = 0;
179  s->read_index = 0;
180
181  s->avFormatCtx = avFormatCtx;
182  s->avCodecCtx = avCodecCtx;
183  s->avFrame = avFrame;
[fcd963a]184
185  // default to mono output
[61ecd1a]186  aubio_source_avcodec_reset_resampler(s, 0);
[ba0ba10]187
[eadd8d5]188  s->eof = 0;
[fcd963a]189  s->multi = 0;
[eadd8d5]190
[ba0ba10]191  //av_log_set_level(AV_LOG_QUIET);
192
193  return s;
194
195beach:
[fcd963a]196  //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
197  //    s->path, s->samplerate, s->hop_size);
[ba0ba10]198  del_aubio_source_avcodec(s);
199  return NULL;
200}
201
[61ecd1a]202void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi) {
203  if ( (multi != s->multi) || (s->avr == NULL) ) {
204    int64_t input_layout = av_get_default_channel_layout(s->input_channels);
205    uint_t output_channels = multi ? s->input_channels : 1;
206    int64_t output_layout = av_get_default_channel_layout(output_channels);
207    if (s->avr != NULL) {
208      avresample_close( s->avr );
209      av_free ( s->avr );
210      s->avr = NULL;
211    }
212    AVAudioResampleContext *avr = s->avr;
213    avr = avresample_alloc_context();
214
215    av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
216    av_opt_set_int(avr, "out_channel_layout", output_layout,          0);
217    av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,    0);
218    av_opt_set_int(avr, "out_sample_rate",    s->samplerate,          0);
219    av_opt_set_int(avr, "in_sample_fmt",      s->avCodecCtx->sample_fmt, 0);
220    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLT,      0);
221    int err;
222    if ( ( err = avresample_open(avr) ) < 0) {
223      char errorstr[256];
224      av_strerror (err, errorstr, sizeof(errorstr));
[491e6ea]225      AUBIO_ERR("source_avcodec: Could not open AVAudioResampleContext for %s (%s)\n",
[61ecd1a]226          s->path, errorstr);
227      //goto beach;
228      return;
229    }
230    s->avr = avr;
231    s->multi = multi;
232  }
233}
234
[d3b9fe4]235void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
[ba0ba10]236  AVFormatContext *avFormatCtx = s->avFormatCtx;
237  AVCodecContext *avCodecCtx = s->avCodecCtx;
238  AVFrame *avFrame = s->avFrame;
[f3b93c6]239  AVPacket avPacket;
240  av_init_packet (&avPacket);
[ba0ba10]241  AVAudioResampleContext *avr = s->avr;
[3d5cddf]242  float *output = s->output;
[0af9003]243  *read_samples = 0;
[ba0ba10]244
[7760b40]245  do
246  {
247    int err = av_read_frame (avFormatCtx, &avPacket);
[0af9003]248    if (err == AVERROR_EOF) {
249      s->eof = 1;
250      goto beach;
251    }
[7760b40]252    if (err != 0) {
[0af9003]253      char errorstr[256];
254      av_strerror (err, errorstr, sizeof(errorstr));
255      AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr);
256      goto beach;
[7760b40]257    }
258  } while (avPacket.stream_index != s->selected_stream);
[ba0ba10]259
260  int got_frame = 0;
261  int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
262
263  if (len < 0) {
264    AUBIO_ERR("Error while decoding %s\n", s->path);
[0af9003]265    goto beach;
[ba0ba10]266  }
267  if (got_frame == 0) {
[eadd8d5]268    //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
[0af9003]269    goto beach;
270  }
271
272  int in_linesize = 0;
273  av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
274      avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
[ba0ba10]275  int in_samples = avFrame->nb_samples;
[0af9003]276  int out_linesize = 0;
277  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
[0044b43]278  int out_samples = avresample_convert ( avr,
[0af9003]279        (uint8_t **)&output, out_linesize, max_out_samples,
280        (uint8_t **)avFrame->data, in_linesize, in_samples);
281  if (out_samples <= 0) {
[5c6acbb2]282    //AUBIO_ERR("No sample found while converting frame (%s)\n", s->path);
[0af9003]283    goto beach;
[ba0ba10]284  }
285
[0af9003]286  *read_samples = out_samples;
287
288beach:
[ba0ba10]289  s->avFormatCtx = avFormatCtx;
290  s->avCodecCtx = avCodecCtx;
291  s->avFrame = avFrame;
292  s->avr = avr;
293  s->output = output;
294
[0af9003]295  av_free_packet(&avPacket);
[ba0ba10]296}
297
298void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
[61ecd1a]299  if (s->multi == 1) aubio_source_avcodec_reset_resampler(s, 0);
[ba0ba10]300  uint_t i;
[eadd8d5]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++) {
[3d5cddf]306      read_data->data[i + total_wrote] = s->output[i + s->read_index];
[ba0ba10]307    }
[eadd8d5]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;
[ba0ba10]316      }
[eadd8d5]317    } else {
318      s->read_index += end;
[ba0ba10]319    }
[eadd8d5]320  }
321  if (total_wrote < s->hop_size) {
322    for (i = end; i < s->hop_size; i++) {
323      read_data->data[i] = 0.;
[ba0ba10]324    }
325  }
[eadd8d5]326  *read = total_wrote;
[ba0ba10]327}
328
329void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
[61ecd1a]330  if (s->multi == 0) aubio_source_avcodec_reset_resampler(s, 1);
[fcd963a]331  uint_t i,j;
332  uint_t end = 0;
333  uint_t total_wrote = 0;
334  while (total_wrote < s->hop_size) {
335    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
336    for (j = 0; j < read_data->height; j++) {
337      for (i = 0; i < end; i++) {
338        read_data->data[j][i + total_wrote] =
339          s->output[(i + s->read_index) * s->input_channels + j];
340      }
341    }
342    total_wrote += end;
343    if (total_wrote < s->hop_size) {
344      uint_t avcodec_read = 0;
345      aubio_source_avcodec_readframe(s, &avcodec_read);
346      s->read_samples = avcodec_read;
347      s->read_index = 0;
348      if (s->eof) {
349        break;
350      }
351    } else {
352      s->read_index += end;
353    }
354  }
355  if (total_wrote < s->hop_size) {
356    for (j = 0; j < read_data->height; j++) {
357      for (i = end; i < s->hop_size; i++) {
358        read_data->data[j][i] = 0.;
359      }
360    }
361  }
362  *read = total_wrote;
[ba0ba10]363}
364
365uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) {
366  return s->samplerate;
367}
368
369uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) {
370  return s->input_channels;
371}
372
373uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
[50bb325]374  int64_t resampled_pos = (uint_t)ROUND(pos * (s->input_samplerate * 1. / s->samplerate));
375  int64_t min_ts = MAX(resampled_pos - 2000, 0);
376  int64_t max_ts = MIN(resampled_pos + 2000, INT64_MAX);
377  int seek_flags = AVSEEK_FLAG_FRAME | AVSEEK_FLAG_ANY;
378  int ret = avformat_seek_file(s->avFormatCtx, s->selected_stream,
379      min_ts, resampled_pos, max_ts, seek_flags);
380  if (ret < 0) {
381    AUBIO_ERR("Failed seeking to %d in file %s", pos, s->path);
382  }
383  // reset read status
384  s->eof = 0;
385  s->read_index = 0;
386  s->read_samples = 0;
387  // reset the AVAudioResampleContext
388  avresample_close(s->avr);
389  avresample_open(s->avr);
390  return ret;
[ba0ba10]391}
392
[422452b]393uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) {
[ba0ba10]394  if (s->avr != NULL) {
395    avresample_close( s->avr );
396    av_free ( s->avr );
397  }
398  s->avr = NULL;
399  if (s->avCodecCtx != NULL) {
400    avcodec_close ( s->avCodecCtx );
401  }
402  s->avCodecCtx = NULL;
403  if (s->avFormatCtx != NULL) {
404    avformat_close_input ( &(s->avFormatCtx) );
405  }
406  s->avFormatCtx = NULL;
[422452b]407  return AUBIO_OK;
408}
409
410void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
411  if (!s) return;
412  aubio_source_avcodec_close(s);
413  if (s->output != NULL) {
414    av_free(s->output);
415  }
416  s->output = NULL;
417  if (s->avFrame != NULL) {
[cd4c997]418    av_frame_free( &(s->avFrame) );
[422452b]419  }
420  s->avFrame = NULL;
[ba0ba10]421  AUBIO_FREE(s);
422}
423
[549928e]424#endif /* HAVE_LIBAV */
Note: See TracBrowser for help on using the repository browser.