Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_avcodec.c

    rde9178e r138cb1f  
    3535// determine whether we use libavformat from ffmpeg or from libav
    3636#define FFMPEG_LIBAVFORMAT (LIBAVFORMAT_VERSION_MICRO > 99 )
    37 // max_analyze_duration2 was used from ffmpeg libavformat 55.43.100 -> 57.2.100
     37// max_analyze_duration2 was used from ffmpeg libavformat 55.43.100 through 57.2.100
    3838#define FFMPEG_LIBAVFORMAT_MAX_DUR2 FFMPEG_LIBAVFORMAT && ( \
    3939      (LIBAVFORMAT_VERSION_MAJOR == 55 && LIBAVFORMAT_VERSION_MINOR >= 43) \
     
    9393};
    9494
    95 // create or re-create the context when _do or _do_multi is called
    96 void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s,
    97     uint_t multi);
    98 // actually read a frame
    99 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s,
    100     uint_t * read_samples);
     95// hack to create or re-create the context the first time _do or _do_multi is called
     96void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi);
     97void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples);
    10198
    10299uint_t aubio_source_avcodec_has_network_url(aubio_source_avcodec_t *s);
     
    115112
    116113
    117 aubio_source_avcodec_t * new_aubio_source_avcodec(const char_t * path,
    118     uint_t samplerate, uint_t hop_size) {
     114aubio_source_avcodec_t * new_aubio_source_avcodec(const char_t * path, uint_t samplerate, uint_t hop_size) {
    119115  aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
    120116  AVFormatContext *avFormatCtx = s->avFormatCtx;
     
    133129  }
    134130  if ((sint_t)samplerate < 0) {
    135     AUBIO_ERR("source_avcodec: Can not open %s with samplerate %d\n",
    136         path, samplerate);
     131    AUBIO_ERR("source_avcodec: Can not open %s with samplerate %d\n", path, samplerate);
    137132    goto beach;
    138133  }
    139134  if ((sint_t)hop_size <= 0) {
    140     AUBIO_ERR("source_avcodec: Can not open %s with hop_size %d\n",
    141         path, hop_size);
     135    AUBIO_ERR("source_avcodec: Can not open %s with hop_size %d\n", path, hop_size);
    142136    goto beach;
    143137  }
     
    179173    char errorstr[256];
    180174    av_strerror (err, errorstr, sizeof(errorstr));
    181     AUBIO_ERR("source_avcodec: Could not find stream information "
    182         "for %s (%s)\n", s->path, errorstr);
     175    AUBIO_ERR("source_avcodec: Could not find stream information " "for %s (%s)\n", s->path,
     176        errorstr);
    183177    goto beach;
    184178  }
     
    220214  avCodecCtx = avcodec_alloc_context3(codec);
    221215  if (!avCodecCtx) {
    222     AUBIO_ERR("source_avcodec: Failed to allocate the %s codec context "
    223         "for path %s\n", av_get_media_type_string(AVMEDIA_TYPE_AUDIO),
    224         s->path);
     216    AUBIO_ERR("source_avcodec: Failed to allocate the %s codec context for path %s\n",
     217        av_get_media_type_string(AVMEDIA_TYPE_AUDIO), s->path);
    225218    goto beach;
    226219  }
     
    237230  /* Copy codec parameters from input stream to output codec context */
    238231  if ((err = avcodec_parameters_to_context(avCodecCtx, codecpar)) < 0) {
    239     AUBIO_ERR("source_avcodec: Failed to copy %s codec parameters to "
    240         "decoder context for %s\n",
    241         av_get_media_type_string(AVMEDIA_TYPE_AUDIO), s->path);
     232    AUBIO_ERR("source_avcodec: Failed to copy %s codec parameters to decoder context for %s\n",
     233       av_get_media_type_string(AVMEDIA_TYPE_AUDIO), s->path);
    242234    goto beach;
    243235  }
     
    247239    char errorstr[256];
    248240    av_strerror (err, errorstr, sizeof(errorstr));
    249     AUBIO_ERR("source_avcodec: Could not load codec for %s (%s)\n", s->path,
    250         errorstr);
     241    AUBIO_ERR("source_avcodec: Could not load codec for %s (%s)\n", s->path, errorstr);
    251242    goto beach;
    252243  }
     
    275266
    276267  /* allocate output for avr */
    277   s->output = (smpl_t *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE
    278       * sizeof(smpl_t));
     268  s->output = (smpl_t *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(smpl_t));
    279269
    280270  s->read_samples = 0;
     
    304294}
    305295
    306 void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s,
    307     uint_t multi)
    308 {
     296void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi) {
    309297  // create or reset resampler to/from mono/multi-channel
    310298  if ( (multi != s->multi) || (s->avr == NULL) ) {
     
    321309#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    322310
    323     av_opt_set_int(avr, "in_channel_layout",  input_layout,              0);
    324     av_opt_set_int(avr, "out_channel_layout", output_layout,             0);
    325     av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,       0);
    326     av_opt_set_int(avr, "out_sample_rate",    s->samplerate,             0);
     311    av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
     312    av_opt_set_int(avr, "out_channel_layout", output_layout,          0);
     313    av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,    0);
     314    av_opt_set_int(avr, "out_sample_rate",    s->samplerate,          0);
    327315    av_opt_set_int(avr, "in_sample_fmt",      s->avCodecCtx->sample_fmt, 0);
    328316#if HAVE_AUBIO_DOUBLE
    329     av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_DBL,         0);
    330 #else
    331     av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLT,         0);
     317    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_DBL,      0);
     318#else
     319    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLT,      0);
    332320#endif
    333321    // TODO: use planar?
     
    341329      char errorstr[256];
    342330      av_strerror (err, errorstr, sizeof(errorstr));
    343       AUBIO_ERR("source_avcodec: Could not open resampling context"
    344          " for %s (%s)\n", s->path, errorstr);
     331      AUBIO_ERR("source_avcodec: Could not open resampling context for %s (%s)\n",
     332          s->path, errorstr);
    345333      return;
    346334    }
     
    359347}
    360348
    361 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s,
    362     uint_t * read_samples)
    363 {
     349void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
    364350  AVFormatContext *avFormatCtx = s->avFormatCtx;
    365351  AVCodecContext *avCodecCtx = s->avCodecCtx;
     
    402388      char errorstr[256];
    403389      av_strerror (err, errorstr, sizeof(errorstr));
    404       AUBIO_ERR("source_avcodec: could not read frame in %s (%s)\n",
    405           s->path, errorstr);
     390      AUBIO_ERR("source_avcodec: could not read frame in %s (%s)\n", s->path, errorstr);
    406391      s->eof = 1;
    407392      goto beach;
     
    421406  if (ret < 0) {
    422407    if (ret == AVERROR(EAGAIN)) {
    423       //AUBIO_WRN("source_avcodec: output is not available right now - "
    424       //    "user must try to send new input\n");
     408      //AUBIO_WRN("source_avcodec: output is not available right now - user must try to send new input\n");
    425409      goto beach;
    426410    } else if (ret == AVERROR_EOF) {
    427       AUBIO_WRN("source_avcodec: the decoder has been fully flushed, "
    428           "and there will be no more output frames\n");
     411      AUBIO_WRN("source_avcodec: the decoder has been fully flushed, and there will be no more output frames\n");
    429412    } else {
    430413      AUBIO_ERR("source_avcodec: decoding errors on %s\n", s->path);
     
    441424#endif
    442425  if (got_frame == 0) {
    443     AUBIO_WRN("source_avcodec: did not get a frame when reading %s\n",
    444         s->path);
     426    AUBIO_WRN("source_avcodec: did not get a frame when reading %s\n", s->path);
    445427    goto beach;
    446428  }
     
    449431  if (avFrame->channels != (sint_t)s->input_channels) {
    450432    AUBIO_WRN ("source_avcodec: trying to read from %d channel(s),"
    451         "but configured for %d; is '%s' corrupt?\n",
    452         avFrame->channels, s->input_channels, s->path);
    453     goto beach;
    454   }
     433        "but configured for %d; is '%s' corrupt?\n", avFrame->channels,
     434        s->input_channels, s->path);
     435    goto beach;
     436  }
     437#else
     438#warning "avutil < 53 is deprecated, crashes might occur on corrupt files"
    455439#endif
    456440
     
    473457#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    474458  if (out_samples <= 0) {
    475     AUBIO_WRN("source_avcodec: no sample found while converting frame (%s)\n",
    476         s->path);
     459    AUBIO_WRN("source_avcodec: no sample found while converting frame (%s)\n", s->path);
    477460    goto beach;
    478461  }
     
    492475}
    493476
    494 void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data,
    495     uint_t * read) {
     477void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
    496478  uint_t i;
    497479  uint_t end = 0;
     
    525507}
    526508
    527 void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s,
    528     fmat_t * read_data, uint_t * read) {
     509void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
    529510  uint_t i,j;
    530511  uint_t end = 0;
     
    572553
    573554uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
    574   int64_t resampled_pos =
    575     (uint_t)ROUND(pos * (s->input_samplerate * 1. / s->samplerate));
     555  int64_t resampled_pos = (uint_t)ROUND(pos * (s->input_samplerate * 1. / s->samplerate));
    576556  int64_t min_ts = MAX(resampled_pos - 2000, 0);
    577557  int64_t max_ts = MIN(resampled_pos + 2000, INT64_MAX);
     
    581561    ret = AUBIO_OK;
    582562  } else {
    583     AUBIO_ERR("source_avcodec: failed seeking in %s (file not opened?)",
    584         s->path);
     563    AUBIO_ERR("source_avcodec: failed seeking in %s (file not opened?)", s->path);
    585564    return ret;
    586565  }
     
    593572      min_ts, resampled_pos, max_ts, seek_flags);
    594573  if (ret < 0) {
    595     AUBIO_ERR("source_avcodec: failed seeking to %d in file %s",
    596         pos, s->path);
     574    AUBIO_ERR("source_avcodec: failed seeking to %d in file %s", pos, s->path);
    597575  }
    598576  // reset read status
Note: See TracChangeset for help on using the changeset viewer.