source: src/io/source_avcodec.c @ 7ba59f1

feature/crepe
Last change on this file since 7ba59f1 was cdfe9ce, checked in by Paul Brossier <piem@piem.org>, 3 years ago

[source_avcodec] avoid deprecation warning with latest avcodec api (58.134.100)

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