Changeset 0af9003 for src


Ignore:
Timestamp:
Dec 6, 2013, 6:58:32 AM (11 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, pitchshift, sampler, timestretch, yinfft+
Children:
b294b3e
Parents:
1fe3ac2
Message:

src/io/source_avcodec.c: simplify error parsing, compute line size

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_avcodec.c

    r1fe3ac2 r0af9003  
    3636#include "source_avcodec.h"
    3737
    38 #define AUBIO_AVCODEC_MIN_BUFFER_SIZE FF_MIN_BUFFER_SIZE
     38#define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
    3939
    4040struct _aubio_source_avcodec_t {
     
    8383  avFormatCtx = NULL;
    8484  if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
    85     uint8_t errorstr_len = 128;
    86     char errorstr[errorstr_len];
    87     if (av_strerror (err, errorstr, errorstr_len) == 0) {
    88       AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr);
    89     } else {
    90       AUBIO_ERR("Failed opening %s (unknown error)\n", s->path);
    91     }
     85    char errorstr[256];
     86    av_strerror (err, errorstr, sizeof(errorstr));
     87    AUBIO_ERR("Failed opening %s (%s)\n", s->path, errorstr);
    9288    goto beach;
    9389  }
     
    9894  // retrieve stream information
    9995  if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
    100     uint8_t errorstr_len = 128;
    101     char errorstr[errorstr_len];
    102     if (av_strerror (err, errorstr, errorstr_len) == 0) {
    103       AUBIO_ERR("Could not find stream information "
    104           "for %s (%s)\n", s->path, errorstr);
    105     } else {
    106       AUBIO_ERR("Could not find stream information "
    107           "for %s (unknown error)\n", s->path);
    108     }
     96    char errorstr[256];
     97    av_strerror (err, errorstr, sizeof(errorstr));
     98    AUBIO_ERR("Could not find stream information " "for %s (%s)\n", s->path,
     99        errorstr);
    109100    goto beach;
    110101  }
     
    142133
    143134  if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
    144     uint8_t errorstr_len = 128;
    145     char errorstr[errorstr_len];
    146     if (av_strerror (err, errorstr, errorstr_len) == 0) {
    147       AUBIO_ERR("Could not load codec for %s (%s)\n", s->path, errorstr);
    148     } else {
    149       AUBIO_ERR("Could not load codec for %s (unknown error)\n", s->path);
    150     }
     135    char errorstr[256];
     136    av_strerror (err, errorstr, sizeof(errorstr));
     137    AUBIO_ERR("Could not load codec for %s (%s)\n", s->path, errorstr);
    151138    goto beach;
    152139  }
     
    176163  av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLTP,     0);
    177164  if ( ( err = avresample_open(avr) ) < 0) {
    178     uint8_t errorstr_len = 128;
    179     char errorstr[errorstr_len];
    180     if (av_strerror (err, errorstr, errorstr_len) == 0) {
    181       AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n",
    182           s->path, errorstr);
    183     } else {
    184       AUBIO_ERR("Could not open AVAudioResampleContext for %s (unknown error)\n",
    185           s->path);
    186     }
     165    char errorstr[256];
     166    av_strerror (err, errorstr, sizeof(errorstr));
     167    AUBIO_ERR("Could not open AVAudioResampleContext for %s (%s)\n",
     168        s->path, errorstr);
    187169    goto beach;
    188170  }
     
    195177
    196178  /* allocate output for avr */
    197   s->output = (float *)av_malloc(AUBIO_AVCODEC_MIN_BUFFER_SIZE * sizeof(float));
     179  s->output = (float *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(float));
    198180
    199181  s->read_samples = 0;
     
    226208  AVAudioResampleContext *avr = s->avr;
    227209  float *output = s->output;
     210  *read_samples = 0;
    228211
    229212  do
    230213  {
    231214    int err = av_read_frame (avFormatCtx, &avPacket);
     215    if (err == AVERROR_EOF) {
     216      s->eof = 1;
     217      goto beach;
     218    }
    232219    if (err != 0) {
    233       if (err == AVERROR_EOF) {
    234         s->eof = 1;
    235         *read_samples = 0;
    236         return;
    237       }
    238       uint8_t errorstr_len = 128;
    239       char errorstr[errorstr_len];
    240       if (av_strerror (err, errorstr, errorstr_len) == 0) {
    241         AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr);
    242       } else {
    243         AUBIO_ERR("Could not read frame in %s (unknown error)\n", s->path);
    244       }
    245       *read_samples = 0;
    246       return;
     220      char errorstr[256];
     221      av_strerror (err, errorstr, sizeof(errorstr));
     222      AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr);
     223      goto beach;
    247224    }
    248225  } while (avPacket.stream_index != s->selected_stream);
     
    252229
    253230  if (len < 0) {
    254     av_free_packet(&avPacket);
    255231    AUBIO_ERR("Error while decoding %s\n", s->path);
    256     return;
     232    goto beach;
    257233  }
    258234  if (got_frame == 0) {
    259     av_free_packet(&avPacket);
    260235    //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
    261     *read_samples = 0;
    262     return;
    263   } /* else {
    264     int data_size =
    265       av_samples_get_buffer_size(NULL,
    266         avCodecCtx->channels, avFrame->nb_samples,
    267         avCodecCtx->sample_fmt, 1);
    268     AUBIO_WRN("Got data_size %d frame for (%s)\n", data_size, s->path);
    269   } */
     236    goto beach;
     237  }
     238
     239  int in_linesize = 0;
     240  av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
     241      avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
     242  //AUBIO_WRN("Got data_size %d in_linesize %d frame for (%s)\n",
     243  //    data_size, in_linesize, s->path);
    270244
    271245  int in_samples = avFrame->nb_samples;
    272   int in_plane_size = 0; //avFrame->linesize[0];
    273   int out_plane_size = 0; //sizeof(float); //in_samples * sizeof(float);
    274   int max_out_samples = AUBIO_AVCODEC_MIN_BUFFER_SIZE;
     246  int out_linesize = 0;
     247  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
    275248  int out_samples = avresample_convert ( avr,
    276         (uint8_t **)&output, out_plane_size, max_out_samples,
    277         (uint8_t **)avFrame->data, in_plane_size, in_samples);
    278   if (out_samples < 0) {
    279       AUBIO_ERR("Could not convert frame  (%s)\n", s->path);
    280       *read_samples = 0;
    281   }
    282   //AUBIO_ERR("Got in_plane_size %d frame for (%s)\n", in_plane_size, s->path);
    283   //AUBIO_WRN("Delay is %d for %s\n", avresample_get_delay(avr), s->path);
    284   //AUBIO_WRN("max_out_samples is %d for AUBIO_AVCODEC_MIN_BUFFER_SIZE %d\n",
    285   //    max_out_samples, AUBIO_AVCODEC_MIN_BUFFER_SIZE);
    286 
    287   av_free_packet(&avPacket);
     249        (uint8_t **)&output, out_linesize, max_out_samples,
     250        (uint8_t **)avFrame->data, in_linesize, in_samples);
     251  if (out_samples <= 0) {
     252    AUBIO_ERR("No sample found while converting frame (%s)\n", s->path);
     253    goto beach;
     254  }
     255
     256  *read_samples = out_samples;
     257
     258beach:
    288259  s->avFormatCtx = avFormatCtx;
    289260  s->avCodecCtx = avCodecCtx;
     
    292263  s->output = output;
    293264
    294   *read_samples = out_samples;
     265  av_free_packet(&avPacket);
    295266}
    296267
Note: See TracChangeset for help on using the changeset viewer.