Changeset d3b9fe4
- Timestamp:
- Dec 5, 2013, 1:59:40 PM (11 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/io/source_avcodec.c
r1b0755d rd3b9fe4 36 36 #include "source_avcodec.h" 37 37 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 41 39 42 40 #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05) … … 49 47 // some data about the file 50 48 char_t *path; 51 int input_samplerate;52 int input_channels;49 uint_t input_samplerate; 50 uint_t input_channels; 53 51 54 52 // avcodec stuff … … 58 56 AVPacket avPacket; 59 57 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; 63 61 }; 64 62 … … 123 121 AUBIO_ERR("No audio stream in %s\n", s->path); 124 122 goto beach; 125 } 123 } 126 124 127 125 //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path); 128 126 129 127 AVCodecContext *avCodecCtx = s->avCodecCtx; 130 avCodecCtx = avFormatCtx->streams[selected_stream]->codec; 128 avCodecCtx = avFormatCtx->streams[selected_stream]->codec; 131 129 AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id); 132 130 if (codec == NULL) { … … 149 147 s->input_samplerate = avCodecCtx->sample_rate; 150 148 s->input_channels = avCodecCtx->channels; 151 152 149 //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate); 153 150 //AUBIO_DBG("input_channels: %d\n", s->input_channels); … … 189 186 av_init_packet(&avPacket); 190 187 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)); 193 190 194 191 s->read_samples = 0; 195 192 s->read_index = 0; 196 193 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);202 194 s->avFormatCtx = avFormatCtx; 203 195 s->avCodecCtx = avCodecCtx; … … 217 209 } 218 210 219 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, int * read_samples) {211 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) { 220 212 AVFormatContext *avFormatCtx = s->avFormatCtx; 221 213 AVCodecContext *avCodecCtx = s->avCodecCtx; … … 238 230 if (len < 0) { 239 231 AUBIO_ERR("Error while decoding %s\n", s->path); 240 //goto beach;241 232 return; 242 233 } 243 234 if (got_frame == 0) { 244 235 AUBIO_ERR("Could not get frame for (%s)\n", s->path); 245 } else {246 //int data_size =236 } /* else { 237 int data_size = 247 238 av_samples_get_buffer_size(NULL, 248 239 avCodecCtx->channels, avFrame->nb_samples, 249 240 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 } */ 252 243 253 244 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 1260 245 int in_plane_size = 0; //avFrame->linesize[0]; 261 246 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; 263 248 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) { 266 251 AUBIO_ERR("Could not convert frame (%s)\n", s->path); 267 252 } 268 253 //AUBIO_ERR("Got in_plane_size %d frame for (%s)\n", in_plane_size, s->path); 269 254 //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) 274 259 + in_samples) * s->samplerate / s->input_samplerate; 275 260 //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 //} 308 264 s->avFormatCtx = avFormatCtx; 309 265 s->avCodecCtx = avCodecCtx; … … 322 278 // begin reading 323 279 if (s->read_samples == 0) { 324 int avcodec_read = 0;280 uint_t avcodec_read = 0; 325 281 aubio_source_avcodec_readframe(s, &avcodec_read); 326 282 s->read_samples += avcodec_read; … … 329 285 if (s->read_samples < s->hop_size) { 330 286 // 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; 332 288 for (i = 0; i < partial; i++) { 333 289 read_data->data[i] = SHORT_TO_FLOAT(s->output[i + s->read_index]); … … 336 292 s->read_index = 0; 337 293 // get more data 338 int avcodec_read = 0;294 uint_t avcodec_read = 0; 339 295 aubio_source_avcodec_readframe(s, &avcodec_read); 340 296 s->read_samples += avcodec_read; 341 297 s->read_index = 0; 342 298 // 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) { 345 301 end = partial; 346 302 }
Note: See TracChangeset
for help on using the changeset viewer.