shithub: sox

Download patch

ref: 9abe8822ba60526b4f02fb0fbf4634f4eb9bf51d
parent: cf7ad263712747461a49a8758f26a064a7042980
author: Mans Rullgard <mans@mansr.com>
date: Thu Aug 13 20:57:49 EDT 2020

amr: replace 3GPP reference library with vo-amrwbenc

The 3GPP reference code has licensing restrictions and isn't commonly
available as a package.  Between them, the opencore and vo-amrwbenc
cover the same functionality, so use those instead.

--- a/INSTALL
+++ b/INSTALL
@@ -17,7 +17,7 @@
 their licence types, is as follows:
 
 OpencoreAMR-NB/WB http://sourceforge.net/projects/opencore-amr  Apache
-AMR-NB/WB         http://www.penguin.cz/~utx/amr        See library web page
+VisualOn AMR-WB   http://sourceforge.net/projects/opencore-amr  Apache
 AO                http://xiph.org/ao                    GPL
 FLAC              http://flac.sourceforge.net           BSD
 LADSPA            http://www.ladspa.org                 LGPL + plugins' licence
--- a/configure.ac
+++ b/configure.ac
@@ -448,14 +448,13 @@
 dnl When enable_dl_amrbw, do not let add libraries to be linked in
 dnl since they will be dlopen()'ed instead.
 ac_sox_save_AMRWB_LIBS="$AMRWB_LIBS"
-tmp_using_amrwb=$opt_default
+tmp_using_amrwb=no
 AC_CHECK_HEADERS(opencore-amrwb/dec_if.h,
-		 [AC_CHECK_LIB(opencore-amrwb, D_IF_init, 
-		  AMRWB_LIBS="$AMRWB_LIBS -lopencore-amrwb", tmp_using_amrwb=no)],
-		  [AC_CHECK_HEADERS(amrwb/dec.h, 
-		   [AC_CHECK_LIB(amrwb, D_IF_init, 
-		    AMRWB_LIBS="$AMRWB_LIBS -lamrwb",tmp_using_amrwb=no)], 
-		    tmp_using_amrwb=no)])
+    [AC_CHECK_LIB(opencore-amrwb, D_IF_init,
+        [AMRWB_LIBS="$AMRWB_LIBS -lopencore-amrwb"; tmp_using_amrwb=$opt_default])])
+AC_CHECK_HEADERS(vo-amrwbenc/enc_if.h,
+    [AC_CHECK_LIB(vo-amrwbenc, E_IF_init,
+		    AMRWB_LIBS="$AMRWB_LIBS -lvo-amrwbenc"; tmp_using_amrwb=$opt_default)])
 AC_ARG_ENABLE(dl_amrwb,
 	      AS_HELP_STRING([--enable-dl-amrwb], 
 			     [Dlopen amrbw instead of linking in.]),
@@ -479,10 +478,7 @@
 AC_CHECK_HEADERS(opencore-amrnb/interf_dec.h, 
 		 [AC_CHECK_LIB(opencore-amrnb, Decoder_Interface_init, 
 		  AMRNB_LIBS="$AMRNB_LIBS -lopencore-amrnb", tmp_using_amrnb=no)],
-		  [AC_CHECK_HEADER(amrnb/sp_dec.h, 
-		   [AC_CHECK_LIB(amrnb, Decoder_Interface_init, 
-		    AMRNB_LIBS="$AMRNB_LIBS -lamrnb", tmp_using_amrnb=no)], 
-		    tmp_using_amrnb=no)])
+		  [tmp_using_amrnb=no])
 AC_ARG_ENABLE(dl_amrnb,
 	      AS_HELP_STRING([--enable-dl-amrnb], 
 			     [Dlopen amrnb instead of linking in.]),
--- a/src/amr-nb.c
+++ b/src/amr-nb.c
@@ -16,11 +16,10 @@
  */
  
 /*
- * In order to use the AMR format with SoX, you need to have an AMR
- * library installed at SoX build time. Currently, the SoX build system
- * recognizes two AMR implementations, in the following order:
- *   http://opencore-amr.sourceforge.net/
- *   http://ftp.penguin.cz/pub/users/utx/amr/
+ * In order to use the AMR format with SoX, you need to have an
+ * AMR library installed at SoX build time. The SoX build system
+ * recognizes the AMR implementations available from
+ * http://opencore-amr.sourceforge.net/
  */
 
 #include "sox_i.h"
@@ -54,10 +53,8 @@
 
 /* OpenCore definitions: */
 
-#if defined(HAVE_OPENCORE_AMRNB_INTERF_DEC_H) || defined(DL_AMRNB)
-  #define AMR_OPENCORE 1
-  #define AMR_OPENCORE_ENABLE_ENCODE 1
-#endif
+#define AMR_OPENCORE 1
+#define AMR_OPENCORE_ENABLE_ENCODE 1
 
 #define AMR_OPENCORE_FUNC_ENTRIES(f,x) \
   AMR_FUNC(f,x, void*, Encoder_Interface_init,   (int dtx)) \
@@ -67,17 +64,17 @@
   AMR_FUNC(f,x, void,  Decoder_Interface_Decode, (void* state, const unsigned char* in, short* out, int bfi)) \
   AMR_FUNC(f,x, void,  Decoder_Interface_exit,   (void* state)) \
 
-#define AmrOpencoreEncoderInit() \
+#define AmrEncoderInit() \
   Encoder_Interface_init(1)
-#define AmrOpencoreEncoderEncode(state, mode, in, out, forceSpeech) \
+#define AmrEncoderEncode(state, mode, in, out, forceSpeech) \
   Encoder_Interface_Encode(state, mode, in, out, forceSpeech)
-#define AmrOpencoreEncoderExit(state) \
+#define AmrEncoderExit(state) \
   Encoder_Interface_exit(state)
-#define AmrOpencoreDecoderInit() \
+#define AmrDecoderInit() \
   Decoder_Interface_init()
-#define AmrOpencoreDecoderDecode(state, in, out, bfi) \
+#define AmrDecoderDecode(state, in, out, bfi) \
   Decoder_Interface_Decode(state, in, out, bfi)
-#define AmrOpencoreDecoderExit(state) \
+#define AmrDecoderExit(state) \
   Decoder_Interface_exit(state)
 
 #define AMR_OPENCORE_DESC "amr-nb OpenCore library"
@@ -86,45 +83,6 @@
 #ifdef DL_AMRWB
   "libopencore-amrnb",
   "libopencore-amrnb-0",
-#endif
-  NULL
-};
-
-/* 3GPP (reference implementation) definitions: */
-
-#if !defined(HAVE_OPENCORE_AMRNB_INTERF_DEC_H) || defined(DL_AMRNB)
-  #define AMR_GP3 1
-#endif
-
-#define AMR_GP3_FUNC_ENTRIES(f,x) \
-  AMR_FUNC(f,x, void*, VADxEncoder_Interface_init,      (int dtx, char vad2_code)) \
-  AMR_FUNC(f,x, int,   GP3VADxEncoder_Interface_Encode, (void* state, enum amrnb_mode mode, short* in, unsigned char* out, int forceSpeech, char vad2_code)) \
-  AMR_FUNC(f,x, void,  Encoder_Interface_exit,          (void* state)) \
-  AMR_FUNC(f,x, void*, Decoder_Interface_init,          (void)) \
-  AMR_FUNC(f,x, void,  GP3Decoder_Interface_Decode,     (void* state, unsigned char* in, short* out, int bfi)) \
-  AMR_FUNC(f,x, void,  Decoder_Interface_exit,          (void* state)) \
-
-#define AmrGp3EncoderInit() \
-  VADxEncoder_Interface_init(1, 0)
-#define AmrGp3EncoderEncode(state, mode, in, out, forceSpeech) \
-  GP3VADxEncoder_Interface_Encode(state, mode, in, out, forceSpeech, 0)
-#define AmrGp3EncoderExit(state) \
-  Encoder_Interface_exit(state)
-#define AmrGp3DecoderInit() \
-  Decoder_Interface_init()
-#define AmrGp3DecoderDecode(state, in, out, bfi) \
-  GP3Decoder_Interface_Decode(state, in, out, bfi)
-#define AmrGp3DecoderExit(state) \
-  Decoder_Interface_exit(state)
-
-#define AMR_GP3_DESC "amr-nb 3GPP reference library"
-static const char* const amr_gp3_library_names[] =
-{
-#ifdef DL_AMRWB
-  "libamrnb-3",
-  "libamrnb",
-  "amrnb",
-  "cygamrnb-3",
 #endif
   NULL
 };
--- a/src/amr-wb.c
+++ b/src/amr-wb.c
@@ -16,11 +16,10 @@
  */
  
 /*
- * In order to use the AMR format with SoX, you need to have an AMR
- * library installed at SoX build time. Currently, the SoX build system
- * recognizes two AMR implementations, in the following order:
- *   http://opencore-amr.sourceforge.net/
- *   http://ftp.penguin.cz/pub/users/utx/amr/
+ * In order to use the AMR format with SoX, you need to have an
+ * AMR library installed at SoX build time. The SoX build system
+ * recognizes the AMR implementations available from
+ * http://opencore-amr.sourceforge.net/
  */
 
 #include "sox_i.h"
@@ -33,7 +32,7 @@
 #define amr_magic amrwb_magic
 #define amr_priv_t amrwb_priv_t
 #define amr_opencore_funcs amrwb_opencore_funcs
-#define amr_gp3_funcs amrwb_gp3_funcs
+#define amr_vo_funcs amrwb_vo_funcs
 
 #define AMR_CODED_MAX       61 /* NB_SERIAL_MAX */
 #define AMR_ENCODING        SOX_ENCODING_AMR_WB
@@ -62,11 +61,11 @@
   AMR_FUNC(f,x, void,  D_IF_decode, (void* state, const unsigned char* in, short* out, int bfi)) \
   AMR_FUNC(f,x, void,  D_IF_exit,   (void* state)) \
 
-#define AmrOpencoreDecoderInit() \
+#define AmrDecoderInit() \
   D_IF_init()
-#define AmrOpencoreDecoderDecode(state, in, out, bfi) \
+#define AmrDecoderDecode(state, in, out, bfi) \
   D_IF_decode(state, in, out, bfi)
-#define AmrOpencoreDecoderExit(state) \
+#define AmrDecoderExit(state) \
   D_IF_exit(state)
 
 #define AMR_OPENCORE_DESC "amr-wb OpenCore library"
@@ -79,41 +78,30 @@
   NULL
 };
 
-/* 3GPP (reference implementation) definitions: */
+/* VO definitions: */
 
-#if !defined(HAVE_OPENCORE_AMRWB_DEC_IF_H) || defined(DL_AMRWB)
-  #define AMR_GP3 1
+#if defined(HAVE_VO_AMRWBENC_ENC_IF_H) || defined(DL_AMRWB)
+  #define AMR_VO 1
 #endif
 
-#define AMR_GP3_FUNC_ENTRIES(f,x) \
+#define AMR_VO_FUNC_ENTRIES(f,x) \
   AMR_FUNC(f,x, void*, E_IF_init,     (void)) \
-  AMR_FUNC(f,x, int,   GP3E_IF_encode,(void* state, int16_t mode, int16_t* in, uint8_t* out, int16_t dtx)) \
+  AMR_FUNC(f,x, int,   E_IF_encode,(void* state, int16_t mode, int16_t* in, uint8_t* out, int16_t dtx)) \
   AMR_FUNC(f,x, void,  E_IF_exit,     (void* state)) \
-  AMR_FUNC(f,x, void*, D_IF_init,     (void)) \
-  AMR_FUNC(f,x, void,  GP3D_IF_decode,(void* state, uint8_t* in, int16_t* out, int32_t bfi)) \
-  AMR_FUNC(f,x, void,  D_IF_exit,     (void* state)) \
 
-#define AmrGp3EncoderInit() \
+#define AmrEncoderInit() \
   E_IF_init()
-#define AmrGp3EncoderEncode(state, mode, in, out, forceSpeech) \
-  GP3E_IF_encode(state, mode, in, out, forceSpeech)
-#define AmrGp3EncoderExit(state) \
+#define AmrEncoderEncode(state, mode, in, out, forceSpeech) \
+  E_IF_encode(state, mode, in, out, forceSpeech)
+#define AmrEncoderExit(state) \
   E_IF_exit(state)
-#define AmrGp3DecoderInit() \
-  D_IF_init()
-#define AmrGp3DecoderDecode(state, in, out, bfi) \
-  GP3D_IF_decode(state, in, out, bfi)
-#define AmrGp3DecoderExit(state) \
-  D_IF_exit(state)
 
-#define AMR_GP3_DESC "amr-wb 3GPP reference library"
-static const char* const amr_gp3_library_names[] =
+#define AMR_VO_DESC "amr-wb VisualOn library"
+static const char* const amr_vo_library_names[] =
 {
 #ifdef DL_AMRWB
-  "libamrwb-3",
-  "libamrwb",
-  "amrwb",
-  "cygamrwb-3",
+  "libvo-amrwbenc",
+  "libvo-amrwbenc-0",
 #endif
   NULL
 };
--- a/src/amr.h
+++ b/src/amr.h
@@ -28,33 +28,22 @@
 
 #endif /* AMR_OPENCORE */
 
-#ifdef AMR_GP3
+#ifdef AMR_VO
 
-LSX_DLENTRIES_TO_FUNCTIONS(AMR_GP3_FUNC_ENTRIES)
+LSX_DLENTRIES_TO_FUNCTIONS(AMR_VO_FUNC_ENTRIES)
 
-typedef struct amr_gp3_funcs {
-  LSX_DLENTRIES_TO_PTRS(AMR_GP3_FUNC_ENTRIES, amr_dl);
-} amr_gp3_funcs;
+typedef struct amr_vo_funcs {
+  LSX_DLENTRIES_TO_PTRS(AMR_VO_FUNC_ENTRIES, amr_dl);
+} amr_vo_funcs;
 
-#endif /* AMR_GP3 */
+#endif /* AMR_VO */
 
-#if defined(AMR_OPENCORE) && defined (AMR_GP3)
-  #define AMR_CALL(p, opencoreFunc, gp3Func, args) \
-    ((p)->loaded_opencore ? ((p)->opencore.opencoreFunc args) : ((p)->gp3.gp3Func args))
-  #if AMR_OPENCORE_ENABLE_ENCODE
-    #define AMR_CALL_ENCODER AMR_CALL
-  #else
-    #define AMR_CALL_ENCODER(p, opencoreFunc, gp3Func, args) \
-      ((p)->gp3.gp3Func args)
-  #endif
-#elif defined(AMR_OPENCORE)
-  #define AMR_CALL(p, opencoreFunc, gp3Func, args) \
-    ((p)->opencore.opencoreFunc args)
-  #define AMR_CALL_ENCODER AMR_CALL
-#elif defined(AMR_GP3)
-  #define AMR_CALL(p, opencoreFunc, gp3Func, args) \
-    ((p)->gp3.gp3Func args)
-  #define AMR_CALL_ENCODER AMR_CALL
+#define AMR_CALL(p, func, args) ((p)->opencore.func args)
+
+#ifdef AMR_VO
+  #define AMR_CALL_ENCODER(p, func, args) ((p)->vo.func args)
+#else
+  #define AMR_CALL_ENCODER(p, func, args) ((p)->opencore.func args)
 #endif
 
 typedef struct amr_priv_t {
@@ -61,16 +50,16 @@
   void* state;
   unsigned mode;
   size_t pcm_index;
-  int loaded_opencore;
 #ifdef AMR_OPENCORE
   amr_opencore_funcs opencore;
 #endif /* AMR_OPENCORE */
-#ifdef AMR_GP3
-  amr_gp3_funcs gp3;
-#endif /* AMR_GP3 */
+#ifdef AMR_VO
+  amr_vo_funcs vo;
+#endif /* AMR_VO */
   short pcm[AMR_FRAME];
 } priv_t;
 
+#ifdef AMR_OPENCORE
 static size_t decode_1_frame(sox_format_t * ft)
 {
   priv_t * p = (priv_t *)ft->priv;
@@ -87,9 +76,10 @@
   n--;
   if (lsx_readbuf(ft, &coded[1], n) != n)
     return AMR_FRAME;
-  AMR_CALL(p, AmrOpencoreDecoderDecode, AmrGp3DecoderDecode, (p->state, coded, p->pcm, 0));
+  AMR_CALL(p, AmrDecoderDecode, (p->state, coded, p->pcm, 0));
   return 0;
 }
+#endif
 
 static int openlibrary(priv_t* p, int encoding)
 {
@@ -108,42 +98,28 @@
       amr_opencore_library_names,
       open_library_result);
     if (!open_library_result)
-    {
-      p->loaded_opencore = 1;
       return SOX_SUCCESS;
-    }
+    lsx_fail("Unable to open " AMR_OPENCORE_DESC);
+    return SOX_EOF;
   }
-  else
-  {
-      lsx_report("Not attempting to load " AMR_OPENCORE_DESC " because it does not support encoding.");
-  }
 #endif /* AMR_OPENCORE */
 
-#ifdef AMR_GP3
-  LSX_DLLIBRARY_TRYOPEN(
-      0,
-      &p->gp3,
-      amr_dl,
-      AMR_GP3_FUNC_ENTRIES,
-      AMR_GP3_DESC,
-      amr_gp3_library_names,
-      open_library_result);
-  if (!open_library_result)
-    return SOX_SUCCESS;
-#endif /* AMR_GP3 */
+#ifdef AMR_VO
+  if (encoding) {
+    LSX_DLLIBRARY_TRYOPEN(
+        0,
+        &p->vo,
+        amr_dl,
+        AMR_VO_FUNC_ENTRIES,
+        AMR_VO_DESC,
+        amr_vo_library_names,
+        open_library_result);
+    if (!open_library_result)
+      return SOX_SUCCESS;
+    lsx_fail("Unable to open " AMR_VO_DESC);
+  }
+#endif /* AMR_VO */
 
-  lsx_fail(
-      "Unable to open "
-#ifdef AMR_OPENCORE
-      AMR_OPENCORE_DESC
-#endif
-#if defined(AMR_OPENCORE) && defined(AMR_GP3)
-      " or "
-#endif
-#ifdef AMR_GP3
-      AMR_GP3_DESC
-#endif
-      ".");
   return SOX_EOF;
 }
 
@@ -152,11 +128,12 @@
 #ifdef AMR_OPENCORE
   LSX_DLLIBRARY_CLOSE(&p->opencore, amr_dl);
 #endif
-#ifdef AMR_GP3
-  LSX_DLLIBRARY_CLOSE(&p->gp3, amr_dl);
+#ifdef AMR_VO
+  LSX_DLLIBRARY_CLOSE(&p->vo, amr_dl);
 #endif
 }
 
+#ifdef AMR_OPENCORE
 static size_t amr_duration_frames(sox_format_t * ft)
 {
   off_t      frame_size, data_start_offset = lsx_tell(ft);
@@ -178,9 +155,14 @@
   lsx_seeki(ft, data_start_offset, SEEK_SET);
   return frames;
 }
+#endif
 
 static int startread(sox_format_t * ft)
 {
+#if !defined(AMR_OPENCORE)
+  lsx_fail_errno(ft, SOX_EOF, "SoX was compiled without AMR-WB decoding support.");
+  return SOX_EOF;
+#else
   priv_t * p = (priv_t *)ft->priv;
   char buffer[sizeof(amr_magic) - 1];
   int open_library_result;
@@ -197,7 +179,7 @@
     return open_library_result;
 
   p->pcm_index = AMR_FRAME;
-  p->state = AMR_CALL(p, AmrOpencoreDecoderInit, AmrGp3DecoderInit, ());
+  p->state = AMR_CALL(p, AmrDecoderInit, ());
   if (!p->state)
   {
       closelibrary(p);
@@ -211,8 +193,11 @@
   ft->signal.length = ft->signal.length != SOX_IGNORE_LENGTH && ft->seekable?
     (size_t)(amr_duration_frames(ft) * .02 * ft->signal.rate +.5) : SOX_UNSPEC;
   return SOX_SUCCESS;
+#endif
 }
 
+#ifdef AMR_OPENCORE
+
 static size_t read_samples(sox_format_t * ft, sox_sample_t * buf, size_t len)
 {
   priv_t * p = (priv_t *)ft->priv;
@@ -231,14 +216,21 @@
 static int stopread(sox_format_t * ft)
 {
   priv_t * p = (priv_t *)ft->priv;
-  AMR_CALL(p, AmrOpencoreDecoderExit, AmrGp3DecoderExit, (p->state));
+  AMR_CALL(p, AmrDecoderExit, (p->state));
   closelibrary(p);
   return SOX_SUCCESS;
 }
 
+#else
+
+#define read_samples NULL
+#define stopread NULL
+
+#endif
+
 static int startwrite(sox_format_t * ft)
 {
-#if !defined(AMR_GP3) && !AMR_OPENCORE_ENABLE_ENCODE
+#if !defined(AMR_VO) && !AMR_OPENCORE_ENABLE_ENCODE
   lsx_fail_errno(ft, SOX_EOF, "SoX was compiled without AMR-WB encoding support.");
   return SOX_EOF;
 #else
@@ -258,7 +250,7 @@
   if (open_library_result != SOX_SUCCESS)
     return open_library_result;
 
-  p->state = AMR_CALL_ENCODER(p, AmrOpencoreEncoderInit, AmrGp3EncoderInit, ());
+  p->state = AMR_CALL_ENCODER(p, AmrEncoderInit, ());
   if (!p->state)
   {
       closelibrary(p);
@@ -272,13 +264,13 @@
 #endif
 }
 
-#if defined(AMR_GP3) || AMR_OPENCORE_ENABLE_ENCODE
+#if defined(AMR_VO) || AMR_OPENCORE_ENABLE_ENCODE
 
 static sox_bool encode_1_frame(sox_format_t * ft)
 {
   priv_t * p = (priv_t *)ft->priv;
   uint8_t coded[AMR_CODED_MAX];
-  int n = AMR_CALL_ENCODER(p, AmrOpencoreEncoderEncode, AmrGp3EncoderEncode, (p->state, p->mode, p->pcm, coded, 1));
+  int n = AMR_CALL_ENCODER(p, AmrEncoderEncode, (p->state, p->mode, p->pcm, coded, 1));
   sox_bool result = lsx_writebuf(ft, coded, (size_t) (size_t) (unsigned)n) == (unsigned)n;
   if (!result)
     lsx_fail_errno(ft, errno, "write error");
@@ -314,7 +306,7 @@
     if (!encode_1_frame(ft))
       result = SOX_EOF;
   }
-  AMR_CALL_ENCODER(p, AmrOpencoreEncoderExit, AmrGp3EncoderExit, (p->state));
+  AMR_CALL_ENCODER(p, AmrEncoderExit, (p->state));
   return result;
 }
 
@@ -323,7 +315,7 @@
 #define write_samples NULL
 #define stopwrite NULL
 
-#endif /* defined(AMR_GP3) || AMR_OPENCORE_ENABLE_ENCODE */
+#endif /* defined(AMR_VO) || AMR_OPENCORE_ENABLE_ENCODE */
 
 sox_format_handler_t const * AMR_FORMAT_FN(void);
 sox_format_handler_t const * AMR_FORMAT_FN(void)