Prepare support for compression levels (archive + flac)
authorRobin Gareus <robin@gareus.org>
Mon, 2 Oct 2017 18:40:44 +0000 (20:40 +0200)
committerRobin Gareus <robin@gareus.org>
Tue, 3 Oct 2017 00:22:42 +0000 (02:22 +0200)
libs/ardour/sndfilesource.cc
libs/pbd/file_archive.cc
libs/pbd/pbd/file_archive.h

index 9c49e1e1fb0dcc6caf94b0fea59279c8667b2ac2..c69325741ed96529e8222bd62a9017c83ff5412f 100644 (file)
@@ -276,6 +276,21 @@ SndFileSource::SndFileSource (Session& s, const AudioFileSource& other, const st
                throw failed_constructor();
        }
 
+#if 0
+       /* setting flac compression quality above the default does not produce a significant size
+        * improvement (not for large raw recordings anyway, the_CLA tests 2017-10-02, >> 250MB files,
+        * ~1% smaller), but does have a significant encoding speed penalty.
+        *
+        * We still may expose this as option someday though, perhaps for opposite reason: "fast encoding"
+        */
+       double flac_quality = 1; // libsndfile uses range 0..1 (mapped to flac 0..8), default is (5/8)
+       if (sf_command (_sndfile, SFC_SET_COMPRESSION_LEVEL, &flac_quality, sizeof (double)) != SF_TRUE) {
+               char errbuf[256];
+               sf_error_str (_sndfile, errbuf, sizeof (errbuf) - 1);
+               error << string_compose (_("Cannot set flac compression level: %1"), errbuf) << endmsg;
+       }
+#endif
+
        Sample buf[8192];
        samplecnt_t off = 0;
        float peak = 0;
index 95cdbf61cf4e98f66c78bdc6514d609a5673b7c6..ccc19c05417fe0a2b972a507f7155b9a852295cc 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 
+#ifndef NDEBUG
+#include <iostream>
+#include <iomanip>
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 #include <cstdio>
@@ -337,7 +342,7 @@ FileArchive::do_extract (struct archive* a)
 
 
 int
-FileArchive::create (const std::string& srcdir)
+FileArchive::create (const std::string& srcdir, CompressionLevel compression_level)
 {
        if (_req.is_remote ()) {
                return -1;
@@ -357,11 +362,11 @@ FileArchive::create (const std::string& srcdir)
                filemap[*f] = f->substr (p_len);
        }
 
-       return create (filemap);
+       return create (filemap, compression_level);
 }
 
 int
-FileArchive::create (const std::map<std::string, std::string>& filemap)
+FileArchive::create (const std::map<std::string, std::string>& filemap, CompressionLevel compression_level)
 {
        struct archive *a;
        struct archive_entry *entry;
@@ -385,10 +390,21 @@ FileArchive::create (const std::map<std::string, std::string>& filemap)
 
        a = archive_write_new ();
        archive_write_set_format_pax_restricted (a);
-       archive_write_add_filter_lzma (a);
+
+       if (compression_level != CompressNone) {
+               archive_write_add_filter_lzma (a);
+               char buf[48];
+               sprintf (buf, "lzma:compression-level=%u,lzma:threads=0", (uint32_t) compression_level);
+               archive_write_set_options (a, buf);
+       }
+
        archive_write_open_filename (a, _req.url);
        entry = archive_entry_new ();
 
+#ifndef NDEBUG
+         const int64_t archive_start_time = g_get_monotonic_time();
+#endif
+
        for (std::map<std::string, std::string>::const_iterator f = filemap.begin (); f != filemap.end (); ++f) {
                char buf[8192];
                const char* filepath = f->first.c_str ();
@@ -433,5 +449,10 @@ FileArchive::create (const std::map<std::string, std::string>& filemap)
        archive_write_close (a);
        archive_write_free (a);
 
+#ifndef NDEBUG
+       const int64_t elapsed_time_us = g_get_monotonic_time() - archive_start_time;
+       std::cerr << "archived in " << std::fixed << std::setprecision (2) << elapsed_time_us / 1000000. << " sec\n";
+#endif
+
        return 0;
 }
index 3708f3bbf4d34e8ff89cd7ff9bd078d3633aed7b..3ef1d93ee0c3c044ef5733fa68d6f467d0d01a61 100644 (file)
@@ -38,8 +38,17 @@ class LIBPBD_API FileArchive
                int inflate (const std::string& destdir);
                std::vector<std::string> contents ();
 
-               int create (const std::string& srcdir);
-               int create (const std::map <std::string, std::string>& filemap);
+               /* these are mapped to libarchive's lzmaz
+                * compression level 0..9
+                */
+               enum CompressionLevel {
+                       CompressNone = -1,
+                       CompressFast = 0,
+                       CompressGood = 6
+               };
+
+               int create (const std::string& srcdir, CompressionLevel compression_level = CompressGood);
+               int create (const std::map <std::string, std::string>& filemap, CompressionLevel compression_level = CompressGood);
 
                PBD::Signal2<void, size_t, size_t> progress; // TODO