Changeset d3b9fe4


Ignore:
Timestamp:
Dec 5, 2013, 1:59:40 PM (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:
23bf001
Parents:
1b0755d
Message:

src/io/source_avcodec.c: tidying up

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_avcodec.c

    r1b0755d rd3b9fe4  
    3636#include "source_avcodec.h"
    3737
    38 #define MAX_CHANNELS 6
    39 #define MAX_SIZE 4096
    40 #define MAX_SAMPLES MAX_CHANNELS * MAX_SIZE
     38#define AUBIO_AVCODEC_MIN_BUFFER_SIZE FF_MIN_BUFFER_SIZE
    4139
    4240#define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
     
    4947  // some data about the file
    5048  char_t *path;
    51   int input_samplerate;
    52   int input_channels;
     49  uint_t input_samplerate;
     50  uint_t input_channels;
    5351
    5452  // avcodec stuff
     
    5856  AVPacket avPacket;
    5957  AVAudioResampleContext *avr;
    60   int16_t *output; 
    61   int read_samples;
    62   int read_index;
     58  int16_t *output;
     59  uint_t read_samples;
     60  uint_t read_index;
    6361};
    6462
     
    123121    AUBIO_ERR("No audio stream in %s\n", s->path);
    124122    goto beach;
    125   } 
     123  }
    126124
    127125  //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
    128126
    129127  AVCodecContext *avCodecCtx = s->avCodecCtx;
    130   avCodecCtx = avFormatCtx->streams[selected_stream]->codec; 
     128  avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
    131129  AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id);
    132130  if (codec == NULL) {
     
    149147  s->input_samplerate = avCodecCtx->sample_rate;
    150148  s->input_channels   = avCodecCtx->channels;
    151 
    152149  //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate);
    153150  //AUBIO_DBG("input_channels: %d\n", s->input_channels);
     
    189186  av_init_packet(&avPacket);
    190187
    191   s->output = (int16_t *)av_malloc(FF_MIN_BUFFER_SIZE * sizeof(int16_t));
    192 
     188  /* allocate output for avr */
     189  s->output = (int16_t *)av_malloc(AUBIO_AVCODEC_MIN_BUFFER_SIZE * sizeof(int16_t));
    193190
    194191  s->read_samples = 0;
    195192  s->read_index = 0;
    196193
    197   //goto beach;
    198 
    199   /* allocate data for de/interleaving reallocated when needed. */
    200   //s->scratch_size = s->input_hop_size * s->input_channels;
    201   //s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
    202194  s->avFormatCtx = avFormatCtx;
    203195  s->avCodecCtx = avCodecCtx;
     
    217209}
    218210
    219 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, int * read_samples) {
     211void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
    220212  AVFormatContext *avFormatCtx = s->avFormatCtx;
    221213  AVCodecContext *avCodecCtx = s->avCodecCtx;
     
    238230  if (len < 0) {
    239231    AUBIO_ERR("Error while decoding %s\n", s->path);
    240     //goto beach;
    241232    return;
    242233  }
    243234  if (got_frame == 0) {
    244235    AUBIO_ERR("Could not get frame for (%s)\n", s->path);
    245   } else {
    246     //int data_size =
     236  } /* else {
     237    int data_size =
    247238      av_samples_get_buffer_size(NULL,
    248239        avCodecCtx->channels, avFrame->nb_samples,
    249240        avCodecCtx->sample_fmt, 1);
    250     //AUBIO_WRN("Got data_size %d frame for (%s)\n", data_size, s->path);
    251   }
     241    AUBIO_WRN("Got data_size %d frame for (%s)\n", data_size, s->path);
     242  } */
    252243
    253244  int in_samples = avFrame->nb_samples;
    254   for (i = 0; i < in_samples; i ++) {
    255     //AUBIO_WRN("%d\n", avFrame->data[i]);
    256   }
    257 
    258 
    259 #if 1
    260245  int in_plane_size = 0; //avFrame->linesize[0];
    261246  int out_plane_size = 0; //sizeof(float); //in_samples * sizeof(float);
    262   int max_out_samples = FF_MIN_BUFFER_SIZE;
     247  int max_out_samples = AUBIO_AVCODEC_MIN_BUFFER_SIZE;
    263248  if (avresample_convert ( avr,
    264         (void **)&output, out_plane_size, max_out_samples,
    265         (void **)avFrame->data, in_plane_size, in_samples) < 0) {
     249        (uint8_t **)&output, out_plane_size, max_out_samples,
     250        (uint8_t **)avFrame->data, in_plane_size, in_samples) < 0) {
    266251      AUBIO_ERR("Could not convert frame  (%s)\n", s->path);
    267252  }
    268253  //AUBIO_ERR("Got in_plane_size %d frame for (%s)\n", in_plane_size, s->path);
    269254  //AUBIO_WRN("Delay is %d for %s\n", avresample_get_delay(avr), s->path);
    270   //AUBIO_WRN("max_out_samples is %d for FF_MIN_BUFFER_SIZE %d\n",
    271   //    max_out_samples, FF_MIN_BUFFER_SIZE);
    272 
    273   int out_samples = avresample_available(avr) + (avresample_get_delay(avr)
     255  //AUBIO_WRN("max_out_samples is %d for AUBIO_AVCODEC_MIN_BUFFER_SIZE %d\n",
     256  //    max_out_samples, AUBIO_AVCODEC_MIN_BUFFER_SIZE);
     257
     258  uint_t out_samples = avresample_available(avr) + (avresample_get_delay(avr)
    274259        + in_samples) * s->samplerate / s->input_samplerate;
    275260  //AUBIO_WRN("Converted %d to %d samples\n", in_samples, out_samples);
    276   for (i = 0; i < out_samples; i ++) {
    277     //AUBIO_DBG("%f\n", SHORT_TO_FLOAT(output[i]));
    278   }
    279 #else
    280   int in_plane_size = 0; //avFrame->linesize[0];
    281   int out_plane_size = 0; //sizeof(float); //in_samples * sizeof(float);
    282   int max_out_samples = FF_MIN_BUFFER_SIZE;
    283   if (avresample_convert ( avr,
    284         NULL, out_plane_size, max_out_samples,
    285         (uint8_t **)avFrame->data, in_plane_size, in_samples) < 0) {
    286       AUBIO_ERR("Could not convert frame  (%s)\n", s->path);
    287   }
    288   AUBIO_ERR("Got in_plane_size %d frame for (%s)\n", in_plane_size, s->path);
    289   AUBIO_WRN("Delay is %d for %s\n", avresample_get_delay(avr), s->path);
    290   AUBIO_WRN("max_out_samples is %d for FF_MIN_BUFFER_SIZE %d\n",
    291       max_out_samples, FF_MIN_BUFFER_SIZE);
    292 
    293   //int out_samples = avresample_available (avr) +
    294   //  (avresample_get_delay(avr) + in_samples) * s->samplerate / s->input_samplerate;
    295   int out_samples = avresample_available(avr);
    296   AUBIO_WRN("Found %d samples available for %s\n", avresample_available(avr), s->path);
    297   //
    298   err = avresample_read (avr, (uint8_t **)output, out_samples);
    299   if (err != 0) {
    300     AUBIO_WRN("Error %d while reading %s\n", err, s->path);
    301   }
    302   AUBIO_WRN("Converted %d to %d samples\n", in_samples, out_samples);
    303 
    304   for (i = 0; i < out_samples; i ++) {
    305     AUBIO_WRN("%f\n", SHORT_TO_FLOAT(output[i]));
    306   }
    307 #endif
     261  //for (i = 0; i < out_samples; i ++) {
     262  //  AUBIO_DBG("%f\n", SHORT_TO_FLOAT(output[i]));
     263  //}
    308264  s->avFormatCtx = avFormatCtx;
    309265  s->avCodecCtx = avCodecCtx;
     
    322278  // begin reading
    323279  if (s->read_samples == 0) {
    324     int avcodec_read = 0;
     280    uint_t avcodec_read = 0;
    325281    aubio_source_avcodec_readframe(s, &avcodec_read);
    326282    s->read_samples += avcodec_read;
     
    329285  if (s->read_samples < s->hop_size) {
    330286    // write the end of the buffer to the beginning of read_data
    331     int partial = s->read_samples;
     287    uint_t partial = s->read_samples;
    332288    for (i = 0; i < partial; i++) {
    333289      read_data->data[i] = SHORT_TO_FLOAT(s->output[i + s->read_index]);
     
    336292    s->read_index = 0;
    337293    // get more data
    338     int avcodec_read = 0;
     294    uint_t avcodec_read = 0;
    339295    aubio_source_avcodec_readframe(s, &avcodec_read);
    340296    s->read_samples += avcodec_read;
    341297    s->read_index = 0;
    342298    // write the beginning of the buffer to the end of read_data
    343     int end = MIN((int)(s->hop_size), s->read_samples);
    344     if (avcodec_read == 0) { 
     299    uint_t end = MIN(s->hop_size, s->read_samples);
     300    if (avcodec_read == 0) {
    345301      end = partial;
    346302    }
Note: See TracChangeset for help on using the changeset viewer.