shithub: aubio

Download patch

ref: ba67cb6195be92a3a04e0b1787576119b9156ed7
parent: d82e7a4d925ffd7ea318384f14653d90d3bf8fe7
author: Paul Brossier <piem@piem.org>
date: Thu Mar 16 20:57:31 EDT 2017

src/io/source_avcodec.c: add libswresample

--- a/src/io/source_avcodec.c
+++ b/src/io/source_avcodec.c
@@ -24,7 +24,11 @@
 
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
+#if defined(HAVE_SWRESAMPLE)
+#include <libswresample/swresample.h>
+#elif defined(HAVE_AVRESAMPLE)
 #include <libavresample/avresample.h>
+#endif
 #include <libavutil/opt.h>
 #include <stdlib.h>
 
@@ -68,7 +72,11 @@
   AVCodecContext *avCodecCtx;
   AVFrame *avFrame;
   AVPacket avPacket;
+#ifdef HAVE_AVRESAMPLE
   AVAudioResampleContext *avr;
+#elif defined(HAVE_SWRESAMPLE)
+  SwrContext *avr;
+#endif
   smpl_t *output;
   uint_t read_samples;
   uint_t read_index;
@@ -276,8 +284,13 @@
     int64_t input_layout = av_get_default_channel_layout(s->input_channels);
     uint_t output_channels = multi ? s->input_channels : 1;
     int64_t output_layout = av_get_default_channel_layout(output_channels);
+#ifdef HAVE_AVRESAMPLE
     AVAudioResampleContext *avr = avresample_alloc_context();
     AVAudioResampleContext *oldavr = s->avr;
+#elif defined(HAVE_SWRESAMPLE)
+    SwrContext *avr = swr_alloc();
+    SwrContext *oldavr = s->avr;
+#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
 
     av_opt_set_int(avr, "in_channel_layout",  input_layout,           0);
     av_opt_set_int(avr, "out_channel_layout", output_layout,          0);
@@ -292,7 +305,11 @@
     // TODO: use planar?
     //av_opt_set_int(avr, "out_sample_fmt",     AV_SAMPLE_FMT_FLTP,      0);
     int err;
+#ifdef HAVE_AVRESAMPLE
     if ( ( err = avresample_open(avr) ) < 0) {
+#elif defined(HAVE_SWRESAMPLE)
+    if ( ( err = swr_init(avr) ) < 0) {
+#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
       char errorstr[256];
       av_strerror (err, errorstr, sizeof(errorstr));
       AUBIO_ERR("source_avcodec: Could not open AVAudioResampleContext for %s (%s)\n",
@@ -302,7 +319,11 @@
     }
     s->avr = avr;
     if (oldavr != NULL) {
+#ifdef HAVE_AVRESAMPLE
       avresample_close( oldavr );
+#elif defined(HAVE_SWRESAMPLE)
+      swr_close ( oldavr );
+#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
       av_free ( oldavr );
       oldavr = NULL;
     }
@@ -316,7 +337,11 @@
   AVFrame *avFrame = s->avFrame;
   AVPacket avPacket = s->avPacket;
   av_init_packet (&avPacket);
+#ifdef HAVE_AVRESAMPLE
   AVAudioResampleContext *avr = s->avr;
+#elif defined(HAVE_SWRESAMPLE)
+  SwrContext *avr = s->avr;
+#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
   smpl_t *output = s->output;
   *read_samples = 0;
 
@@ -369,6 +394,7 @@
     goto beach;
   }
 
+#ifdef HAVE_AVRESAMPLE
   int in_linesize = 0;
   av_samples_get_buffer_size(&in_linesize, avCodecCtx->channels,
       avFrame->nb_samples, avCodecCtx->sample_fmt, 1);
@@ -378,6 +404,13 @@
   int out_samples = avresample_convert ( avr,
         (uint8_t **)&output, out_linesize, max_out_samples,
         (uint8_t **)avFrame->data, in_linesize, in_samples);
+#elif defined(HAVE_SWRESAMPLE)
+  int in_samples = avFrame->nb_samples;
+  int max_out_samples = AUBIO_AVCODEC_MAX_BUFFER_SIZE / avCodecCtx->channels;
+  int out_samples = swr_convert( avr,
+      (uint8_t **)&output, max_out_samples,
+      (const uint8_t **)avFrame->data, in_samples);
+#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
   if (out_samples <= 0) {
     AUBIO_WRN("source_avcodec: no sample found while converting frame (%s)\n", s->path);
     goto beach;
@@ -389,7 +422,9 @@
   s->avFormatCtx = avFormatCtx;
   s->avCodecCtx = avCodecCtx;
   s->avFrame = avFrame;
+#if defined(HAVE_AVRESAMPLE) || defined(HAVE_SWRESAMPLE)
   s->avr = avr;
+#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
   s->output = output;
 
   av_packet_unref(&avPacket);
@@ -498,9 +533,14 @@
   s->eof = 0;
   s->read_index = 0;
   s->read_samples = 0;
+#ifdef HAVE_AVRESAMPLE
   // reset the AVAudioResampleContext
   avresample_close(s->avr);
   avresample_open(s->avr);
+#elif defined(HAVE_SWRESAMPLE)
+  swr_close(s->avr);
+  swr_init(s->avr);
+#endif
   return ret;
 }
 
@@ -514,7 +554,11 @@
 
 uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) {
   if (s->avr != NULL) {
+#ifdef HAVE_AVRESAMPLE
     avresample_close( s->avr );
+#elif defined(HAVE_SWRESAMPLE)
+    swr_close ( s->avr );
+#endif
     av_free ( s->avr );
   }
   s->avr = NULL;