Changeset ba67cb6


Ignore:
Timestamp:
Mar 17, 2017, 12:57:31 AM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, sampler
Children:
c3b3b84
Parents:
d82e7a4
Message:

src/io/source_avcodec.c: add libswresample

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_avcodec.c

    rd82e7a4 rba67cb6  
    2525#include <libavcodec/avcodec.h>
    2626#include <libavformat/avformat.h>
     27#if defined(HAVE_SWRESAMPLE)
     28#include <libswresample/swresample.h>
     29#elif defined(HAVE_AVRESAMPLE)
    2730#include <libavresample/avresample.h>
     31#endif
    2832#include <libavutil/opt.h>
    2933#include <stdlib.h>
     
    6973  AVFrame *avFrame;
    7074  AVPacket avPacket;
     75#ifdef HAVE_AVRESAMPLE
    7176  AVAudioResampleContext *avr;
     77#elif defined(HAVE_SWRESAMPLE)
     78  SwrContext *avr;
     79#endif
    7280  smpl_t *output;
    7381  uint_t read_samples;
     
    277285    uint_t output_channels = multi ? s->input_channels : 1;
    278286    int64_t output_layout = av_get_default_channel_layout(output_channels);
     287#ifdef HAVE_AVRESAMPLE
    279288    AVAudioResampleContext *avr = avresample_alloc_context();
    280289    AVAudioResampleContext *oldavr = s->avr;
     290#elif defined(HAVE_SWRESAMPLE)
     291    SwrContext *avr = swr_alloc();
     292    SwrContext *oldavr = s->avr;
     293#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    281294
    282295    av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
     
    293306    //av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLTP,      0);
    294307    int err;
     308#ifdef HAVE_AVRESAMPLE
    295309    if ( ( err = avresample_open(avr) ) < 0) {
     310#elif defined(HAVE_SWRESAMPLE)
     311    if ( ( err = swr_init(avr) ) < 0) {
     312#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    296313      char errorstr[256];
    297314      av_strerror (err, errorstr, sizeof(errorstr));
     
    303320    s->avr = avr;
    304321    if (oldavr != NULL) {
     322#ifdef HAVE_AVRESAMPLE
    305323      avresample_close( oldavr );
     324#elif defined(HAVE_SWRESAMPLE)
     325      swr_close ( oldavr );
     326#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    306327      av_free ( oldavr );
    307328      oldavr = NULL;
     
    317338  AVPacket avPacket = s->avPacket;
    318339  av_init_packet (&avPacket);
     340#ifdef HAVE_AVRESAMPLE
    319341  AVAudioResampleContext *avr = s->avr;
     342#elif defined(HAVE_SWRESAMPLE)
     343  SwrContext *avr = s->avr;
     344#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    320345  smpl_t *output = s->output;
    321346  *read_samples = 0;
     
    370395  }
    371396
     397#ifdef HAVE_AVRESAMPLE
    372398  int in_linesize = 0;
    373399  av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
     
    379405        (uint8_t **)&output, out_linesize, max_out_samples,
    380406        (uint8_t **)avFrame->data, in_linesize, in_samples);
     407#elif defined(HAVE_SWRESAMPLE)
     408  int in_samples = avFrame->nb_samples;
     409  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE / avCodecCtx->channels;
     410  int out_samples = swr_convert( avr,
     411      (uint8_t **)&output, max_out_samples,
     412      (const uint8_t **)avFrame->data, in_samples);
     413#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    381414  if (out_samples <= 0) {
    382415    AUBIO_WRN("source_avcodec: no sample found while converting frame (%s)\n", s->path);
     
    390423  s->avCodecCtx = avCodecCtx;
    391424  s->avFrame = avFrame;
     425#if defined(HAVE_AVRESAMPLE) || defined(HAVE_SWRESAMPLE)
    392426  s->avr = avr;
     427#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    393428  s->output = output;
    394429
     
    499534  s->read_index = 0;
    500535  s->read_samples = 0;
     536#ifdef HAVE_AVRESAMPLE
    501537  // reset the AVAudioResampleContext
    502538  avresample_close(s->avr);
    503539  avresample_open(s->avr);
     540#elif defined(HAVE_SWRESAMPLE)
     541  swr_close(s->avr);
     542  swr_init(s->avr);
     543#endif
    504544  return ret;
    505545}
     
    515555uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) {
    516556  if (s->avr != NULL) {
     557#ifdef HAVE_AVRESAMPLE
    517558    avresample_close( s->avr );
     559#elif defined(HAVE_SWRESAMPLE)
     560    swr_close ( s->avr );
     561#endif
    518562    av_free ( s->avr );
    519563  }
Note: See TracChangeset for help on using the changeset viewer.