source: src/io/source_avcodec.c @ 50f39f5

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since 50f39f5 was 50f39f5, checked in by Paul Brossier <piem@piem.org>, 7 years ago

src/io/source_avcodec.c: fix for windows vc9

  • Property mode set to 100644
File size: 18.3 KB
RevLine 
[ba0ba10]1/*
2  Copyright (C) 2013 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
[33d0242]21#include "aubio_priv.h"
[ba0ba10]22
[549928e]23#ifdef HAVE_LIBAV
[ba0ba10]24
[dcde285]25#include <libavcodec/avcodec.h>
26#include <libavformat/avformat.h>
[ba67cb6]27#if defined(HAVE_SWRESAMPLE)
28#include <libswresample/swresample.h>
29#elif defined(HAVE_AVRESAMPLE)
[dcde285]30#include <libavresample/avresample.h>
[ba67cb6]31#endif
[dcde285]32#include <libavutil/opt.h>
33#include <stdlib.h>
34
[a9c33a2]35// determine whether we use libavformat from ffmpeg or from libav
36#define FFMPEG_LIBAVFORMAT (LIBAVFORMAT_VERSION_MICRO > 99 )
37// max_analyze_duration2 was used from ffmpeg libavformat 55.43.100 through 57.2.100
[9ac77ac]38#define FFMPEG_LIBAVFORMAT_MAX_DUR2 FFMPEG_LIBAVFORMAT && ( \
[a9c33a2]39      (LIBAVFORMAT_VERSION_MAJOR == 55 && LIBAVFORMAT_VERSION_MINOR >= 43) \
40      || (LIBAVFORMAT_VERSION_MAJOR == 56) \
41      || (LIBAVFORMAT_VERSION_MAJOR == 57 && LIBAVFORMAT_VERSION_MINOR < 2) \
42      )
[2a32644]43
[a2d628b]44// backward compatibility with libavcodec55
[8250214]45#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57,0,0)
[1504b7c]46#define HAVE_AUBIO_LIBAVCODEC_DEPRECATED 1
[8250214]47#endif
48
49#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
50#warning "libavcodec < 56 is deprecated"
[a2d628b]51#define av_frame_alloc  avcodec_alloc_frame
52#define av_frame_free avcodec_free_frame
53#define av_packet_unref av_free_packet
54#endif
55
[ba0ba10]56#include "aubio_priv.h"
57#include "fvec.h"
58#include "fmat.h"
59#include "source_avcodec.h"
60
[0af9003]61#define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
[ba0ba10]62
63struct _aubio_source_avcodec_t {
64  uint_t hop_size;
65  uint_t samplerate;
66  uint_t channels;
67
68  // some data about the file
69  char_t *path;
[d3b9fe4]70  uint_t input_samplerate;
71  uint_t input_channels;
[ba0ba10]72
73  // avcodec stuff
74  AVFormatContext *avFormatCtx;
75  AVCodecContext *avCodecCtx;
76  AVFrame *avFrame;
[261836d]77  AVPacket avPacket;
[ba67cb6]78#ifdef HAVE_AVRESAMPLE
[ba0ba10]79  AVAudioResampleContext *avr;
[ba67cb6]80#elif defined(HAVE_SWRESAMPLE)
81  SwrContext *avr;
82#endif
[2f89ef4]83  smpl_t *output;
[d3b9fe4]84  uint_t read_samples;
85  uint_t read_index;
[7760b40]86  sint_t selected_stream;
[eadd8d5]87  uint_t eof;
[fcd963a]88  uint_t multi;
[ba0ba10]89};
90
[61ecd1a]91// hack to create or re-create the context the first time _do or _do_multi is called
92void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi);
[029bf4e]93void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples);
[fcd963a]94
[6769586]95uint_t aubio_source_avcodec_has_network_url(aubio_source_avcodec_t *s);
96
97uint_t aubio_source_avcodec_has_network_url(aubio_source_avcodec_t *s) {
98  char proto[20], authorization[256], hostname[128], uripath[256];
99  int proto_size = 20, authorization_size = 256, hostname_size = 128,
100      *port_ptr = 0, path_size = 256;
101  av_url_split(proto, proto_size, authorization, authorization_size, hostname,
102      hostname_size, port_ptr, uripath, path_size, s->path);
103  if (strlen(proto)) {
104    return 1;
105  }
106  return 0;
107}
108
109
[ae5d58a]110aubio_source_avcodec_t * new_aubio_source_avcodec(const char_t * path, uint_t samplerate, uint_t hop_size) {
[ba0ba10]111  aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
[50f39f5]112  AVFormatContext *avFormatCtx = s->avFormatCtx;
113  AVCodecContext *avCodecCtx = s->avCodecCtx;
114  AVCodec *codec;
115  AVFrame *avFrame = s->avFrame;
116  uint_t i;
117  sint_t selected_stream = -1;
[ba0ba10]118  int err;
119  if (path == NULL) {
[491e6ea]120    AUBIO_ERR("source_avcodec: Aborted opening null path\n");
[b294b3e]121    goto beach;
122  }
123  if ((sint_t)samplerate < 0) {
[491e6ea]124    AUBIO_ERR("source_avcodec: Can not open %s with samplerate %d\n", path, samplerate);
[b294b3e]125    goto beach;
126  }
127  if ((sint_t)hop_size <= 0) {
[491e6ea]128    AUBIO_ERR("source_avcodec: Can not open %s with hop_size %d\n", path, hop_size);
[b294b3e]129    goto beach;
[ba0ba10]130  }
131
132  s->hop_size = hop_size;
133  s->channels = 1;
[b643a33]134
135  if (s->path) AUBIO_FREE(s->path);
[d2be104]136  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
137  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[ba0ba10]138
139  // register all formats and codecs
140  av_register_all();
141
[6769586]142  if (aubio_source_avcodec_has_network_url(s)) {
143    avformat_network_init();
144  }
[eadd8d5]145
[d75e2d5]146  // try opening the file and get some info about it
[ba0ba10]147  avFormatCtx = NULL;
148  if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
[0af9003]149    char errorstr[256];
150    av_strerror (err, errorstr, sizeof(errorstr));
[491e6ea]151    AUBIO_ERR("source_avcodec: Failed opening %s (%s)\n", s->path, errorstr);
[ba0ba10]152    goto beach;
153  }
154
[6abb4de]155  // try to make sure max_analyze_duration is big enough for most songs
[a9c33a2]156#if FFMPEG_LIBAVFORMAT_MAX_DUR2
[2a32644]157  avFormatCtx->max_analyze_duration2 *= 100;
158#else
[6abb4de]159  avFormatCtx->max_analyze_duration *= 100;
[2a32644]160#endif
[6abb4de]161
[ba0ba10]162  // retrieve stream information
163  if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
[0af9003]164    char errorstr[256];
165    av_strerror (err, errorstr, sizeof(errorstr));
[491e6ea]166    AUBIO_ERR("source_avcodec: Could not find stream information " "for %s (%s)\n", s->path,
[0af9003]167        errorstr);
[ba0ba10]168    goto beach;
169  }
170
[1fe3ac2]171  // dump information about file onto standard error
[ba0ba10]172  //av_dump_format(avFormatCtx, 0, s->path, 0);
173
[1fe3ac2]174  // look for the first audio stream
[ba0ba10]175  for (i = 0; i < avFormatCtx->nb_streams; i++) {
[2b3c438]176#if FF_API_LAVF_AVCTX
177    if (avFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
178#else
[ba0ba10]179    if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
[2b3c438]180#endif
[ba0ba10]181      if (selected_stream == -1) {
182        selected_stream = i;
183      } else {
[491e6ea]184        AUBIO_WRN("source_avcodec: More than one audio stream in %s, "
[1fe3ac2]185            "taking the first one\n", s->path);
[ba0ba10]186      }
187    }
188  }
189  if (selected_stream == -1) {
[491e6ea]190    AUBIO_ERR("source_avcodec: No audio stream in %s\n", s->path);
[ba0ba10]191    goto beach;
[d3b9fe4]192  }
[eadd8d5]193  //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
[7760b40]194  s->selected_stream = selected_stream;
[ba0ba10]195
[2b3c438]196#if FF_API_LAVF_AVCTX
197  AVCodecParameters *codecpar = avFormatCtx->streams[selected_stream]->codecpar;
198  if (codecpar == NULL) {
199    AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path);
200    goto beach;
201  }
[50f39f5]202  codec = avcodec_find_decoder(codecpar->codec_id);
[2b3c438]203
204  /* Allocate a codec context for the decoder */
205  avCodecCtx = avcodec_alloc_context3(codec);
206  if (!avCodecCtx) {
207    AUBIO_ERR("source_avcodec: Failed to allocate the %s codec context for path %s\n",
208        av_get_media_type_string(AVMEDIA_TYPE_AUDIO), s->path);
209    goto beach;
210  }
211#else
[d3b9fe4]212  avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
[ba0ba10]213  AVCodec *codec = avcodec_find_decoder(avCodecCtx->codec_id);
[2b3c438]214#endif
[ba0ba10]215  if (codec == NULL) {
[491e6ea]216    AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path);
[ba0ba10]217    goto beach;
218  }
219
[2b3c438]220#if FF_API_LAVF_AVCTX
221  /* Copy codec parameters from input stream to output codec context */
222  if ((err = avcodec_parameters_to_context(avCodecCtx, codecpar)) < 0) {
223    AUBIO_ERR("source_avcodec: Failed to copy %s codec parameters to decoder context for %s\n",
224       av_get_media_type_string(AVMEDIA_TYPE_AUDIO), s->path);
225    goto beach;
226  }
227#endif
228
[ba0ba10]229  if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
[0af9003]230    char errorstr[256];
231    av_strerror (err, errorstr, sizeof(errorstr));
[491e6ea]232    AUBIO_ERR("source_avcodec: Could not load codec for %s (%s)\n", s->path, errorstr);
[ba0ba10]233    goto beach;
234  }
235
236  /* get input specs */
237  s->input_samplerate = avCodecCtx->sample_rate;
238  s->input_channels   = avCodecCtx->channels;
239  //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate);
240  //AUBIO_DBG("input_channels: %d\n", s->input_channels);
241
242  if (samplerate == 0) {
[26a6af7]243    s->samplerate = s->input_samplerate;
244  } else {
245    s->samplerate = samplerate;
[ba0ba10]246  }
247
[018e511]248  if (s->samplerate >  s->input_samplerate) {
[491e6ea]249    AUBIO_WRN("source_avcodec: upsampling %s from %d to %d\n", s->path,
[018e511]250        s->input_samplerate, s->samplerate);
251  }
252
[cd4c997]253  avFrame = av_frame_alloc();
[ba0ba10]254  if (!avFrame) {
[491e6ea]255    AUBIO_ERR("source_avcodec: Could not allocate frame for (%s)\n", s->path);
[ba0ba10]256  }
257
[d3b9fe4]258  /* allocate output for avr */
[2f89ef4]259  s->output = (smpl_t *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE * sizeof(smpl_t));
[ba0ba10]260
261  s->read_samples = 0;
262  s->read_index = 0;
263
264  s->avFormatCtx = avFormatCtx;
265  s->avCodecCtx = avCodecCtx;
266  s->avFrame = avFrame;
[fcd963a]267
268  // default to mono output
[61ecd1a]269  aubio_source_avcodec_reset_resampler(s, 0);
[ba0ba10]270
[eadd8d5]271  s->eof = 0;
[fcd963a]272  s->multi = 0;
[eadd8d5]273
[ba0ba10]274  //av_log_set_level(AV_LOG_QUIET);
275
276  return s;
277
278beach:
[fcd963a]279  //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
280  //    s->path, s->samplerate, s->hop_size);
[ba0ba10]281  del_aubio_source_avcodec(s);
282  return NULL;
283}
284
[61ecd1a]285void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi) {
[2f89ef4]286  // create or reset resampler to/from mono/multi-channel
[61ecd1a]287  if ( (multi != s->multi) || (s->avr == NULL) ) {
288    int64_t input_layout = av_get_default_channel_layout(s->input_channels);
289    uint_t output_channels = multi ? s->input_channels : 1;
290    int64_t output_layout = av_get_default_channel_layout(output_channels);
[ba67cb6]291#ifdef HAVE_AVRESAMPLE
[7cc80b6]292    AVAudioResampleContext *avr = avresample_alloc_context();
293    AVAudioResampleContext *oldavr = s->avr;
[ba67cb6]294#elif defined(HAVE_SWRESAMPLE)
295    SwrContext *avr = swr_alloc();
296    SwrContext *oldavr = s->avr;
297#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
[61ecd1a]298
299    av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
300    av_opt_set_int(avr, "out_channel_layout", output_layout,          0);
301    av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,    0);
302    av_opt_set_int(avr, "out_sample_rate",    s->samplerate,          0);
303    av_opt_set_int(avr, "in_sample_fmt",      s->avCodecCtx->sample_fmt, 0);
[2f89ef4]304#if HAVE_AUBIO_DOUBLE
305    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_DBL,      0);
306#else
[61ecd1a]307    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLT,      0);
[2f89ef4]308#endif
309    // TODO: use planar?
310    //av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLTP,      0);
[61ecd1a]311    int err;
[ba67cb6]312#ifdef HAVE_AVRESAMPLE
[61ecd1a]313    if ( ( err = avresample_open(avr) ) < 0) {
[ba67cb6]314#elif defined(HAVE_SWRESAMPLE)
315    if ( ( err = swr_init(avr) ) < 0) {
316#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
[61ecd1a]317      char errorstr[256];
318      av_strerror (err, errorstr, sizeof(errorstr));
[491e6ea]319      AUBIO_ERR("source_avcodec: Could not open AVAudioResampleContext for %s (%s)\n",
[61ecd1a]320          s->path, errorstr);
321      //goto beach;
322      return;
323    }
324    s->avr = avr;
[7cc80b6]325    if (oldavr != NULL) {
[ba67cb6]326#ifdef HAVE_AVRESAMPLE
[7cc80b6]327      avresample_close( oldavr );
[ba67cb6]328#elif defined(HAVE_SWRESAMPLE)
329      swr_close ( oldavr );
330#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
[7cc80b6]331      av_free ( oldavr );
332      oldavr = NULL;
333    }
[61ecd1a]334    s->multi = multi;
335  }
336}
337
[d3b9fe4]338void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples) {
[ba0ba10]339  AVFormatContext *avFormatCtx = s->avFormatCtx;
340  AVCodecContext *avCodecCtx = s->avCodecCtx;
341  AVFrame *avFrame = s->avFrame;
[261836d]342  AVPacket avPacket = s->avPacket;
[f3b93c6]343  av_init_packet (&avPacket);
[ba67cb6]344#ifdef HAVE_AVRESAMPLE
[ba0ba10]345  AVAudioResampleContext *avr = s->avr;
[ba67cb6]346#elif defined(HAVE_SWRESAMPLE)
347  SwrContext *avr = s->avr;
348#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
[877b3b8]349  smpl_t *output = s->output;
[0af9003]350  *read_samples = 0;
[ba0ba10]351
[7760b40]352  do
353  {
354    int err = av_read_frame (avFormatCtx, &avPacket);
[0af9003]355    if (err == AVERROR_EOF) {
356      s->eof = 1;
357      goto beach;
358    }
[7760b40]359    if (err != 0) {
[0af9003]360      char errorstr[256];
361      av_strerror (err, errorstr, sizeof(errorstr));
[8d41c1d]362      AUBIO_ERR("source_avcodec: could not read frame in %s (%s)\n", s->path, errorstr);
[bdf7caf]363      s->eof = 1;
[0af9003]364      goto beach;
[7760b40]365    }
366  } while (avPacket.stream_index != s->selected_stream);
[ba0ba10]367
368  int got_frame = 0;
[2b3c438]369#if FF_API_LAVF_AVCTX
370  int ret = avcodec_send_packet(avCodecCtx, &avPacket);
371  if (ret < 0 && ret != AVERROR_EOF) {
372    AUBIO_ERR("source_avcodec: error when sending packet for %s\n", s->path);
373    goto beach;
374  }
375  ret = avcodec_receive_frame(avCodecCtx, avFrame);
376  if (ret >= 0) {
377    got_frame = 1;
378  }
379  if (ret < 0) {
380    if (ret == AVERROR(EAGAIN)) {
[3e944fe4]381      //AUBIO_WRN("source_avcodec: output is not available right now - user must try to send new input\n");
382      goto beach;
[2b3c438]383    } else if (ret == AVERROR_EOF) {
384      AUBIO_WRN("source_avcodec: the decoder has been fully flushed, and there will be no more output frames\n");
385    } else {
386      AUBIO_ERR("source_avcodec: decoding errors on %s\n", s->path);
387      goto beach;
388    }
389  }
390#else
[ba0ba10]391  int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
392
393  if (len < 0) {
[e0ad269]394    AUBIO_ERR("source_avcodec: error while decoding %s\n", s->path);
[0af9003]395    goto beach;
[ba0ba10]396  }
[2b3c438]397#endif
[ba0ba10]398  if (got_frame == 0) {
[e0ad269]399    AUBIO_WRN("source_avcodec: did not get a frame when reading %s\n", s->path);
[0af9003]400    goto beach;
401  }
402
[ba67cb6]403#ifdef HAVE_AVRESAMPLE
[0af9003]404  int in_linesize = 0;
405  av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
406      avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
[ba0ba10]407  int in_samples = avFrame->nb_samples;
[0af9003]408  int out_linesize = 0;
409  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
[0044b43]410  int out_samples = avresample_convert ( avr,
[0af9003]411        (uint8_t **)&output, out_linesize, max_out_samples,
412        (uint8_t **)avFrame->data, in_linesize, in_samples);
[ba67cb6]413#elif defined(HAVE_SWRESAMPLE)
414  int in_samples = avFrame->nb_samples;
415  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE / avCodecCtx->channels;
416  int out_samples = swr_convert( avr,
417      (uint8_t **)&output, max_out_samples,
418      (const uint8_t **)avFrame->data, in_samples);
419#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
[0af9003]420  if (out_samples <= 0) {
[e0ad269]421    AUBIO_WRN("source_avcodec: no sample found while converting frame (%s)\n", s->path);
[0af9003]422    goto beach;
[ba0ba10]423  }
424
[0af9003]425  *read_samples = out_samples;
426
427beach:
[ba0ba10]428  s->avFormatCtx = avFormatCtx;
429  s->avCodecCtx = avCodecCtx;
430  s->avFrame = avFrame;
[ba67cb6]431#if defined(HAVE_AVRESAMPLE) || defined(HAVE_SWRESAMPLE)
[ba0ba10]432  s->avr = avr;
[ba67cb6]433#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
[ba0ba10]434  s->output = output;
435
[a9c33a2]436  av_packet_unref(&avPacket);
[ba0ba10]437}
438
439void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
440  uint_t i;
[eadd8d5]441  uint_t end = 0;
442  uint_t total_wrote = 0;
[8b210a9]443  // switch from multi
444  if (s->multi == 1) aubio_source_avcodec_reset_resampler(s, 0);
[eadd8d5]445  while (total_wrote < s->hop_size) {
446    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
447    for (i = 0; i < end; i++) {
[3d5cddf]448      read_data->data[i + total_wrote] = s->output[i + s->read_index];
[ba0ba10]449    }
[eadd8d5]450    total_wrote += end;
451    if (total_wrote < s->hop_size) {
452      uint_t avcodec_read = 0;
453      aubio_source_avcodec_readframe(s, &avcodec_read);
454      s->read_samples = avcodec_read;
455      s->read_index = 0;
456      if (s->eof) {
457        break;
[ba0ba10]458      }
[eadd8d5]459    } else {
460      s->read_index += end;
[ba0ba10]461    }
[eadd8d5]462  }
463  if (total_wrote < s->hop_size) {
[eacc55c]464    for (i = total_wrote; i < s->hop_size; i++) {
[eadd8d5]465      read_data->data[i] = 0.;
[ba0ba10]466    }
467  }
[eadd8d5]468  *read = total_wrote;
[ba0ba10]469}
470
471void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
[fcd963a]472  uint_t i,j;
473  uint_t end = 0;
474  uint_t total_wrote = 0;
[8b210a9]475  // switch from mono
476  if (s->multi == 0) aubio_source_avcodec_reset_resampler(s, 1);
[fcd963a]477  while (total_wrote < s->hop_size) {
478    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
479    for (j = 0; j < read_data->height; j++) {
480      for (i = 0; i < end; i++) {
481        read_data->data[j][i + total_wrote] =
482          s->output[(i + s->read_index) * s->input_channels + j];
483      }
484    }
485    total_wrote += end;
486    if (total_wrote < s->hop_size) {
487      uint_t avcodec_read = 0;
488      aubio_source_avcodec_readframe(s, &avcodec_read);
489      s->read_samples = avcodec_read;
490      s->read_index = 0;
491      if (s->eof) {
492        break;
493      }
494    } else {
495      s->read_index += end;
496    }
497  }
498  if (total_wrote < s->hop_size) {
499    for (j = 0; j < read_data->height; j++) {
[eacc55c]500      for (i = total_wrote; i < s->hop_size; i++) {
[fcd963a]501        read_data->data[j][i] = 0.;
502      }
503    }
504  }
505  *read = total_wrote;
[ba0ba10]506}
507
[ae5d58a]508uint_t aubio_source_avcodec_get_samplerate(const aubio_source_avcodec_t * s) {
[ba0ba10]509  return s->samplerate;
510}
511
[ae5d58a]512uint_t aubio_source_avcodec_get_channels(const aubio_source_avcodec_t * s) {
[ba0ba10]513  return s->input_channels;
514}
515
516uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
[50bb325]517  int64_t resampled_pos = (uint_t)ROUND(pos * (s->input_samplerate * 1. / s->samplerate));
518  int64_t min_ts = MAX(resampled_pos - 2000, 0);
519  int64_t max_ts = MIN(resampled_pos + 2000, INT64_MAX);
520  int seek_flags = AVSEEK_FLAG_FRAME | AVSEEK_FLAG_ANY;
[41b4421]521  int ret = AUBIO_FAIL;
522  if (s->avFormatCtx != NULL && s->avr != NULL) {
523    ret = AUBIO_OK;
524  } else {
525    AUBIO_ERR("source_avcodec: failed seeking in %s (file not opened?)", s->path);
526    return ret;
527  }
[4d44531]528  if ((sint_t)pos < 0) {
529    AUBIO_ERR("source_avcodec: could not seek %s at %d (seeking position"
530       " should be >= 0)\n", s->path, pos);
531    return AUBIO_FAIL;
532  }
[41b4421]533  ret = avformat_seek_file(s->avFormatCtx, s->selected_stream,
[50bb325]534      min_ts, resampled_pos, max_ts, seek_flags);
535  if (ret < 0) {
[41b4421]536    AUBIO_ERR("source_avcodec: failed seeking to %d in file %s", pos, s->path);
[50bb325]537  }
538  // reset read status
539  s->eof = 0;
540  s->read_index = 0;
541  s->read_samples = 0;
[ba67cb6]542#ifdef HAVE_AVRESAMPLE
[50bb325]543  // reset the AVAudioResampleContext
544  avresample_close(s->avr);
545  avresample_open(s->avr);
[ba67cb6]546#elif defined(HAVE_SWRESAMPLE)
547  swr_close(s->avr);
548  swr_init(s->avr);
549#endif
[50bb325]550  return ret;
[ba0ba10]551}
552
[2d071ad]553uint_t aubio_source_avcodec_get_duration (aubio_source_avcodec_t * s) {
554  if (s && &(s->avFormatCtx) != NULL) {
555    int64_t duration = s->avFormatCtx->duration;
556    return s->samplerate * ((uint_t)duration / 1e6 );
557  }
558  return 0;
559}
560
[422452b]561uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) {
[ba0ba10]562  if (s->avr != NULL) {
[ba67cb6]563#ifdef HAVE_AVRESAMPLE
[ba0ba10]564    avresample_close( s->avr );
[ba67cb6]565#elif defined(HAVE_SWRESAMPLE)
566    swr_close ( s->avr );
567#endif
[ba0ba10]568    av_free ( s->avr );
569  }
570  s->avr = NULL;
571  if (s->avCodecCtx != NULL) {
[91fa88d]572#ifndef HAVE_AUBIO_LIBAVCODEC_DEPRECATED
573    avcodec_free_context( &s->avCodecCtx );
574#else
[ba0ba10]575    avcodec_close ( s->avCodecCtx );
[91fa88d]576#endif
[ba0ba10]577  }
578  s->avCodecCtx = NULL;
579  if (s->avFormatCtx != NULL) {
[41ebc91]580    avformat_close_input(&s->avFormatCtx);
581    s->avFormatCtx = NULL;
[ba0ba10]582  }
[261836d]583  av_packet_unref(&s->avPacket);
[422452b]584  return AUBIO_OK;
585}
586
587void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
588  if (!s) return;
589  aubio_source_avcodec_close(s);
590  if (s->output != NULL) {
591    av_free(s->output);
592  }
593  s->output = NULL;
594  if (s->avFrame != NULL) {
[cd4c997]595    av_frame_free( &(s->avFrame) );
[422452b]596  }
597  s->avFrame = NULL;
[b6bb265]598  if (s->path) {
599    AUBIO_FREE(s->path);
600  }
601  s->path = NULL;
[ba0ba10]602  AUBIO_FREE(s);
603}
604
[549928e]605#endif /* HAVE_LIBAV */
Note: See TracBrowser for help on using the repository browser.