Changeset 61ecd1a


Ignore:
Timestamp:
Dec 7, 2013, 5:44:40 AM (10 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:
7f2c515
Parents:
3da8187
Message:

src/io/source_avcodec.c: rename hack to _reset_resampler, move up for clarity

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_avcodec.c

    r3da8187 r61ecd1a  
    6161};
    6262
    63 /* convenience function to recreate the AVAudioResampleContext the first time _do
    64  * or _do_multi is called */
    65 void aubio_source_avcodec_set_multi(aubio_source_avcodec_t * s, uint_t multi);
     63// hack to create or re-create the context the first time _do or _do_multi is called
     64void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi);
    6665
    6766aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplerate, uint_t hop_size) {
     
    180179
    181180  // default to mono output
    182   aubio_source_avcodec_set_multi(s, 0);
     181  aubio_source_avcodec_reset_resampler(s, 0);
    183182
    184183  s->eof = 0;
     
    196195}
    197196
    198 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
    199   AVFormatContext *avFormatCtx = s->avFormatCtx;
    200   AVCodecContext *avCodecCtx = s->avCodecCtx;
    201   AVFrame *avFrame = s->avFrame;
    202   AVPacket avPacket;
    203   av_init_packet (&avPacket);
    204   AVAudioResampleContext *avr = s->avr;
    205   float *output = s->output;
    206   *read_samples = 0;
    207 
    208   do
    209   {
    210     int err = av_read_frame (avFormatCtx, &avPacket);
    211     if (err == AVERROR_EOF) {
    212       s->eof = 1;
    213       goto beach;
    214     }
    215     if (err != 0) {
    216       char errorstr[256];
    217       av_strerror (err, errorstr, sizeof(errorstr));
    218       AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr);
    219       goto beach;
    220     }
    221   } while (avPacket.stream_index != s->selected_stream);
    222 
    223   int got_frame = 0;
    224   int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
    225 
    226   if (len < 0) {
    227     AUBIO_ERR("Error while decoding %s\n", s->path);
    228     goto beach;
    229   }
    230   if (got_frame == 0) {
    231     //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
    232     goto beach;
    233   }
    234 
    235   int in_linesize = 0;
    236   av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
    237       avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
    238   int in_samples = avFrame->nb_samples;
    239   int out_linesize = 0;
    240   int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
    241   int out_samples = avresample_convert ( avr,
    242         (uint8_t **)&output, out_linesize, max_out_samples,
    243         (uint8_t **)avFrame->data, in_linesize, in_samples);
    244   if (out_samples <= 0) {
    245     AUBIO_ERR("No sample found while converting frame (%s)\n", s->path);
    246     goto beach;
    247   }
    248 
    249   *read_samples = out_samples;
    250 
    251 beach:
    252   s->avFormatCtx = avFormatCtx;
    253   s->avCodecCtx = avCodecCtx;
    254   s->avFrame = avFrame;
    255   s->avr = avr;
    256   s->output = output;
    257 
    258   av_free_packet(&avPacket);
    259 }
    260 
    261 void aubio_source_avcodec_set_multi(aubio_source_avcodec_t * s, uint_t multi) {
     197void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi) {
    262198  if ( (multi != s->multi) || (s->avr == NULL) ) {
    263199    int64_t input_layout = av_get_default_channel_layout(s->input_channels);
    264     int64_t mono_layout = av_get_default_channel_layout(1);
     200    uint_t output_channels = multi ? s->input_channels : 1;
     201    int64_t output_layout = av_get_default_channel_layout(output_channels);
    265202    if (s->avr != NULL) {
    266203      avresample_close( s->avr );
     
    272209
    273210    av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
    274     if (multi) {
    275     av_opt_set_int(avr, "out_channel_layout", input_layout,            0);
    276     } else {
     211    av_opt_set_int(avr, "out_channel_layout", output_layout,          0);
    277212    av_opt_set_int(avr, "out_channel_layout", mono_layout,            0);
    278     }
    279213    av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,    0);
    280214    av_opt_set_int(avr, "out_sample_rate",    s->samplerate,          0);
     
    295229}
    296230
     231void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
     232  AVFormatContext *avFormatCtx = s->avFormatCtx;
     233  AVCodecContext *avCodecCtx = s->avCodecCtx;
     234  AVFrame *avFrame = s->avFrame;
     235  AVPacket avPacket;
     236  av_init_packet (&avPacket);
     237  AVAudioResampleContext *avr = s->avr;
     238  float *output = s->output;
     239  *read_samples = 0;
     240
     241  do
     242  {
     243    int err = av_read_frame (avFormatCtx, &avPacket);
     244    if (err == AVERROR_EOF) {
     245      s->eof = 1;
     246      goto beach;
     247    }
     248    if (err != 0) {
     249      char errorstr[256];
     250      av_strerror (err, errorstr, sizeof(errorstr));
     251      AUBIO_ERR("Could not read frame in %s (%s)\n", s->path, errorstr);
     252      goto beach;
     253    }
     254  } while (avPacket.stream_index != s->selected_stream);
     255
     256  int got_frame = 0;
     257  int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
     258
     259  if (len < 0) {
     260    AUBIO_ERR("Error while decoding %s\n", s->path);
     261    goto beach;
     262  }
     263  if (got_frame == 0) {
     264    //AUBIO_ERR("Could not get frame for (%s)\n", s->path);
     265    goto beach;
     266  }
     267
     268  int in_linesize = 0;
     269  av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
     270      avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
     271  int in_samples = avFrame->nb_samples;
     272  int out_linesize = 0;
     273  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
     274  int out_samples = avresample_convert ( avr,
     275        (uint8_t **)&output, out_linesize, max_out_samples,
     276        (uint8_t **)avFrame->data, in_linesize, in_samples);
     277  if (out_samples <= 0) {
     278    AUBIO_ERR("No sample found while converting frame (%s)\n", s->path);
     279    goto beach;
     280  }
     281
     282  *read_samples = out_samples;
     283
     284beach:
     285  s->avFormatCtx = avFormatCtx;
     286  s->avCodecCtx = avCodecCtx;
     287  s->avFrame = avFrame;
     288  s->avr = avr;
     289  s->output = output;
     290
     291  av_free_packet(&avPacket);
     292}
     293
    297294void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
    298   if (s->multi == 1) aubio_source_avcodec_set_multi(s, 0);
     295  if (s->multi == 1) aubio_source_avcodec_reset_resampler(s, 0);
    299296  uint_t i;
    300297  uint_t end = 0;
     
    327324
    328325void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
    329   if (s->multi == 0) aubio_source_avcodec_set_multi(s, 1);
     326  if (s->multi == 0) aubio_source_avcodec_reset_resampler(s, 1);
    330327  uint_t i,j;
    331328  uint_t end = 0;
Note: See TracChangeset for help on using the changeset viewer.