shithub: sox

Download patch

ref: 8eaf550341043082a2bc6a6e45438bf963c0a93b
parent: e2f6fd3c004cf2f3e75d91828acc6ff479108d83
author: robs <robs>
date: Sat Feb 7 08:07:11 EST 2009

build internal formats statically; dynamically load other formats only if needed

--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -46,117 +46,9 @@
 if HAVE_LIBLTDL
 
 # Format modules; listed first so optional ones can be added later
-pkglib_LTLIBRARIES = libsox_fmt_raw.la libsox_fmt_s1.la \
-	  libsox_fmt_s2.la libsox_fmt_s3.la libsox_fmt_s4.la libsox_fmt_u1.la \
-	  libsox_fmt_u2.la libsox_fmt_u3.la libsox_fmt_u4.la		\
-	  libsox_fmt_al.la libsox_fmt_la.la libsox_fmt_ul.la		\
-	  libsox_fmt_lu.la libsox_fmt_svx.la libsox_fmt_aiff.la		\
-	  libsox_fmt_aifc.la libsox_fmt_au.la libsox_fmt_avr.la		\
-	  libsox_fmt_cdr.la libsox_fmt_cvsd.la libsox_fmt_dvms.la	\
-	  libsox_fmt_dat.la libsox_fmt_gsm.la libsox_fmt_hcom.la	\
-	  libsox_fmt_lpc10.la libsox_fmt_maud.la libsox_fmt_prc.la	\
-	  libsox_fmt_sf.la libsox_fmt_smp.la	\
-	  libsox_fmt_sphere.la libsox_fmt_txw.la libsox_fmt_voc.la	\
-	  libsox_fmt_vox.la libsox_fmt_ima.la libsox_fmt_wav.la		\
-	  libsox_fmt_wve.la libsox_fmt_xa.la libsox_fmt_nul.la \
-	  libsox_fmt_htk.la libsox_fmt_sounder.la libsox_fmt_soundtool.la \
-	  libsox_fmt_f4.la libsox_fmt_f8.la
+pkglib_LTLIBRARIES =
 
 # File formats
-libsox_fmt_raw_la_SOURCES = raw-fmt.c
-libsox_fmt_raw_la_LIBADD = libsox.la
-libsox_fmt_f4_la_SOURCES = f4-fmt.c
-libsox_fmt_f4_la_LIBADD = libsox.la
-libsox_fmt_f8_la_SOURCES = f8-fmt.c
-libsox_fmt_f8_la_LIBADD = libsox.la
-libsox_fmt_s1_la_SOURCES = s1-fmt.c
-libsox_fmt_s1_la_LIBADD = libsox.la
-libsox_fmt_s2_la_SOURCES = s2-fmt.c
-libsox_fmt_s2_la_LIBADD = libsox.la
-libsox_fmt_s3_la_SOURCES = s3-fmt.c
-libsox_fmt_s3_la_LIBADD = libsox.la
-libsox_fmt_s4_la_SOURCES = s4-fmt.c
-libsox_fmt_s4_la_LIBADD = libsox.la
-libsox_fmt_u1_la_SOURCES = u1-fmt.c
-libsox_fmt_u1_la_LIBADD = libsox.la
-libsox_fmt_u2_la_SOURCES = u2-fmt.c
-libsox_fmt_u2_la_LIBADD = libsox.la
-libsox_fmt_u3_la_SOURCES = u3-fmt.c
-libsox_fmt_u3_la_LIBADD = libsox.la
-libsox_fmt_u4_la_SOURCES = u4-fmt.c
-libsox_fmt_u4_la_LIBADD = libsox.la
-libsox_fmt_al_la_SOURCES = al-fmt.c
-libsox_fmt_al_la_LIBADD = libsox.la
-libsox_fmt_la_la_SOURCES = la-fmt.c
-libsox_fmt_la_la_LIBADD = libsox.la
-libsox_fmt_ul_la_SOURCES = ul-fmt.c
-libsox_fmt_ul_la_LIBADD = libsox.la
-libsox_fmt_lu_la_SOURCES = lu-fmt.c
-libsox_fmt_lu_la_LIBADD = libsox.la
-libsox_fmt_svx_la_SOURCES = 8svx.c
-libsox_fmt_svx_la_LIBADD = libsox.la
-libsox_fmt_aiff_la_SOURCES = aiff-fmt.c
-libsox_fmt_aiff_la_LIBADD = libsox.la
-libsox_fmt_aifc_la_SOURCES = aifc-fmt.c
-libsox_fmt_aifc_la_LIBADD = libsox.la
-libsox_fmt_au_la_SOURCES = au.c
-libsox_fmt_au_la_LIBADD = libsox.la
-libsox_fmt_avr_la_SOURCES = avr.c
-libsox_fmt_avr_la_LIBADD = libsox.la
-libsox_fmt_cdr_la_SOURCES = cdr.c
-libsox_fmt_cdr_la_LIBADD = libsox.la
-if HAVE_COREAUDIO
-libsox_fmt_coreaudio_la_SOURCES = coreaudio.c
-libsox_fmt_coreaudio_la_LIBADD = libsox.la @COREAUDIO_LIBS@
-pkglib_LTLIBRARIES += libsox_fmt_coreaudio.la
-endif
-libsox_fmt_cvsd_la_SOURCES = cvsd-fmt.c
-libsox_fmt_cvsd_la_LIBADD = libsox.la
-libsox_fmt_dvms_la_SOURCES = dvms-fmt.c
-libsox_fmt_dvms_la_LIBADD = libsox.la
-libsox_fmt_dat_la_SOURCES = dat.c
-libsox_fmt_dat_la_LIBADD = libsox.la
-libsox_fmt_gsm_la_SOURCES = gsm.c
-libsox_fmt_gsm_la_LIBADD = libsox.la @GSM_LIBS@ @LIBGSM_LIBADD@
-libsox_fmt_hcom_la_SOURCES = hcom.c
-libsox_fmt_hcom_la_LIBADD = libsox.la
-libsox_fmt_htk_la_SOURCES = htk.c
-libsox_fmt_htk_la_LIBADD = libsox.la
-libsox_fmt_lpc10_la_SOURCES = lpc10.c
-libsox_fmt_lpc10_la_LIBADD = libsox.la @LPC10_LIBS@ @LIBLPC10_LIBADD@
-libsox_fmt_maud_la_SOURCES = maud.c
-libsox_fmt_maud_la_LIBADD = libsox.la
-libsox_fmt_prc_la_SOURCES = prc.c
-libsox_fmt_prc_la_LIBADD = libsox.la
-if HAVE_PULSEAUDIO
-libsox_fmt_pulseaudio_la_SOURCES = pulseaudio.c
-libsox_fmt_pulseaudio_la_LIBADD = libsox.la @PULSEAUDIO_LIBS@
-pkglib_LTLIBRARIES += libsox_fmt_pulseaudio.la
-endif
-libsox_fmt_sf_la_SOURCES = sf.c
-libsox_fmt_sf_la_LIBADD = libsox.la
-libsox_fmt_smp_la_SOURCES = smp.c
-libsox_fmt_smp_la_LIBADD = libsox.la
-libsox_fmt_sounder_la_SOURCES = sounder.c
-libsox_fmt_sounder_la_LIBADD = libsox.la
-libsox_fmt_soundtool_la_SOURCES = soundtool.c
-libsox_fmt_soundtool_la_LIBADD = libsox.la
-libsox_fmt_sphere_la_SOURCES = sphere.c
-libsox_fmt_sphere_la_LIBADD = libsox.la
-libsox_fmt_txw_la_SOURCES = tx16w.c
-libsox_fmt_txw_la_LIBADD = libsox.la
-libsox_fmt_voc_la_SOURCES = voc.c
-libsox_fmt_voc_la_LIBADD = libsox.la
-libsox_fmt_vox_la_SOURCES = vox-fmt.c
-libsox_fmt_vox_la_LIBADD = libsox.la
-libsox_fmt_ima_la_SOURCES = ima-fmt.c
-libsox_fmt_ima_la_LIBADD = libsox.la
-libsox_fmt_wav_la_SOURCES = adpcm.c adpcm.h ima_rw.c ima_rw.h wav.c
-libsox_fmt_wav_la_LIBADD = libsox.la @GSM_LIBS@ @LIBGSM_LIBADD@
-libsox_fmt_wve_la_SOURCES = wve.c
-libsox_fmt_wve_la_LIBADD = libsox.la
-libsox_fmt_xa_la_SOURCES = xa.c
-libsox_fmt_xa_la_LIBADD = libsox.la
 if HAVE_FLAC
 libsox_fmt_flac_la_SOURCES = flac.c
 libsox_fmt_flac_la_LIBADD = libsox.la @FLAC_LIBS@
@@ -227,13 +119,16 @@
 endif
 
 # I/O devices
-libsox_fmt_nul_la_SOURCES = nulfile.c
-libsox_fmt_nul_la_LIBADD = libsox.la
 if HAVE_ALSA
 libsox_fmt_alsa_la_SOURCES = alsa.c
 libsox_fmt_alsa_la_LIBADD = libsox.la @ALSA_LIBS@
 pkglib_LTLIBRARIES += libsox_fmt_alsa.la
 endif
+if HAVE_COREAUDIO
+libsox_fmt_coreaudio_la_SOURCES = coreaudio.c
+libsox_fmt_coreaudio_la_LIBADD = libsox.la @COREAUDIO_LIBS@
+pkglib_LTLIBRARIES += libsox_fmt_coreaudio.la
+endif
 if HAVE_LIBAO
 libsox_fmt_ao_la_SOURCES = ao.c
 libsox_fmt_ao_la_LIBADD = libsox.la @AO_LIBS@
@@ -244,6 +139,11 @@
 libsox_fmt_oss_la_LIBADD = libsox.la @OSS_LIBS@
 pkglib_LTLIBRARIES += libsox_fmt_oss.la
 endif
+if HAVE_PULSEAUDIO
+libsox_fmt_pulseaudio_la_SOURCES = pulseaudio.c
+libsox_fmt_pulseaudio_la_LIBADD = libsox.la @PULSEAUDIO_LIBS@
+pkglib_LTLIBRARIES += libsox_fmt_pulseaudio.la
+endif
 if HAVE_SNDIO
 libsox_fmt_sndio_la_SOURCES = sndio.c
 libsox_fmt_sndio_la_LIBADD = libsox.la @SNDIO_LIBS@
@@ -297,19 +197,19 @@
 libsox_la_CFLAGS = @WARN_CFLAGS@
 libsox_la_LDFLAGS = @APP_LDFLAGS@ -version-info @SHLIB_VERSION@
 
+libsox_la_SOURCES += raw-fmt.c s1-fmt.c s2-fmt.c s3-fmt.c \
+  s4-fmt.c u1-fmt.c u2-fmt.c u3-fmt.c u4-fmt.c al-fmt.c la-fmt.c ul-fmt.c \
+  lu-fmt.c 8svx.c aiff-fmt.c aifc-fmt.c au.c avr.c cdr.c cvsd-fmt.c \
+  dvms-fmt.c dat.c gsm.c hcom.c htk.c lpc10.c maud.c prc.c sf.c smp.c \
+  sounder.c soundtool.c sphere.c tx16w.c voc.c vox-fmt.c ima-fmt.c adpcm.c adpcm.h \
+  ima_rw.c ima_rw.h wav.c wve.c xa.c nulfile.c f4-fmt.c f8-fmt.c
+libsox_la_LIBADD += @GSM_LIBS@ @LIBGSM_LIBADD@
+libsox_la_LIBADD += @LPC10_LIBS@ @LIBLPC10_LIBADD@
+
 if HAVE_LIBLTDL
   libsox_la_CFLAGS += $(LTDLINCL) 
   libsox_la_LDFLAGS += $(LIBLTDL) 
 else
-  libsox_la_SOURCES += raw-fmt.c s1-fmt.c s2-fmt.c s3-fmt.c \
-    s4-fmt.c u1-fmt.c u2-fmt.c u3-fmt.c u4-fmt.c al-fmt.c la-fmt.c ul-fmt.c \
-    lu-fmt.c 8svx.c aiff-fmt.c aifc-fmt.c au.c avr.c cdr.c cvsd-fmt.c \
-    dvms-fmt.c dat.c gsm.c hcom.c htk.c lpc10.c maud.c prc.c sf.c smp.c \
-    sounder.c soundtool.c sphere.c tx16w.c voc.c vox-fmt.c ima-fmt.c adpcm.c adpcm.h \
-    ima_rw.c ima_rw.h wav.c wve.c xa.c nulfile.c f4-fmt.c f8-fmt.c
-libsox_la_LIBADD += @GSM_LIBS@ @LIBGSM_LIBADD@
-libsox_la_LIBADD += @LPC10_LIBS@ @LIBLPC10_LIBADD@
-
 sox_LDADD += @GSM_LIBS@ @LIBGSM_LIBADD@
 sox_LDADD += @LPC10_LIBS@ @LIBLPC10_LIBADD@
 
--- a/src/example2.c
+++ b/src/example2.c
@@ -43,7 +43,7 @@
   uint64_t seek;
 
   /* All libSoX applications must start by initialising the SoX library */
-  assert(sox_format_init() == SOX_SUCCESS);
+  assert(sox_init() == SOX_SUCCESS);
 
   assert(argc > 1);
   ++argv, --argc; /* Move to 1st parameter */
--- a/src/formats.c
+++ b/src/formats.c
@@ -1004,94 +1004,87 @@
 
 /*----------------------------- Formats library ------------------------------*/
 
+enum {
+  #define FORMAT(f) f,
+  #include "formats.h"
+  #undef FORMAT
+  NSTATIC_FORMATS
+};
+
+static sox_bool plugins_initted = sox_false;
+
 #ifdef HAVE_LIBLTDL /* Plugin format handlers */
-#define sox_format_fns sox_format_fns0
+  #include <ltdl.h>
+  #define MAX_FORMATS (NSTATIC_FORMATS + 40)
+  #define MAX_FORMATS_1 (MAX_FORMATS + 1)
+  #define MAX_NAME_LEN (size_t)1024 /* FIXME: Use vasprintf */
+
+  static unsigned nformats = NSTATIC_FORMATS;
+
+  static int init_format(const char *file, lt_ptr data)
+  {
+    lt_dlhandle lth = lt_dlopenext(file);
+    const char *end = file + strlen(file);
+    const char prefix[] = "sox_fmt_";
+    char fnname[MAX_NAME_LEN];
+    char *start = strstr(file, prefix);
+
+    (void)data;
+    if (start && (start += sizeof(prefix) - 1) < end) {
+      int ret = snprintf(fnname, MAX_NAME_LEN, "lsx_%.*s_format_fn", (int)(end - start), start);
+      if (ret > 0 && ret < (int)MAX_NAME_LEN) {
+        union {sox_format_fn_t fn; lt_ptr ptr;} ltptr;
+        ltptr.ptr = lt_dlsym(lth, fnname);
+        lsx_debug("opening format plugin `%s': library %p, entry point %p\n", fnname, (void *)lth, ltptr.ptr);
+        if (nformats < MAX_FORMATS && ltptr.fn && (ltptr.fn()->sox_lib_version_code & ~255) == (SOX_LIB_VERSION_CODE & ~255))
+          sox_format_fns[nformats++].fn = ltptr.fn;
+      }
+    }
+    return SOX_SUCCESS;
+  }
+#else
+  #define MAX_FORMATS_1
 #endif
 
-#ifndef HAVE_LIBLTDL
 #define FORMAT(f) extern sox_format_handler_t const * lsx_##f##_format_fn(void);
 #include "formats.h"
 #undef FORMAT
 
-sox_format_tab_t sox_format_fns[] = {
+sox_format_tab_t sox_format_fns[MAX_FORMATS_1] = {
   #define FORMAT(f) {NULL, lsx_##f##_format_fn},
   #include "formats.h"
   #undef FORMAT
   {NULL, NULL}
 };
-#endif
 
-#ifdef HAVE_LIBLTDL
-
-#include <ltdl.h>
-#undef sox_format_fns
-#define MAX_FORMATS 256 /* FIXME: Use a vector, not a fixed-size array */
-#define MAX_NAME_LEN (size_t)1024 /* FIXME: Use vasprintf */
-
-static sox_bool plugins_initted = sox_false;
-
-sox_format_tab_t sox_format_fns[MAX_FORMATS + 1];
-
-static unsigned nformats = 0;
-
-static int init_format(const char *file, lt_ptr data)
+int sox_format_init(void) /* Find & load format handlers.  */
 {
-  lt_dlhandle lth = lt_dlopenext(file);
-  const char *end = file + strlen(file);
-  const char prefix[] = "sox_fmt_";
-  char fnname[MAX_NAME_LEN];
-  char *start = strstr(file, prefix);
+  if (plugins_initted)
+    return SOX_EOF;
 
-  (void)data;
-  if (start && (start += sizeof(prefix) - 1) < end) {
-    int ret = snprintf(fnname, MAX_NAME_LEN, "lsx_%.*s_format_fn", (int)(end - start), start);
-    if (ret > 0 && ret < (int)MAX_NAME_LEN) {
-      union {sox_format_fn_t fn; lt_ptr ptr;} ltptr;
-      ltptr.ptr = lt_dlsym(lth, fnname);
-      lsx_debug("opening format plugin `%s': library %p, entry point %p\n", fnname, (void *)lth, ltptr.ptr);
-      if (nformats < MAX_FORMATS && ltptr.fn && (ltptr.fn()->sox_lib_version_code & ~255) == (SOX_LIB_VERSION_CODE & ~255))
-        sox_format_fns[nformats++].fn = ltptr.fn;
+  plugins_initted = sox_true;
+#ifdef HAVE_LIBLTDL
+  {
+    int error = lt_dlinit();
+    if (error) {
+      lsx_fail("lt_dlinit failed with %d error(s): %s", error, lt_dlerror());
+      return SOX_EOF;
     }
+    lt_dlforeachfile(PKGLIBDIR, init_format, NULL);
   }
+#endif
   return SOX_SUCCESS;
 }
 
-int sox_format_init(void) /* Find & load format handlers.  */
-{
-  sox_format_handler_t const * lsx_sox_format_fn(void);
-  int ret;
-
-#if 0
-  nformats = array_length(sox_format_fns0) - 1;
-  memcpy(sox_format_fns, sox_format_fns0, nformats * sizeof(sox_format_fns[0]));
-#else
-  sox_format_fns[nformats++].fn = lsx_sox_format_fn;
-#endif 
-
-  if ((ret = lt_dlinit()) != 0) {
-    lsx_fail("lt_dlinit failed with %d error(s): %s", ret, lt_dlerror());
-    return SOX_EOF;
-  }
-  plugins_initted = sox_true;
-
-  lt_dlforeachfile(PKGLIBDIR, init_format, NULL);
-  return SOX_SUCCESS;
-}
-
 void sox_format_quit(void) /* Cleanup things.  */
 {
+#ifdef HAVE_LIBLTDL
   int ret;
   if (plugins_initted && (ret = lt_dlexit()) != 0)
     lsx_fail("lt_dlexit failed with %d error(s): %s", ret, lt_dlerror());
+#endif
 }
 
-#else /* Static format handlers */
-
-int sox_format_init(void) {return SOX_SUCCESS;}
-void sox_format_quit(void) {}
-
-#endif
-
 /* Find a named format in the formats library.
  *
  * (c) 2005-8 Chris Bagwell and SoX contributors.
@@ -1115,5 +1108,7 @@
         if (!strcasecmp(handler->names[n], name))
           return handler;                 /* Found it. */
   }
+  if (sox_format_init() == SOX_SUCCESS)
+    return sox_find_format(name, no_dev);
   return NULL;
 }
--- a/src/libsox.c
+++ b/src/libsox.c
@@ -110,7 +110,6 @@
 
 int sox_init(void)
 {
-  sox_format_init();
   return lsx_effects_init();
 }
 
--- a/src/sox.c
+++ b/src/sox.c
@@ -1664,6 +1664,7 @@
   char const * * format_list;
   char const * const * names;
 
+  sox_format_init();
   for (i = formats = 0; sox_format_fns[i].fn; ++i) {
     char const * const *names = sox_format_fns[i].fn()->names;
     while (*names++)