src/io/source_avcodec.c: check if we still need max_analyze_duration2 (closes #53...
[aubio.git] / src / io / source_avcodec.c
index 22d2297..cbecb69 100644 (file)
 
 #ifdef HAVE_LIBAV
 
+// determine whether we use libavformat from ffmpeg or from libav
+#define FFMPEG_LIBAVFORMAT (LIBAVFORMAT_VERSION_MICRO > 99 )
+// max_analyze_duration2 was used from ffmpeg libavformat 55.43.100 through 57.2.100
+#define FFMPEG_LIBAVFORMAT_MAX_DUR2 FFMPEG_LIBAVFORMAT && defined( \
+      (LIBAVFORMAT_VERSION_MAJOR == 55 && LIBAVFORMAT_VERSION_MINOR >= 43) \
+      || (LIBAVFORMAT_VERSION_MAJOR == 56) \
+      || (LIBAVFORMAT_VERSION_MAJOR == 57 && LIBAVFORMAT_VERSION_MINOR < 2) \
+      )
+
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
 #include <libavresample/avresample.h>
@@ -63,7 +72,7 @@ struct _aubio_source_avcodec_t {
 void aubio_source_avcodec_reset_resampler(aubio_source_avcodec_t * s, uint_t multi);
 void aubio_source_avcodec_readframe(aubio_source_avcodec_t *s, uint_t * read_samples);
 
-aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplerate, uint_t hop_size) {
+aubio_source_avcodec_t * new_aubio_source_avcodec(const char_t * path, uint_t samplerate, uint_t hop_size) {
   aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
   int err;
   if (path == NULL) {
@@ -81,7 +90,10 @@ aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplera
 
   s->hop_size = hop_size;
   s->channels = 1;
-  s->path = path;
+
+  if (s->path) AUBIO_FREE(s->path);
+  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
+  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
 
   // register all formats and codecs
   av_register_all();
@@ -100,7 +112,11 @@ aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplera
   }
 
   // try to make sure max_analyze_duration is big enough for most songs
+#if FFMPEG_LIBAVFORMAT_MAX_DUR2
+  avFormatCtx->max_analyze_duration2 *= 100;
+#else
   avFormatCtx->max_analyze_duration *= 100;
+#endif
 
   // retrieve stream information
   if ( (err = avformat_find_stream_info(avFormatCtx, NULL)) < 0 ) {
@@ -156,10 +172,10 @@ aubio_source_avcodec_t * new_aubio_source_avcodec(char_t * path, uint_t samplera
   //AUBIO_DBG("input_channels: %d\n", s->input_channels);
 
   if (samplerate == 0) {
-    samplerate = s->input_samplerate;
-    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
+    s->samplerate = s->input_samplerate;
+  } else {
+    s->samplerate = samplerate;
   }
-  s->samplerate = samplerate;
 
   if (s->samplerate >  s->input_samplerate) {
     AUBIO_WRN("source_avcodec: upsampling %s from %d to %d\n", s->path,
@@ -292,7 +308,7 @@ beach:
   s->avr = avr;
   s->output = output;
 
-  av_free_packet(&avPacket);
+  av_packet_unref(&avPacket);
 }
 
 void aubio_source_avcodec_do(aubio_source_avcodec_t * s, fvec_t * read_data, uint_t * read){
@@ -362,11 +378,11 @@ void aubio_source_avcodec_do_multi(aubio_source_avcodec_t * s, fmat_t * read_dat
   *read = total_wrote;
 }
 
-uint_t aubio_source_avcodec_get_samplerate(aubio_source_avcodec_t * s) {
+uint_t aubio_source_avcodec_get_samplerate(const aubio_source_avcodec_t * s) {
   return s->samplerate;
 }
 
-uint_t aubio_source_avcodec_get_channels(aubio_source_avcodec_t * s) {
+uint_t aubio_source_avcodec_get_channels(const aubio_source_avcodec_t * s) {
   return s->input_channels;
 }
 
@@ -390,6 +406,14 @@ uint_t aubio_source_avcodec_seek (aubio_source_avcodec_t * s, uint_t pos) {
   return ret;
 }
 
+uint_t aubio_source_avcodec_get_duration (aubio_source_avcodec_t * s) {
+  if (s && &(s->avFormatCtx) != NULL) {
+    int64_t duration = s->avFormatCtx->duration;
+    return s->samplerate * ((uint_t)duration / 1e6 );
+  }
+  return 0;
+}
+
 uint_t aubio_source_avcodec_close(aubio_source_avcodec_t * s) {
   if (s->avr != NULL) {
     avresample_close( s->avr );
@@ -417,6 +441,7 @@ void del_aubio_source_avcodec(aubio_source_avcodec_t * s){
   if (s->avFrame != NULL) {
     av_frame_free( &(s->avFrame) );
   }
+  if (s->path) AUBIO_FREE(s->path);
   s->avFrame = NULL;
   AUBIO_FREE(s);
 }