source: src/io/source_avcodec.c @ a5004903

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

src/io/source_avcodec.c: more fixes for declaration before assignment

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