source: src/io/source_avcodec.c @ 0b947f9

Last change on this file since 0b947f9 was 0b947f9, checked in by Paul Brossier <piem@piem.org>, 13 months ago

[source_avcodec] add support for AVChannelLayout (ffmpeg 5.1)

  • Property mode set to 100644
File size: 19.4 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#include <libswresample/swresample.h>
[dcde285]28#include <libavutil/opt.h>
29
[a9c33a2]30// determine whether we use libavformat from ffmpeg or from libav
31#define FFMPEG_LIBAVFORMAT (LIBAVFORMAT_VERSION_MICRO > 99 )
[de9178e]32// max_analyze_duration2 was used from ffmpeg libavformat 55.43.100 -> 57.2.100
[9ac77ac]33#define FFMPEG_LIBAVFORMAT_MAX_DUR2 FFMPEG_LIBAVFORMAT && ( \
[a9c33a2]34      (LIBAVFORMAT_VERSION_MAJOR == 55 && LIBAVFORMAT_VERSION_MINOR >= 43) \
35      || (LIBAVFORMAT_VERSION_MAJOR == 56) \
36      || (LIBAVFORMAT_VERSION_MAJOR == 57 && LIBAVFORMAT_VERSION_MINOR < 2) \
37      )
[2a32644]38
[a2d628b]39// backward compatibility with libavcodec55
[8250214]40#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57,0,0)
[1504b7c]41#define HAVE_AUBIO_LIBAVCODEC_DEPRECATED 1
[8250214]42#endif
43
[2434566]44#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,3,102)
45#define HAVE_AUBIO_LIBAVCODEC_TIMEBASE_FIX 1
46#endif
47
[8250214]48#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
49#warning "libavcodec < 56 is deprecated"
[a2d628b]50#define av_frame_alloc  avcodec_alloc_frame
51#define av_frame_free avcodec_free_frame
52#define av_packet_unref av_free_packet
53#endif
54
[ba0ba10]55#include "aubio_priv.h"
56#include "fvec.h"
57#include "fmat.h"
[9b5aa50]58#include "ioutils.h"
[ba0ba10]59#include "source_avcodec.h"
60
[5690daf]61#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 56, 0)
[0af9003]62#define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
[5690daf]63#else
64#define AUBIO_AVCODEC_MAX_BUFFER_SIZE AV_INPUT_BUFFER_MIN_SIZE
65#endif
[ba0ba10]66
[8a05420]67#if LIBAVCODEC_VERSION_MAJOR >= 59
68#define FF_API_LAVF_AVCTX 1
69#endif
70
[ba0ba10]71struct _aubio_source_avcodec_t {
72  uint_t hop_size;
73  uint_t samplerate;
74  uint_t channels;
75
76  // some data about the file
77  char_t *path;
[d3b9fe4]78  uint_t input_samplerate;
79  uint_t input_channels;
[ba0ba10]80
81  // avcodec stuff
82  AVFormatContext *avFormatCtx;
83  AVCodecContext *avCodecCtx;
84  AVFrame *avFrame;
[cdfe9ce]85#if FF_API_INIT_PACKET
86  AVPacket *avPacket;
87#else
[261836d]88  AVPacket avPacket;
[cdfe9ce]89#endif
[ba67cb6]90  SwrContext *avr;
[2f89ef4]91  smpl_t *output;
[d3b9fe4]92  uint_t read_samples;
93  uint_t read_index;
[7760b40]94  sint_t selected_stream;
[eadd8d5]95  uint_t eof;
[ba0ba10]96};
97
[de9178e]98// create or re-create the context when _do or _do_multi is called
[ac97e80d]99void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s);
[de9178e]100// actually read a frame
101void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s,
102    uint_t * read_samples);
[fcd963a]103
[6769586]104uint_t aubio_source_avcodec_has_network_url(aubio_source_avcodec_t *s);
105
106uint_t aubio_source_avcodec_has_network_url(aubio_source_avcodec_t *s) {
107  char proto[20], authorization[256], hostname[128], uripath[256];
108  int proto_size = 20, authorization_size = 256, hostname_size = 128,
109      *port_ptr = 0, path_size = 256;
110  av_url_split(proto, proto_size, authorization, authorization_size, hostname,
111      hostname_size, port_ptr, uripath, path_size, s->path);
112  if (strlen(proto)) {
113    return 1;
114  }
115  return 0;
116}
117
118
[de9178e]119aubio_source_avcodec_t * new_aubio_source_avcodec(const char_t * path,
120    uint_t samplerate, uint_t hop_size) {
[ba0ba10]121  aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
[724303e]122  AVFormatContext *avFormatCtx = NULL;
123  AVCodecContext *avCodecCtx = NULL;
124  AVFrame *avFrame = NULL;
[cdfe9ce]125#if FF_API_INIT_PACKET
126  AVPacket *avPacket = NULL;
127#endif
[5bd9a2b]128  sint_t selected_stream = -1;
[de23e58]129#if FF_API_LAVF_AVCTX
130  AVCodecParameters *codecpar;
131#endif
[cdfe9ce]132
[128acfd]133  const AVCodec *codec;
[50f39f5]134  uint_t i;
[ba0ba10]135  int err;
136  if (path == NULL) {
[491e6ea]137    AUBIO_ERR("source_avcodec: Aborted opening null path\n");
[b294b3e]138    goto beach;
139  }
140  if ((sint_t)samplerate < 0) {
[de9178e]141    AUBIO_ERR("source_avcodec: Can not open %s with samplerate %d\n",
142        path, samplerate);
[b294b3e]143    goto beach;
144  }
145  if ((sint_t)hop_size <= 0) {
[de9178e]146    AUBIO_ERR("source_avcodec: Can not open %s with hop_size %d\n",
147        path, hop_size);
[b294b3e]148    goto beach;
[ba0ba10]149  }
150
151  s->hop_size = hop_size;
152  s->channels = 1;
[b643a33]153
[d2be104]154  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
155  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[ba0ba10]156
[d62fb90]157#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58,0,0)
[ba0ba10]158  // register all formats and codecs
159  av_register_all();
[d62fb90]160#endif
[ba0ba10]161
[6769586]162  if (aubio_source_avcodec_has_network_url(s)) {
163    avformat_network_init();
164  }
[eadd8d5]165
[d75e2d5]166  // try opening the file and get some info about it
[ba0ba10]167  avFormatCtx = NULL;
168  if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
[0af9003]169    char errorstr[256];
170    av_strerror (err, errorstr, sizeof(errorstr));
[491e6ea]171    AUBIO_ERR("source_avcodec: Failed opening %s (%s)\n", s->path, errorstr);
[ba0ba10]172    goto beach;
173  }
174
[6abb4de]175  // try to make sure max_analyze_duration is big enough for most songs
[a9c33a2]176#if FFMPEG_LIBAVFORMAT_MAX_DUR2
[2a32644]177  avFormatCtx->max_analyze_duration2 *= 100;
178#else
[6abb4de]179  avFormatCtx->max_analyze_duration *= 100;
[2a32644]180#endif
[6abb4de]181
[ba0ba10]182  // retrieve stream information
183  if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
[0af9003]184    char errorstr[256];
185    av_strerror (err, errorstr, sizeof(errorstr));
[de9178e]186    AUBIO_ERR("source_avcodec: Could not find stream information "
187        "for %s (%s)\n", s->path, errorstr);
[ba0ba10]188    goto beach;
189  }
190
[1fe3ac2]191  // dump information about file onto standard error
[ba0ba10]192  //av_dump_format(avFormatCtx, 0, s->path, 0);
193
[1fe3ac2]194  // look for the first audio stream
[ba0ba10]195  for (i = 0; i < avFormatCtx->nb_streams; i++) {
[2b3c438]196#if FF_API_LAVF_AVCTX
197    if (avFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
198#else
[ba0ba10]199    if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
[2b3c438]200#endif
[ba0ba10]201      if (selected_stream == -1) {
202        selected_stream = i;
203      } else {
[491e6ea]204        AUBIO_WRN("source_avcodec: More than one audio stream in %s, "
[1fe3ac2]205            "taking the first one\n", s->path);
[ba0ba10]206      }
207    }
208  }
209  if (selected_stream == -1) {
[491e6ea]210    AUBIO_ERR("source_avcodec: No audio stream in %s\n", s->path);
[ba0ba10]211    goto beach;
[d3b9fe4]212  }
[eadd8d5]213  //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
[7760b40]214  s->selected_stream = selected_stream;
[ba0ba10]215
[2b3c438]216#if FF_API_LAVF_AVCTX
[de23e58]217  codecpar = avFormatCtx->streams[selected_stream]->codecpar;
[2b3c438]218  if (codecpar == NULL) {
219    AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path);
220    goto beach;
221  }
[50f39f5]222  codec = avcodec_find_decoder(codecpar->codec_id);
[2b3c438]223
224  /* Allocate a codec context for the decoder */
225  avCodecCtx = avcodec_alloc_context3(codec);
226  if (!avCodecCtx) {
[de9178e]227    AUBIO_ERR("source_avcodec: Failed to allocate the %s codec context "
228        "for path %s\n", av_get_media_type_string(AVMEDIA_TYPE_AUDIO),
229        s->path);
[2b3c438]230    goto beach;
231  }
232#else
[d3b9fe4]233  avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
[de23e58]234  codec = avcodec_find_decoder(avCodecCtx->codec_id);
[2b3c438]235#endif
[ba0ba10]236  if (codec == NULL) {
[491e6ea]237    AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path);
[ba0ba10]238    goto beach;
239  }
240
[2b3c438]241#if FF_API_LAVF_AVCTX
242  /* Copy codec parameters from input stream to output codec context */
243  if ((err = avcodec_parameters_to_context(avCodecCtx, codecpar)) < 0) {
[de9178e]244    AUBIO_ERR("source_avcodec: Failed to copy %s codec parameters to "
245        "decoder context for %s\n",
246        av_get_media_type_string(AVMEDIA_TYPE_AUDIO), s->path);
[2b3c438]247    goto beach;
248  }
[2434566]249#if HAVE_AUBIO_LIBAVCODEC_TIMEBASE_FIX
250  // avoids 'skipped frames warning' with avecodec < 58, deprecated after
[39c8d08]251  av_codec_set_pkt_timebase(avCodecCtx,
252      avFormatCtx->streams[selected_stream]->time_base);
[2b3c438]253#endif
[0f5837d]254#endif
[2b3c438]255
[ba0ba10]256  if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
[0af9003]257    char errorstr[256];
258    av_strerror (err, errorstr, sizeof(errorstr));
[de9178e]259    AUBIO_ERR("source_avcodec: Could not load codec for %s (%s)\n", s->path,
260        errorstr);
[ba0ba10]261    goto beach;
262  }
263
264  /* get input specs */
265  s->input_samplerate = avCodecCtx->sample_rate;
[0b947f9]266#ifdef AVUTIL_CHANNEL_LAYOUT_H
267  s->input_channels   = avCodecCtx->ch_layout.nb_channels;
268#else
[ba0ba10]269  s->input_channels   = avCodecCtx->channels;
[0b947f9]270#endif
[ba0ba10]271  //AUBIO_DBG("input_samplerate: %d\n", s->input_samplerate);
272  //AUBIO_DBG("input_channels: %d\n", s->input_channels);
273
274  if (samplerate == 0) {
[26a6af7]275    s->samplerate = s->input_samplerate;
276  } else {
277    s->samplerate = samplerate;
[ba0ba10]278  }
279
[018e511]280  if (s->samplerate >  s->input_samplerate) {
[491e6ea]281    AUBIO_WRN("source_avcodec: upsampling %s from %d to %d\n", s->path,
[018e511]282        s->input_samplerate, s->samplerate);
283  }
284
[cd4c997]285  avFrame = av_frame_alloc();
[ba0ba10]286  if (!avFrame) {
[491e6ea]287    AUBIO_ERR("source_avcodec: Could not allocate frame for (%s)\n", s->path);
[cdfe9ce]288    goto beach;
[ba0ba10]289  }
290
[cdfe9ce]291#if FF_API_INIT_PACKET
292  avPacket = av_packet_alloc();
293  if (!avPacket) {
294    AUBIO_ERR("source_avcodec: Could not allocate packet for (%s)\n", s->path);
295    goto beach;
296  }
297#endif
298
[d3b9fe4]299  /* allocate output for avr */
[de9178e]300  s->output = (smpl_t *)av_malloc(AUBIO_AVCODEC_MAX_BUFFER_SIZE
301      * sizeof(smpl_t));
[ba0ba10]302
303  s->read_samples = 0;
304  s->read_index = 0;
305
306  s->avFormatCtx = avFormatCtx;
307  s->avCodecCtx = avCodecCtx;
308  s->avFrame = avFrame;
[cdfe9ce]309#if FF_API_INIT_PACKET
310  s->avPacket = avPacket;
311#endif
[fcd963a]312
[ac97e80d]313  aubio_source_avcodec_reset_resampler(s);
[ba0ba10]314
[a81b12a]315  if (s->avr == NULL) goto beach;
316
[eadd8d5]317  s->eof = 0;
318
[ba0ba10]319  //av_log_set_level(AV_LOG_QUIET);
320
321  return s;
322
323beach:
[fcd963a]324  //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
325  //    s->path, s->samplerate, s->hop_size);
[ba0ba10]326  del_aubio_source_avcodec(s);
327  return NULL;
328}
329
[ac97e80d]330void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s)
[de9178e]331{
[2f89ef4]332  // create or reset resampler to/from mono/multi-channel
[ac97e80d]333  if ( s->avr == NULL ) {
[808c8525]334    int err;
[245deea]335    SwrContext *avr = swr_alloc();
[0b947f9]336#ifdef AVUTIL_CHANNEL_LAYOUT_H
337    AVChannelLayout input_layout;
338    AVChannelLayout output_layout;
339    av_channel_layout_default(&input_layout, s->input_channels);
340    av_channel_layout_default(&output_layout, s->input_channels);
341
342    av_opt_set_chlayout(avr, "in_channel_layout",  &input_layout,        0);
343    av_opt_set_chlayout(avr, "out_channel_layout", &output_layout,       0);
344#else
[61ecd1a]345    int64_t input_layout = av_get_default_channel_layout(s->input_channels);
[8a4ccf7]346    int64_t output_layout = av_get_default_channel_layout(s->input_channels);
[61ecd1a]347
[de9178e]348    av_opt_set_int(avr, "in_channel_layout",  input_layout,              0);
349    av_opt_set_int(avr, "out_channel_layout", output_layout,             0);
[0b947f9]350#endif
[de9178e]351    av_opt_set_int(avr, "in_sample_rate",     s->input_samplerate,       0);
352    av_opt_set_int(avr, "out_sample_rate",    s->samplerate,             0);
[61ecd1a]353    av_opt_set_int(avr, "in_sample_fmt",      s->avCodecCtx->sample_fmt, 0);
[2f89ef4]354#if HAVE_AUBIO_DOUBLE
[de9178e]355    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_DBL,         0);
[2f89ef4]356#else
[de9178e]357    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLT,         0);
[2f89ef4]358#endif
359    // TODO: use planar?
360    //av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLTP,      0);
[c3e98d7]361    if ( ( err = swr_init(avr) ) < 0)
362    {
[61ecd1a]363      char errorstr[256];
364      av_strerror (err, errorstr, sizeof(errorstr));
[de9178e]365      AUBIO_ERR("source_avcodec: Could not open resampling context"
366         " for %s (%s)\n", s->path, errorstr);
[61ecd1a]367      return;
368    }
369    s->avr = avr;
370  }
371}
372
[de9178e]373void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s,
374    uint_t * read_samples)
375{
[ba0ba10]376  AVFormatContext *avFormatCtx = s->avFormatCtx;
377  AVCodecContext *avCodecCtx = s->avCodecCtx;
378  AVFrame *avFrame = s->avFrame;
[cdfe9ce]379#if FF_API_INIT_PACKET
380  AVPacket *avPacket = s->avPacket;
381#else
382  AVPacket *avPacket = &s->avPacket;
383#endif
[ba67cb6]384  SwrContext *avr = s->avr;
[808c8525]385  int got_frame = 0;
386  int in_samples = avFrame->nb_samples;
[0b947f9]387#ifdef AVUTIL_CHANNEL_LAYOUT_H
388  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE / avCodecCtx->ch_layout.nb_channels;
389#else
[808c8525]390  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE / avCodecCtx->channels;
[0b947f9]391#endif
[808c8525]392  int out_samples = 0;
[877b3b8]393  smpl_t *output = s->output;
[1a6f2de]394#ifndef FF_API_LAVF_AVCTX
395  int len = 0;
[9956027]396#else
397  int ret = 0;
[1a6f2de]398#endif
[cdfe9ce]399#ifndef FF_API_INIT_PACKET
400  av_init_packet (avPacket);
401#endif
[0af9003]402  *read_samples = 0;
[ba0ba10]403
[7760b40]404  do
405  {
[cdfe9ce]406    int err = av_read_frame (avFormatCtx, avPacket);
[0af9003]407    if (err == AVERROR_EOF) {
408      s->eof = 1;
409      goto beach;
410    }
[7760b40]411    if (err != 0) {
[0af9003]412      char errorstr[256];
413      av_strerror (err, errorstr, sizeof(errorstr));
[de9178e]414      AUBIO_ERR("source_avcodec: could not read frame in %s (%s)\n",
415          s->path, errorstr);
[bdf7caf]416      s->eof = 1;
[0af9003]417      goto beach;
[7760b40]418    }
[cdfe9ce]419  } while (avPacket->stream_index != s->selected_stream);
[ba0ba10]420
[2b3c438]421#if FF_API_LAVF_AVCTX
[cdfe9ce]422  ret = avcodec_send_packet(avCodecCtx, avPacket);
[2b3c438]423  if (ret < 0 && ret != AVERROR_EOF) {
424    AUBIO_ERR("source_avcodec: error when sending packet for %s\n", s->path);
425    goto beach;
426  }
427  ret = avcodec_receive_frame(avCodecCtx, avFrame);
428  if (ret >= 0) {
429    got_frame = 1;
430  }
431  if (ret < 0) {
432    if (ret == AVERROR(EAGAIN)) {
[de9178e]433      //AUBIO_WRN("source_avcodec: output is not available right now - "
434      //    "user must try to send new input\n");
[3e944fe4]435      goto beach;
[2b3c438]436    } else if (ret == AVERROR_EOF) {
[de9178e]437      AUBIO_WRN("source_avcodec: the decoder has been fully flushed, "
438          "and there will be no more output frames\n");
[2b3c438]439    } else {
440      AUBIO_ERR("source_avcodec: decoding errors on %s\n", s->path);
441      goto beach;
442    }
443  }
444#else
[cdfe9ce]445  len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, avPacket);
[ba0ba10]446
447  if (len < 0) {
[e0ad269]448    AUBIO_ERR("source_avcodec: error while decoding %s\n", s->path);
[0af9003]449    goto beach;
[ba0ba10]450  }
[2b3c438]451#endif
[ba0ba10]452  if (got_frame == 0) {
[de9178e]453    AUBIO_WRN("source_avcodec: did not get a frame when reading %s\n",
454        s->path);
[0af9003]455    goto beach;
456  }
457
[58fe197]458#if LIBAVUTIL_VERSION_MAJOR > 52
[0b947f9]459#ifdef AVUTIL_CHANNEL_LAYOUT_H
460  int frame_channels = avFrame->ch_layout.nb_channels;
461#else
462  int frame_channels = avFrame->channels;
463#endif
464  if (frame_channels != (sint_t)s->input_channels) {
[265fe9a]465    AUBIO_WRN ("source_avcodec: trying to read from %d channel(s),"
[de9178e]466        "but configured for %d; is '%s' corrupt?\n",
[0b947f9]467        frame_channels, s->input_channels, s->path);
[265fe9a]468    goto beach;
469  }
[138cb1f]470#else
471#warning "avutil < 53 is deprecated, crashes might occur on corrupt files"
[58fe197]472#endif
[265fe9a]473
[808c8525]474  in_samples = avFrame->nb_samples;
[0b947f9]475  max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
476  if (frame_channels > 0) max_out_samples /= frame_channels;
[808c8525]477  out_samples = swr_convert( avr,
[ba67cb6]478      (uint8_t **)&output, max_out_samples,
479      (const uint8_t **)avFrame->data, in_samples);
[79dc9ad]480  if (out_samples < 0) {
481    AUBIO_WRN("source_avcodec: error while resampling %s (%d)\n",
482        s->path, out_samples);
[0af9003]483    goto beach;
[ba0ba10]484  }
485
[0af9003]486  *read_samples = out_samples;
487
488beach:
[cdfe9ce]489  av_packet_unref(avPacket);
[ba0ba10]490}
491
[de9178e]492void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data,
493    uint_t * read) {
[ac97e80d]494  uint_t i, j;
[eadd8d5]495  uint_t end = 0;
496  uint_t total_wrote = 0;
[9b5aa50]497  uint_t length = aubio_source_validate_input_length("source_avcodec", s->path,
498      s->hop_size, read_data->length);
[fc633f3]499  if (!s->avr || !s->avFormatCtx || !s->avCodecCtx) {
500    AUBIO_ERR("source_avcodec: could not read from %s (file was closed)\n",
501        s->path);
502    *read= 0;
503    return;
504  }
[9b5aa50]505  while (total_wrote < length) {
506    end = MIN(s->read_samples - s->read_index, length - total_wrote);
[eadd8d5]507    for (i = 0; i < end; i++) {
[8a4ccf7]508      read_data->data[i + total_wrote] = 0.;
509      for (j = 0; j < s->input_channels; j++) {
510        read_data->data[i + total_wrote] +=
511          s->output[(i + s->read_index) * s->input_channels + j];
512      }
513      read_data->data[i + total_wrote] *= 1./s->input_channels;
[ba0ba10]514    }
[eadd8d5]515    total_wrote += end;
[9b5aa50]516    if (total_wrote < length) {
[eadd8d5]517      uint_t avcodec_read = 0;
518      aubio_source_avcodec_readframe(s, &avcodec_read);
519      s->read_samples = avcodec_read;
520      s->read_index = 0;
521      if (s->eof) {
522        break;
[ba0ba10]523      }
[eadd8d5]524    } else {
525      s->read_index += end;
[ba0ba10]526    }
[eadd8d5]527  }
[2510248]528
529  aubio_source_pad_output(read_data, total_wrote);
530
[eadd8d5]531  *read = total_wrote;
[ba0ba10]532}
533
[de9178e]534void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s,
535    fmat_t * read_data, uint_t * read) {
[fcd963a]536  uint_t i,j;
537  uint_t end = 0;
538  uint_t total_wrote = 0;
[d4927c2]539  uint_t length = aubio_source_validate_input_length("source_avcodec", s->path,
[9b5aa50]540      s->hop_size, read_data->length);
[d4927c2]541  uint_t channels = aubio_source_validate_input_channels("source_avcodec",
[9b5aa50]542      s->path, s->input_channels, read_data->height);
[fc633f3]543  if (!s->avr || !s->avFormatCtx || !s->avCodecCtx) {
544    AUBIO_ERR("source_avcodec: could not read from %s (file was closed)\n",
545        s->path);
546    *read= 0;
547    return;
548  }
[9b5aa50]549  while (total_wrote < length) {
550    end = MIN(s->read_samples - s->read_index, length - total_wrote);
551    for (j = 0; j < channels; j++) {
[fcd963a]552      for (i = 0; i < end; i++) {
553        read_data->data[j][i + total_wrote] =
554          s->output[(i + s->read_index) * s->input_channels + j];
555      }
556    }
557    total_wrote += end;
[9b5aa50]558    if (total_wrote < length) {
[fcd963a]559      uint_t avcodec_read = 0;
560      aubio_source_avcodec_readframe(s, &avcodec_read);
561      s->read_samples = avcodec_read;
562      s->read_index = 0;
563      if (s->eof) {
564        break;
565      }
566    } else {
567      s->read_index += end;
568    }
569  }
[2510248]570
571  aubio_source_pad_multi_output(read_data, s->input_channels, total_wrote);
572
[fcd963a]573  *read = total_wrote;
[ba0ba10]574}
575
[ae5d58a]576uint_t aubio_source_avcodec_get_samplerate(const aubio_source_avcodec_t * s) {
[ba0ba10]577  return s->samplerate;
578}
579
[ae5d58a]580uint_t aubio_source_avcodec_get_channels(const aubio_source_avcodec_t * s) {
[ba0ba10]581  return s->input_channels;
582}
583
584uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
[de9178e]585  int64_t resampled_pos =
586    (uint_t)ROUND(pos * (s->input_samplerate * 1. / s->samplerate));
[50bb325]587  int64_t min_ts = MAX(resampled_pos - 2000, 0);
588  int64_t max_ts = MIN(resampled_pos + 2000, INT64_MAX);
589  int seek_flags = AVSEEK_FLAG_FRAME | AVSEEK_FLAG_ANY;
[41b4421]590  int ret = AUBIO_FAIL;
591  if (s->avFormatCtx != NULL && s->avr != NULL) {
592    ret = AUBIO_OK;
593  } else {
[de9178e]594    AUBIO_ERR("source_avcodec: failed seeking in %s (file not opened?)",
595        s->path);
[41b4421]596    return ret;
597  }
[4d44531]598  if ((sint_t)pos < 0) {
599    AUBIO_ERR("source_avcodec: could not seek %s at %d (seeking position"
600       " should be >= 0)\n", s->path, pos);
601    return AUBIO_FAIL;
602  }
[41b4421]603  ret = avformat_seek_file(s->avFormatCtx, s->selected_stream,
[50bb325]604      min_ts, resampled_pos, max_ts, seek_flags);
605  if (ret < 0) {
[de9178e]606    AUBIO_ERR("source_avcodec: failed seeking to %d in file %s",
607        pos, s->path);
[50bb325]608  }
609  // reset read status
610  s->eof = 0;
611  s->read_index = 0;
612  s->read_samples = 0;
[ba67cb6]613  swr_close(s->avr);
614  swr_init(s->avr);
[50bb325]615  return ret;
[ba0ba10]616}
617
[2d071ad]618uint_t aubio_source_avcodec_get_duration (aubio_source_avcodec_t * s) {
619  if (s && &(s->avFormatCtx) != NULL) {
620    int64_t duration = s->avFormatCtx->duration;
621    return s->samplerate * ((uint_t)duration / 1e6 );
622  }
623  return 0;
624}
625
[422452b]626uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) {
[ba0ba10]627  if (s->avr != NULL) {
[ba67cb6]628    swr_close ( s->avr );
[dc7362fd]629    swr_free ( &s->avr );
[ba0ba10]630  }
631  s->avr = NULL;
632  if (s->avCodecCtx != NULL) {
[91fa88d]633#ifndef HAVE_AUBIO_LIBAVCODEC_DEPRECATED
634    avcodec_free_context( &s->avCodecCtx );
635#else
[ba0ba10]636    avcodec_close ( s->avCodecCtx );
[91fa88d]637#endif
[ba0ba10]638  }
639  s->avCodecCtx = NULL;
640  if (s->avFormatCtx != NULL) {
[41ebc91]641    avformat_close_input(&s->avFormatCtx);
642    s->avFormatCtx = NULL;
[ba0ba10]643  }
[cdfe9ce]644#if FF_API_INIT_PACKET
645  if (s->avPacket) {
646    av_packet_unref(s->avPacket);
647  }
648#else
[261836d]649  av_packet_unref(&s->avPacket);
[cdfe9ce]650#endif
[422452b]651  return AUBIO_OK;
652}
653
654void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
[c0a1906]655  AUBIO_ASSERT(s);
[422452b]656  aubio_source_avcodec_close(s);
657  if (s->output != NULL) {
658    av_free(s->output);
659  }
660  s->output = NULL;
661  if (s->avFrame != NULL) {
[cd4c997]662    av_frame_free( &(s->avFrame) );
[422452b]663  }
664  s->avFrame = NULL;
[cdfe9ce]665#if FF_API_INIT_PACKET
666  if (s->avPacket != NULL) {
667    av_packet_free( &(s->avPacket) );
668  }
669  s->avPacket = NULL;
670#endif
[b6bb265]671  if (s->path) {
672    AUBIO_FREE(s->path);
673  }
674  s->path = NULL;
[ba0ba10]675  AUBIO_FREE(s);
676}
677
[549928e]678#endif /* HAVE_LIBAV */
Note: See TracBrowser for help on using the repository browser.