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

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

[source_avcodec] add support for AVChannelLayout (ffmpeg 5.1)

  • Property mode set to 100644
File size: 19.4 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#include <libswresample/swresample.h>
28#include <libavutil/opt.h>
29
30// determine whether we use libavformat from ffmpeg or from libav
31#define FFMPEG_LIBAVFORMAT (LIBAVFORMAT_VERSION_MICRO > 99 )
32// max_analyze_duration2 was used from ffmpeg libavformat 55.43.100 -> 57.2.100
33#define FFMPEG_LIBAVFORMAT_MAX_DUR2 FFMPEG_LIBAVFORMAT && ( \
34      (LIBAVFORMAT_VERSION_MAJOR == 55 && LIBAVFORMAT_VERSION_MINOR >= 43) \
35      || (LIBAVFORMAT_VERSION_MAJOR == 56) \
36      || (LIBAVFORMAT_VERSION_MAJOR == 57 && LIBAVFORMAT_VERSION_MINOR < 2) \
37      )
38
39// backward compatibility with libavcodec55
40#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57,0,0)
41#define HAVE_AUBIO_LIBAVCODEC_DEPRECATED 1
42#endif
43
44#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,3,102)
45#define HAVE_AUBIO_LIBAVCODEC_TIMEBASE_FIX 1
46#endif
47
48#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
49#warning "libavcodec < 56 is deprecated"
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
55#include "aubio_priv.h"
56#include "fvec.h"
57#include "fmat.h"
58#include "ioutils.h"
59#include "source_avcodec.h"
60
61#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 56, 0)
62#define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
63#else
64#define AUBIO_AVCODEC_MAX_BUFFER_SIZE AV_INPUT_BUFFER_MIN_SIZE
65#endif
66
67#if LIBAVCODEC_VERSION_MAJOR >= 59
68#define FF_API_LAVF_AVCTX 1
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  SwrContext *avr;
91  smpl_t *output;
92  uint_t read_samples;
93  uint_t read_index;
94  sint_t selected_stream;
95  uint_t eof;
96};
97
98// create or re-create the context when _do or _do_multi is called
99void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s);
100// actually read a frame
101void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s,
102    uint_t * read_samples);
103
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
119aubio_source_avcodec_t * new_aubio_source_avcodec(const char_t * path,
120    uint_t samplerate, uint_t hop_size) {
121  aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
122  AVFormatContext *avFormatCtx = NULL;
123  AVCodecContext *avCodecCtx = NULL;
124  AVFrame *avFrame = NULL;
125#if FF_API_INIT_PACKET
126  AVPacket *avPacket = NULL;
127#endif
128  sint_t selected_stream = -1;
129#if FF_API_LAVF_AVCTX
130  AVCodecParameters *codecpar;
131#endif
132
133  const AVCodec *codec;
134  uint_t i;
135  int err;
136  if (path == NULL) {
137    AUBIO_ERR("source_avcodec: Aborted opening null path\n");
138    goto beach;
139  }
140  if ((sint_t)samplerate < 0) {
141    AUBIO_ERR("source_avcodec: Can not open %s with samplerate %d\n",
142        path, samplerate);
143    goto beach;
144  }
145  if ((sint_t)hop_size <= 0) {
146    AUBIO_ERR("source_avcodec: Can not open %s with hop_size %d\n",
147        path, hop_size);
148    goto beach;
149  }
150
151  s->hop_size = hop_size;
152  s->channels = 1;
153
154  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
155  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
156
157#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58,0,0)
158  // register all formats and codecs
159  av_register_all();
160#endif
161
162  if (aubio_source_avcodec_has_network_url(s)) {
163    avformat_network_init();
164  }
165
166  // try opening the file and get some info about it
167  avFormatCtx = NULL;
168  if ( (err = avformat_open_input(&avFormatCtx, s->path, NULL, NULL) ) < 0 ) {
169    char errorstr[256];
170    av_strerror (err, errorstr, sizeof(errorstr));
171    AUBIO_ERR("source_avcodec: Failed opening %s (%s)\n", s->path, errorstr);
172    goto beach;
173  }
174
175  // try to make sure max_analyze_duration is big enough for most songs
176#if FFMPEG_LIBAVFORMAT_MAX_DUR2
177  avFormatCtx->max_analyze_duration2 *= 100;
178#else
179  avFormatCtx->max_analyze_duration *= 100;
180#endif
181
182  // retrieve stream information
183  if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
184    char errorstr[256];
185    av_strerror (err, errorstr, sizeof(errorstr));
186    AUBIO_ERR("source_avcodec: Could not find stream information "
187        "for %s (%s)\n", s->path, errorstr);
188    goto beach;
189  }
190
191  // dump information about file onto standard error
192  //av_dump_format(avFormatCtx, 0, s->path, 0);
193
194  // look for the first audio stream
195  for (i = 0; i < avFormatCtx->nb_streams; i++) {
196#if FF_API_LAVF_AVCTX
197    if (avFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
198#else
199    if (avFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
200#endif
201      if (selected_stream == -1) {
202        selected_stream = i;
203      } else {
204        AUBIO_WRN("source_avcodec: More than one audio stream in %s, "
205            "taking the first one\n", s->path);
206      }
207    }
208  }
209  if (selected_stream == -1) {
210    AUBIO_ERR("source_avcodec: No audio stream in %s\n", s->path);
211    goto beach;
212  }
213  //AUBIO_DBG("Taking stream %d in file %s\n", selected_stream, s->path);
214  s->selected_stream = selected_stream;
215
216#if FF_API_LAVF_AVCTX
217  codecpar = avFormatCtx->streams[selected_stream]->codecpar;
218  if (codecpar == NULL) {
219    AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path);
220    goto beach;
221  }
222  codec = avcodec_find_decoder(codecpar->codec_id);
223
224  /* Allocate a codec context for the decoder */
225  avCodecCtx = avcodec_alloc_context3(codec);
226  if (!avCodecCtx) {
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);
230    goto beach;
231  }
232#else
233  avCodecCtx = avFormatCtx->streams[selected_stream]->codec;
234  codec = avcodec_find_decoder(avCodecCtx->codec_id);
235#endif
236  if (codec == NULL) {
237    AUBIO_ERR("source_avcodec: Could not find decoder for %s", s->path);
238    goto beach;
239  }
240
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) {
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);
247    goto beach;
248  }
249#if HAVE_AUBIO_LIBAVCODEC_TIMEBASE_FIX
250  // avoids 'skipped frames warning' with avecodec < 58, deprecated after
251  av_codec_set_pkt_timebase(avCodecCtx,
252      avFormatCtx->streams[selected_stream]->time_base);
253#endif
254#endif
255
256  if ( ( err = avcodec_open2(avCodecCtx, codec, NULL) ) < 0) {
257    char errorstr[256];
258    av_strerror (err, errorstr, sizeof(errorstr));
259    AUBIO_ERR("source_avcodec: Could not load codec for %s (%s)\n", s->path,
260        errorstr);
261    goto beach;
262  }
263
264  /* get input specs */
265  s->input_samplerate = avCodecCtx->sample_rate;
266#ifdef AVUTIL_CHANNEL_LAYOUT_H
267  s->input_channels   = avCodecCtx->ch_layout.nb_channels;
268#else
269  s->input_channels   = avCodecCtx->channels;
270#endif
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    SwrContext *avr = swr_alloc();
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
345    int64_t input_layout = av_get_default_channel_layout(s->input_channels);
346    int64_t output_layout = av_get_default_channel_layout(s->input_channels);
347
348    av_opt_set_int(avr, "in_channel_layout",  input_layout,              0);
349    av_opt_set_int(avr, "out_channel_layout", output_layout,             0);
350#endif
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);
353    av_opt_set_int(avr, "in_sample_fmt",      s->avCodecCtx->sample_fmt, 0);
354#if HAVE_AUBIO_DOUBLE
355    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_DBL,         0);
356#else
357    av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLT,         0);
358#endif
359    // TODO: use planar?
360    //av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLTP,      0);
361    if ( ( err = swr_init(avr) ) < 0)
362    {
363      char errorstr[256];
364      av_strerror (err, errorstr, sizeof(errorstr));
365      AUBIO_ERR("source_avcodec: Could not open resampling context"
366         " for %s (%s)\n", s->path, errorstr);
367      return;
368    }
369    s->avr = avr;
370  }
371}
372
373void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s,
374    uint_t * read_samples)
375{
376  AVFormatContext *avFormatCtx = s->avFormatCtx;
377  AVCodecContext *avCodecCtx = s->avCodecCtx;
378  AVFrame *avFrame = s->avFrame;
379#if FF_API_INIT_PACKET
380  AVPacket *avPacket = s->avPacket;
381#else
382  AVPacket *avPacket = &s->avPacket;
383#endif
384  SwrContext *avr = s->avr;
385  int got_frame = 0;
386  int in_samples = avFrame->nb_samples;
387#ifdef AVUTIL_CHANNEL_LAYOUT_H
388  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE / avCodecCtx->ch_layout.nb_channels;
389#else
390  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE / avCodecCtx->channels;
391#endif
392  int out_samples = 0;
393  smpl_t *output = s->output;
394#ifndef FF_API_LAVF_AVCTX
395  int len = 0;
396#else
397  int ret = 0;
398#endif
399#ifndef FF_API_INIT_PACKET
400  av_init_packet (avPacket);
401#endif
402  *read_samples = 0;
403
404  do
405  {
406    int err = av_read_frame (avFormatCtx, avPacket);
407    if (err == AVERROR_EOF) {
408      s->eof = 1;
409      goto beach;
410    }
411    if (err != 0) {
412      char errorstr[256];
413      av_strerror (err, errorstr, sizeof(errorstr));
414      AUBIO_ERR("source_avcodec: could not read frame in %s (%s)\n",
415          s->path, errorstr);
416      s->eof = 1;
417      goto beach;
418    }
419  } while (avPacket->stream_index != s->selected_stream);
420
421#if FF_API_LAVF_AVCTX
422  ret = avcodec_send_packet(avCodecCtx, avPacket);
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)) {
433      //AUBIO_WRN("source_avcodec: output is not available right now - "
434      //    "user must try to send new input\n");
435      goto beach;
436    } else if (ret == AVERROR_EOF) {
437      AUBIO_WRN("source_avcodec: the decoder has been fully flushed, "
438          "and there will be no more output frames\n");
439    } else {
440      AUBIO_ERR("source_avcodec: decoding errors on %s\n", s->path);
441      goto beach;
442    }
443  }
444#else
445  len = avcodec_decode_audio4(avCodecCtx, avFrame, &got_frame, avPacket);
446
447  if (len < 0) {
448    AUBIO_ERR("source_avcodec: error while decoding %s\n", s->path);
449    goto beach;
450  }
451#endif
452  if (got_frame == 0) {
453    AUBIO_WRN("source_avcodec: did not get a frame when reading %s\n",
454        s->path);
455    goto beach;
456  }
457
458#if LIBAVUTIL_VERSION_MAJOR > 52
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) {
465    AUBIO_WRN ("source_avcodec: trying to read from %d channel(s),"
466        "but configured for %d; is '%s' corrupt?\n",
467        frame_channels, s->input_channels, s->path);
468    goto beach;
469  }
470#else
471#warning "avutil < 53 is deprecated, crashes might occur on corrupt files"
472#endif
473
474  in_samples = avFrame->nb_samples;
475  max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE;
476  if (frame_channels > 0) max_out_samples /= frame_channels;
477  out_samples = swr_convert( avr,
478      (uint8_t **)&output, max_out_samples,
479      (const uint8_t **)avFrame->data, in_samples);
480  if (out_samples < 0) {
481    AUBIO_WRN("source_avcodec: error while resampling %s (%d)\n",
482        s->path, out_samples);
483    goto beach;
484  }
485
486  *read_samples = out_samples;
487
488beach:
489  av_packet_unref(avPacket);
490}
491
492void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data,
493    uint_t * read) {
494  uint_t i, j;
495  uint_t end = 0;
496  uint_t total_wrote = 0;
497  uint_t length = aubio_source_validate_input_length("source_avcodec", s->path,
498      s->hop_size, read_data->length);
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  }
505  while (total_wrote < length) {
506    end = MIN(s->read_samples - s->read_index, length - total_wrote);
507    for (i = 0; i < end; i++) {
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;
514    }
515    total_wrote += end;
516    if (total_wrote < length) {
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;
523      }
524    } else {
525      s->read_index += end;
526    }
527  }
528
529  aubio_source_pad_output(read_data, total_wrote);
530
531  *read = total_wrote;
532}
533
534void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s,
535    fmat_t * read_data, uint_t * read) {
536  uint_t i,j;
537  uint_t end = 0;
538  uint_t total_wrote = 0;
539  uint_t length = aubio_source_validate_input_length("source_avcodec", s->path,
540      s->hop_size, read_data->length);
541  uint_t channels = aubio_source_validate_input_channels("source_avcodec",
542      s->path, s->input_channels, read_data->height);
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  }
549  while (total_wrote < length) {
550    end = MIN(s->read_samples - s->read_index, length - total_wrote);
551    for (j = 0; j < channels; j++) {
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;
558    if (total_wrote < length) {
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  }
570
571  aubio_source_pad_multi_output(read_data, s->input_channels, total_wrote);
572
573  *read = total_wrote;
574}
575
576uint_t aubio_source_avcodec_get_samplerate(const aubio_source_avcodec_t * s) {
577  return s->samplerate;
578}
579
580uint_t aubio_source_avcodec_get_channels(const aubio_source_avcodec_t * s) {
581  return s->input_channels;
582}
583
584uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
585  int64_t resampled_pos =
586    (uint_t)ROUND(pos * (s->input_samplerate * 1. / s->samplerate));
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;
590  int ret = AUBIO_FAIL;
591  if (s->avFormatCtx != NULL && s->avr != NULL) {
592    ret = AUBIO_OK;
593  } else {
594    AUBIO_ERR("source_avcodec: failed seeking in %s (file not opened?)",
595        s->path);
596    return ret;
597  }
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  }
603  ret = avformat_seek_file(s->avFormatCtx, s->selected_stream,
604      min_ts, resampled_pos, max_ts, seek_flags);
605  if (ret < 0) {
606    AUBIO_ERR("source_avcodec: failed seeking to %d in file %s",
607        pos, s->path);
608  }
609  // reset read status
610  s->eof = 0;
611  s->read_index = 0;
612  s->read_samples = 0;
613  swr_close(s->avr);
614  swr_init(s->avr);
615  return ret;
616}
617
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
626uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) {
627  if (s->avr != NULL) {
628    swr_close ( s->avr );
629    swr_free ( &s->avr );
630  }
631  s->avr = NULL;
632  if (s->avCodecCtx != NULL) {
633#ifndef HAVE_AUBIO_LIBAVCODEC_DEPRECATED
634    avcodec_free_context( &s->avCodecCtx );
635#else
636    avcodec_close ( s->avCodecCtx );
637#endif
638  }
639  s->avCodecCtx = NULL;
640  if (s->avFormatCtx != NULL) {
641    avformat_close_input(&s->avFormatCtx);
642    s->avFormatCtx = NULL;
643  }
644#if FF_API_INIT_PACKET
645  if (s->avPacket) {
646    av_packet_unref(s->avPacket);
647  }
648#else
649  av_packet_unref(&s->avPacket);
650#endif
651  return AUBIO_OK;
652}
653
654void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
655  AUBIO_ASSERT(s);
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) {
662    av_frame_free( &(s->avFrame) );
663  }
664  s->avFrame = NULL;
665#if FF_API_INIT_PACKET
666  if (s->avPacket != NULL) {
667    av_packet_free( &(s->avPacket) );
668  }
669  s->avPacket = NULL;
670#endif
671  if (s->path) {
672    AUBIO_FREE(s->path);
673  }
674  s->path = NULL;
675  AUBIO_FREE(s);
676}
677
678#endif /* HAVE_LIBAV */
Note: See TracBrowser for help on using the repository browser.