Merge master.
[dcpomatic.git] / src / lib / util.cc
index c7ef40a1c6e22d6914f3fdbfc7b1eeda1351ab9f..c9685aa44ff3ee811e1f7e77e052b8047ca8ff1d 100644 (file)
@@ -22,7 +22,6 @@
  *  @brief Some utility functions and classes.
  */
 
-#include <sstream>
 #include <iomanip>
 #include <iostream>
 #include <fstream>
@@ -49,7 +48,6 @@
 #include <magick/version.h>
 #include <dcp/version.h>
 #include <dcp/util.h>
-#include <dcp/signer_chain.h>
 #include <dcp/signer.h>
 #include <dcp/raw_convert.h>
 extern "C" {
@@ -73,6 +71,7 @@ extern "C" {
 #include "rect.h"
 #include "md5_digester.h"
 #include "audio_processor.h"
+#include "safe_stringstream.h"
 #ifdef DCPOMATIC_WINDOWS
 #include "stack.hpp"
 #endif
@@ -80,7 +79,6 @@ extern "C" {
 #include "i18n.h"
 
 using std::string;
-using std::stringstream;
 using std::setfill;
 using std::ostream;
 using std::endl;
@@ -124,7 +122,7 @@ seconds_to_hms (int s)
        int h = m / 60;
        m -= (h * 60);
 
-       stringstream hms;
+       SafeStringStream hms;
        hms << h << N_(":");
        hms.width (2);
        hms << std::setfill ('0') << m << N_(":");
@@ -145,7 +143,7 @@ seconds_to_approximate_hms (int s)
        int h = m / 60;
        m -= (h * 60);
 
-       stringstream ap;
+       SafeStringStream ap;
 
        bool const hours = h > 0;
        bool const minutes = h < 10 && m > 0;
@@ -264,7 +262,7 @@ stacktrace (ostream& out, int levels)
 static string
 ffmpeg_version_to_string (int v)
 {
-       stringstream s;
+       SafeStringStream s;
        s << ((v & 0xff0000) >> 16) << N_(".") << ((v & 0xff00) >> 8) << N_(".") << (v & 0xff);
        return s.str ();
 }
@@ -637,6 +635,17 @@ stride_round_up (int c, int const * stride, int t)
        return a - (a % t);
 }
 
+/** @param n A number.
+ *  @param r Rounding `boundary' (must be a power of 2)
+ *  @return n rounded to the nearest r
+ */
+int
+round_to (float n, int r)
+{
+       assert (r == 1 || r == 2 || r == 4);
+       return int (n + float(r) / 2) &~ (r - 1);
+}
+
 /** Read a sequence of key / value pairs from a text stream;
  *  the keys are the first words on the line, and the values are
  *  the remainder of the line following the key.  Lines beginning
@@ -838,13 +847,13 @@ split_get_request (string url)
 }
 
 dcp::Size
-fit_ratio_within (float ratio, dcp::Size full_frame)
+fit_ratio_within (float ratio, dcp::Size full_frame, int round)
 {
        if (ratio < full_frame.ratio ()) {
-               return dcp::Size (rint (full_frame.height * ratio), full_frame.height);
+               return dcp::Size (round_to (full_frame.height * ratio, round), full_frame.height);
        }
        
-       return dcp::Size (full_frame.width, rint (full_frame.width / ratio));
+       return dcp::Size (full_frame.width, round_to (full_frame.width / ratio, round));
 }
 
 void *
@@ -879,7 +888,7 @@ divide_with_round (int64_t a, int64_t b)
 string
 dependency_version_summary ()
 {
-       stringstream s;
+       SafeStringStream s;
        s << N_("libopenjpeg ") << opj_version () << N_(", ")
          << N_("libavcodec ") << ffmpeg_version_to_string (avcodec_version()) << N_(", ")
          << N_("libavfilter ") << ffmpeg_version_to_string (avfilter_version()) << N_(", ")