source: src/io/source_avcodec.c @ 9938200

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

src/io/source_avcodec.c: simplify new_ failure recovery

  • Property mode set to 100644
File size: 19.3 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      goto beach;
391    }
392  } while (avPacket.stream_index != s->selected_stream);
393
394  int got_frame = 0;
395#if FF_API_LAVF_AVCTX
396  int ret = avcodec_send_packet(avCodecCtx, &avPacket);
397  if (ret < 0 && ret != AVERROR_EOF) {
398    AUBIO_ERR("source_avcodec: error when sending packet for %s\n", s->path);
399    goto beach;
400  }
401  ret = avcodec_receive_frame(avCodecCtx, avFrame);
402  if (ret >= 0) {
403    got_frame = 1;
404  }
405  if (ret < 0) {
406    if (ret == AVERROR(EAGAIN)) {
407      AUBIO_WRN("source_avcodec: output is not available right now - user must try to send new input\n");
408    } else if (ret == AVERROR_EOF) {
409      AUBIO_WRN("source_avcodec: the decoder has been fully flushed, and there will be no more output frames\n");
410    } else {
411      AUBIO_ERR("source_avcodec: decoding errors on %s\n", s->path);
412      goto beach;
413    }
414  }
415#else
416  int len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, &avPacket);
417
418  if (len < 0) {
419    AUBIO_ERR("source_avcodec: error while decoding %s\n", s->path);
420    goto beach;
421  }
422#endif
423  if (got_frame == 0) {
424    AUBIO_WRN("source_avcodec: did not get a frame when reading %s\n", s->path);
425    goto beach;
426  }
427
428#ifdef HAVE_AVRESAMPLE
429  int in_linesize = 0;
430  av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
431      avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
432  int in_samples = avFrame->nb_samples;
433  int out_linesize = 0;
434  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
435  int out_samples = avresample_convert ( avr,
436        (uint8_t **)&output, out_linesize, max_out_samples,
437        (uint8_t **)avFrame->data, in_linesize, in_samples);
438#elif defined(HAVE_SWRESAMPLE)
439  int in_samples = avFrame->nb_samples;
440  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE / avCodecCtx->channels;
441  int out_samples = swr_convert( avr,
442      (uint8_t **)&output, max_out_samples,
443      (const uint8_t **)avFrame->data, in_samples);
444#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
445  if (out_samples <= 0) {
446    AUBIO_WRN("source_avcodec: no sample found while converting frame (%s)\n", s->path);
447    goto beach;
448  }
449
450  *read_samples = out_samples;
451
452beach:
453  s->avFormatCtx = avFormatCtx;
454  s->avCodecCtx = avCodecCtx;
455  s->avFrame = avFrame;
456#if defined(HAVE_AVRESAMPLE) || defined(HAVE_SWRESAMPLE)
457  s->avr = avr;
458#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
459  s->output = output;
460
461  av_packet_unref(&avPacket);
462}
463
464void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
465  uint_t i;
466  uint_t end = 0;
467  uint_t total_wrote = 0;
468  // switch from multi
469  if (s->multi == 1) aubio_source_avcodec_reset_resampler(s, 0);
470  while (total_wrote < s->hop_size) {
471    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
472    for (i = 0; i < end; i++) {
473      read_data->data[i + total_wrote] = s->output[i + s->read_index];
474    }
475    total_wrote += end;
476    if (total_wrote < s->hop_size) {
477      uint_t avcodec_read = 0;
478      aubio_source_avcodec_readframe(s, &avcodec_read);
479      s->read_samples = avcodec_read;
480      s->read_index = 0;
481      if (s->eof) {
482        break;
483      }
484    } else {
485      s->read_index += end;
486    }
487  }
488  if (total_wrote < s->hop_size) {
489    for (i = total_wrote; i < s->hop_size; i++) {
490      read_data->data[i] = 0.;
491    }
492  }
493  *read = total_wrote;
494}
495
496void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_data, uint_t * read){
497  uint_t i,j;
498  uint_t end = 0;
499  uint_t total_wrote = 0;
500  // switch from mono
501  if (s->multi == 0) aubio_source_avcodec_reset_resampler(s, 1);
502  while (total_wrote < s->hop_size) {
503    end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
504    for (j = 0; j < read_data->height; j++) {
505      for (i = 0; i < end; i++) {
506        read_data->data[j][i + total_wrote] =
507          s->output[(i + s->read_index) * s->input_channels + j];
508      }
509    }
510    total_wrote += end;
511    if (total_wrote < s->hop_size) {
512      uint_t avcodec_read = 0;
513      aubio_source_avcodec_readframe(s, &avcodec_read);
514      s->read_samples = avcodec_read;
515      s->read_index = 0;
516      if (s->eof) {
517        break;
518      }
519    } else {
520      s->read_index += end;
521    }
522  }
523  if (total_wrote < s->hop_size) {
524    for (j = 0; j < read_data->height; j++) {
525      for (i = total_wrote; i < s->hop_size; i++) {
526        read_data->data[j][i] = 0.;
527      }
528    }
529  }
530  *read = total_wrote;
531}
532
533uint_t aubio_source_avcodec_get_samplerate(const aubio_source_avcodec_t * s) {
534  return s->samplerate;
535}
536
537uint_t aubio_source_avcodec_get_channels(const aubio_source_avcodec_t * s) {
538  return s->input_channels;
539}
540
541uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
542  int64_t resampled_pos = (uint_t)ROUND(pos * (s->input_samplerate * 1. / s->samplerate));
543  int64_t min_ts = MAX(resampled_pos - 2000, 0);
544  int64_t max_ts = MIN(resampled_pos + 2000, INT64_MAX);
545  int seek_flags = AVSEEK_FLAG_FRAME | AVSEEK_FLAG_ANY;
546  int ret = AUBIO_FAIL;
547  if (s->avFormatCtx != NULL && s->avr != NULL) {
548    ret = AUBIO_OK;
549  } else {
550    AUBIO_ERR("source_avcodec: failed seeking in %s (file not opened?)", s->path);
551    return ret;
552  }
553  if ((sint_t)pos < 0) {
554    AUBIO_ERR("source_avcodec: could not seek %s at %d (seeking position"
555       " should be >= 0)\n", s->path, pos);
556    return AUBIO_FAIL;
557  }
558  ret = avformat_seek_file(s->avFormatCtx, s->selected_stream,
559      min_ts, resampled_pos, max_ts, seek_flags);
560  if (ret < 0) {
561    AUBIO_ERR("source_avcodec: failed seeking to %d in file %s", pos, s->path);
562  }
563  // reset read status
564  s->eof = 0;
565  s->read_index = 0;
566  s->read_samples = 0;
567#ifdef HAVE_AVRESAMPLE
568  // reset the AVAudioResampleContext
569  avresample_close(s->avr);
570  avresample_open(s->avr);
571#elif defined(HAVE_SWRESAMPLE)
572  swr_close(s->avr);
573  swr_init(s->avr);
574#endif
575  return ret;
576}
577
578uint_t aubio_source_avcodec_get_duration (aubio_source_avcodec_t * s) {
579  if (s && &(s->avFormatCtx) != NULL) {
580    int64_t duration = s->avFormatCtx->duration;
581    return s->samplerate * ((uint_t)duration / 1e6 );
582  }
583  return 0;
584}
585
586uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) {
587  if (s->avr != NULL) {
588#ifdef HAVE_AVRESAMPLE
589    avresample_close( s->avr );
590#elif defined(HAVE_SWRESAMPLE)
591    swr_close ( s->avr );
592#endif
593    av_free ( s->avr );
594  }
595  s->avr = NULL;
596  if (s->avCodecCtx != NULL) {
597    avcodec_close ( s->avCodecCtx );
598  }
599  s->avCodecCtx = NULL;
600  if (s->avFormatCtx != NULL) {
601    avformat_close_input(&s->avFormatCtx);
602#ifndef HAVE_AUBIO_LIBAVCODEC_DEPRECATED // avoid crash on old libavcodec54
603    avformat_free_context(s->avFormatCtx);
604#endif
605    s->avFormatCtx = NULL;
606  }
607  av_packet_unref(&s->avPacket);
608  return AUBIO_OK;
609}
610
611void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
612  if (!s) return;
613  aubio_source_avcodec_close(s);
614  if (s->output != NULL) {
615    av_free(s->output);
616  }
617  s->output = NULL;
618  if (s->avFrame != NULL) {
619    av_frame_free( &(s->avFrame) );
620  }
621  s->avFrame = NULL;
622  if (s->path) {
623    AUBIO_FREE(s->path);
624  }
625  s->path = NULL;
626  AUBIO_FREE(s);
627}
628
629#endif /* HAVE_LIBAV */
Note: See TracBrowser for help on using the repository browser.