Merge master.
[dcpomatic.git] / src / lib / util.cc
index 40e9d9c2eed730795a8f8476533ae16290c403c0..074e08cb70e819617391f5c4a6115e830d6444cc 100644 (file)
@@ -37,7 +37,6 @@
 #include <boost/algorithm/string.hpp>
 #include <boost/bind.hpp>
 #include <boost/lambda/lambda.hpp>
-#include <boost/lexical_cast.hpp>
 #include <boost/thread.hpp>
 #include <boost/filesystem.hpp>
 #ifdef DCPOMATIC_WINDOWS
@@ -45,7 +44,6 @@
 #endif
 #include <glib.h>
 #include <openjpeg.h>
-#include <openssl/md5.h>
 #include <pangomm/init.h>
 #include <magick/MagickCore.h>
 #include <magick/version.h>
@@ -53,6 +51,7 @@
 #include <dcp/util.h>
 #include <dcp/signer_chain.h>
 #include <dcp/signer.h>
+#include <dcp/raw_convert.h>
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
@@ -72,6 +71,7 @@ extern "C" {
 #include "cross.h"
 #include "video_content.h"
 #include "rect.h"
+#include "md5_digester.h"
 #ifdef DCPOMATIC_WINDOWS
 #include "stack.hpp"
 #endif
@@ -101,9 +101,9 @@ using std::streampos;
 using std::set_terminate;
 using boost::shared_ptr;
 using boost::thread;
-using boost::lexical_cast;
 using boost::optional;
 using dcp::Size;
+using dcp::raw_convert;
 
 static boost::thread::id ui_thread;
 static boost::filesystem::path backtrace_file;
@@ -250,8 +250,9 @@ LONG WINAPI exception_handler(struct _EXCEPTION_POINTERS *)
 {
        dbg::stack s;
        FILE* f = fopen_boost (backtrace_file, "w");
+       fprintf (f, "Exception thrown:");
        for (dbg::stack::const_iterator i = s.begin(); i != s.end(); ++i) {
-               fprintf (f, "%p %s %d %s", i->instruction, i->function.c_str(), i->line, i->module.c_str());
+               fprintf (f, "%p %s %d %s\n", i->instruction, i->function.c_str(), i->line, i->module.c_str());
        }
        fclose (f);
        return EXCEPTION_CONTINUE_SEARCH;
@@ -409,23 +410,6 @@ split_at_spaces_considering_quotes (string s)
        return out;
 }
 
-string
-md5_digest (void const * data, int size)
-{
-       MD5_CTX md5_context;
-       MD5_Init (&md5_context);
-       MD5_Update (&md5_context, data, size);
-       unsigned char digest[MD5_DIGEST_LENGTH];
-       MD5_Final (digest, &md5_context);
-       
-       stringstream s;
-       for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
-               s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
-       }
-
-       return s.str ();
-}
-
 /** @param job Optional job for which to report progress */
 string
 md5_digest (vector<boost::filesystem::path> files, shared_ptr<Job> job)
@@ -433,8 +417,7 @@ md5_digest (vector<boost::filesystem::path> files, shared_ptr<Job> job)
        boost::uintmax_t const buffer_size = 64 * 1024;
        char buffer[buffer_size];
 
-       MD5_CTX md5_context;
-       MD5_Init (&md5_context);
+       MD5Digester digester;
 
        vector<int64_t> sizes;
        for (size_t i = 0; i < files.size(); ++i) {
@@ -453,7 +436,7 @@ md5_digest (vector<boost::filesystem::path> files, shared_ptr<Job> job)
                while (remaining > 0) {
                        int const t = min (remaining, buffer_size);
                        fread (buffer, 1, t, f);
-                       MD5_Update (&md5_context, buffer, t);
+                       digester.add (buffer, t);
                        remaining -= t;
 
                        if (job) {
@@ -464,15 +447,7 @@ md5_digest (vector<boost::filesystem::path> files, shared_ptr<Job> job)
                fclose (f);
        }
 
-       unsigned char digest[MD5_DIGEST_LENGTH];
-       MD5_Final (digest, &md5_context);
-
-       stringstream s;
-       for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
-               s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
-       }
-
-       return s.str ();
+       return digester.get ();
 }
 
 /** @param An arbitrary audio frame rate.
@@ -687,14 +662,14 @@ int
 get_required_int (multimap<string, string> const & kv, string k)
 {
        string const v = get_required_string (kv, k);
-       return lexical_cast<int> (v);
+       return raw_convert<int> (v);
 }
 
 float
 get_required_float (multimap<string, string> const & kv, string k)
 {
        string const v = get_required_string (kv, k);
-       return lexical_cast<float> (v);
+       return raw_convert<float> (v);
 }
 
 string
@@ -724,7 +699,7 @@ get_optional_int (multimap<string, string> const & kv, string k)
                return 0;
        }
 
-       return lexical_cast<int> (i->second);
+       return raw_convert<int> (i->second);
 }
 
 /** Trip an assert if the caller is not in the UI thread */
@@ -737,7 +712,7 @@ ensure_ui_thread ()
 string
 audio_channel_name (int c)
 {
-       assert (MAX_AUDIO_CHANNELS == 12);
+       assert (MAX_DCP_AUDIO_CHANNELS == 12);
 
        /* TRANSLATORS: these are the names of audio channels; Lfe (sub) is the low-frequency
           enhancement channel (sub-woofer).  HI is the hearing-impaired audio track and
@@ -761,31 +736,12 @@ audio_channel_name (int c)
        return channels[c];
 }
 
-LocaleGuard::LocaleGuard ()
-       : _old (0)
-{
-       char const * old = setlocale (LC_NUMERIC, 0);
-
-       if (old) {
-               _old = strdup (old);
-               if (strcmp (_old, "C")) {
-                       setlocale (LC_NUMERIC, "C");
-               }
-       }
-}
-
-LocaleGuard::~LocaleGuard ()
-{
-       setlocale (LC_NUMERIC, _old);
-       free (_old);
-}
-
 bool
 valid_image_file (boost::filesystem::path f)
 {
        string ext = f.extension().string();
        transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
-       return (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".tga");
+       return (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".tga" || ext == ".dpx");
 }
 
 string
@@ -958,12 +914,16 @@ dependency_version_summary ()
        return s.str ();
 }
 
+/** Construct a ScopedTemporary.  A temporary filename is decided but the file is not opened
+ *  until ::open() is called.
+ */
 ScopedTemporary::ScopedTemporary ()
        : _open (0)
 {
        _file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path ();
 }
 
+/** Close and delete the temporary file */
 ScopedTemporary::~ScopedTemporary ()
 {
        close ();       
@@ -971,12 +931,16 @@ ScopedTemporary::~ScopedTemporary ()
        boost::filesystem::remove (_file, ec);
 }
 
+/** @return temporary filename */
 char const *
 ScopedTemporary::c_str () const
 {
        return _file.string().c_str ();
 }
 
+/** Open the temporary file.
+ *  @return File's FILE pointer.
+ */
 FILE*
 ScopedTemporary::open (char const * params)
 {
@@ -984,6 +948,7 @@ ScopedTemporary::open (char const * params)
        return _open;
 }
 
+/** Close the file */
 void
 ScopedTemporary::close ()
 {
@@ -992,3 +957,16 @@ ScopedTemporary::close ()
                _open = 0;
        }
 }
+
+ContentTimePeriod
+subtitle_period (AVSubtitle const & sub)
+{
+       ContentTime const packet_time = ContentTime::from_seconds (static_cast<double> (sub.pts) / AV_TIME_BASE);
+
+       ContentTimePeriod period (
+               packet_time + ContentTime::from_seconds (sub.start_display_time / 1e3),
+               packet_time + ContentTime::from_seconds (sub.end_display_time / 1e3)
+               );
+
+       return period;
+}