source: src/io/source_avcodec.c @ fbafd2c

sampler
Last change on this file since fbafd2c was fbafd2c, checked in by Paul Brossier <piem@piem.org>, 7 years ago

Merge branch 'master' into sampler

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