Tidying.
[dcpomatic.git] / src / lib / util.cc
index 981cfa521eda35957eec466f8225f510f9c11506..106a565413a965e838688b7d83af58fe8a6a6335 100644 (file)
@@ -112,14 +112,14 @@ using std::shared_ptr;
 using std::string;
 using std::vector;
 using std::wstring;
-using boost::thread;
-using boost::optional;
-using boost::lexical_cast;
 using boost::bad_lexical_cast;
+using boost::lexical_cast;
+using boost::optional;
 using boost::scoped_array;
+using boost::thread;
 using dcp::Size;
-using dcp::raw_convert;
 using dcp::locale_convert;
+using dcp::raw_convert;
 using namespace dcpomatic;
 
 
@@ -131,6 +131,7 @@ bool is_batch_converter = false;
 static boost::thread::id ui_thread;
 static boost::filesystem::path backtrace_file;
 
+
 /** Convert some number of seconds to a string representation
  *  in hours, minutes and seconds.
  *
@@ -151,6 +152,7 @@ seconds_to_hms (int s)
        return buffer;
 }
 
+
 string
 time_to_hmsf (DCPTime time, Frame rate)
 {
@@ -167,6 +169,7 @@ time_to_hmsf (DCPTime time, Frame rate)
        return buffer;
 }
 
+
 /** @param s Number of seconds.
  *  @return String containing an approximate description of s (e.g. "about 2 hours")
  */
@@ -225,12 +228,14 @@ seconds_to_approximate_hms (int s)
        return ap;
 }
 
+
 double
 seconds (struct timeval t)
 {
        return t.tv_sec + (double (t.tv_usec) / 1e6);
 }
 
+
 #ifdef DCPOMATIC_WINDOWS
 
 /** Resolve symbol name and source location given the path to the executable */
@@ -242,6 +247,7 @@ addr2line (void const * const addr)
        return system(addr2line_cmd);
 }
 
+
 DCPOMATIC_DISABLE_WARNINGS
 /** This is called when C signals occur on Windows (e.g. SIGSEGV)
  *  (NOT C++ exceptions!).  We write a backtrace to backtrace_file by dark means.
@@ -302,12 +308,14 @@ exception_handler(struct _EXCEPTION_POINTERS * info)
 DCPOMATIC_ENABLE_WARNINGS
 #endif
 
+
 void
 set_backtrace_file (boost::filesystem::path p)
 {
        backtrace_file = p;
 }
 
+
 /** This is called when there is an unhandled exception.  Any
  *  backtrace in this function is useless on Windows as the stack has
  *  already been unwound from the throw; we have the gdb wrap hack to
@@ -336,6 +344,7 @@ terminate ()
        abort();
 }
 
+
 void
 dcpomatic_setup_path_encoding ()
 {
@@ -357,6 +366,7 @@ dcpomatic_setup_path_encoding ()
 #endif
 }
 
+
 /** Call the required functions to set up DCP-o-matic's static arrays, etc.
  *  Must be called from the UI thread, if there is one.
  */
@@ -406,7 +416,7 @@ DCPOMATIC_ENABLE_WARNINGS
        list<StringText> subs;
        dcp::SubtitleString ss(
                optional<string>(), false, false, false, dcp::Colour(), 42, 1, dcp::Time(), dcp::Time(), 0, dcp::HAlign::CENTER, 0, dcp::VAlign::CENTER, dcp::Direction::LTR,
-               "Hello dolly", dcp::Effect::NONE, dcp::Colour(), dcp::Time(), dcp::Time()
+               "Hello dolly", dcp::Effect::NONE, dcp::Colour(), dcp::Time(), dcp::Time(), 0
                );
        subs.push_back (StringText(ss, 0));
        render_text (subs, list<shared_ptr<Font>>(), dcp::Size(640, 480), DCPTime(), 24);
@@ -424,6 +434,7 @@ DCPOMATIC_ENABLE_WARNINGS
        ui_thread = boost::this_thread::get_id ();
 }
 
+
 #ifdef DCPOMATIC_WINDOWS
 boost::filesystem::path
 mo_path ()
@@ -438,6 +449,7 @@ mo_path ()
 }
 #endif
 
+
 #ifdef DCPOMATIC_OSX
 boost::filesystem::path
 mo_path ()
@@ -446,6 +458,7 @@ mo_path ()
 }
 #endif
 
+
 void
 dcpomatic_setup_gettext_i18n (string lang)
 {
@@ -478,6 +491,7 @@ dcpomatic_setup_gettext_i18n (string lang)
 #endif
 }
 
+
 /** Compute a digest of the first and last `size' bytes of a set of files. */
 string
 digest_head_tail (vector<boost::filesystem::path> files, boost::uintmax_t size)
@@ -529,6 +543,14 @@ digest_head_tail (vector<boost::filesystem::path> files, boost::uintmax_t size)
        return digester.get ();
 }
 
+
+string
+simple_digest (vector<boost::filesystem::path> paths)
+{
+       return digest_head_tail(paths, 1000000) + raw_convert<string>(boost::filesystem::file_size(paths.front()));
+}
+
+
 /** Round a number up to the nearest multiple of another number.
  *  @param c Index.
  *  @param stride Array of numbers to round, indexed by c.
@@ -542,6 +564,7 @@ stride_round_up (int c, int const * stride, int t)
        return a - (a % t);
 }
 
+
 /** Trip an assert if the caller is not in the UI thread */
 void
 ensure_ui_thread ()
@@ -549,6 +572,7 @@ ensure_ui_thread ()
        DCPOMATIC_ASSERT (boost::this_thread::get_id() == ui_thread);
 }
 
+
 string
 audio_channel_name (int c)
 {
@@ -578,6 +602,7 @@ audio_channel_name (int c)
        return channels[c];
 }
 
+
 string
 short_audio_channel_name (int c)
 {
@@ -627,6 +652,7 @@ valid_image_file (boost::filesystem::path f)
                );
 }
 
+
 bool
 valid_sound_file (boost::filesystem::path f)
 {
@@ -639,6 +665,7 @@ valid_sound_file (boost::filesystem::path f)
        return (ext == ".wav" || ext == ".mp3" || ext == ".aif" || ext == ".aiff");
 }
 
+
 bool
 valid_j2k_file (boost::filesystem::path f)
 {
@@ -647,6 +674,7 @@ valid_j2k_file (boost::filesystem::path f)
        return (ext == ".j2k" || ext == ".j2c" || ext == ".jp2");
 }
 
+
 string
 tidy_for_filename (string f)
 {
@@ -654,6 +682,7 @@ tidy_for_filename (string f)
        return f;
 }
 
+
 dcp::Size
 fit_ratio_within (float ratio, dcp::Size full_frame)
 {
@@ -664,6 +693,7 @@ fit_ratio_within (float ratio, dcp::Size full_frame)
        return dcp::Size (full_frame.width, lrintf (full_frame.width / ratio));
 }
 
+
 void *
 wrapped_av_malloc (size_t s)
 {
@@ -674,6 +704,7 @@ wrapped_av_malloc (size_t s)
        return p;
 }
 
+
 map<string, string>
 split_get_request (string url)
 {
@@ -720,6 +751,7 @@ split_get_request (string url)
        return r;
 }
 
+
 string
 video_asset_filename (shared_ptr<dcp::PictureAsset> asset, int reel_index, int reel_count, optional<string> summary)
 {
@@ -733,6 +765,7 @@ video_asset_filename (shared_ptr<dcp::PictureAsset> asset, int reel_index, int r
        return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + ".mxf");
 }
 
+
 string
 audio_asset_filename (shared_ptr<dcp::SoundAsset> asset, int reel_index, int reel_count, optional<string> summary)
 {
@@ -803,6 +836,7 @@ careful_string_filter (string s)
        return boost::locale::conv::utf_to_utf<char>(out);
 }
 
+
 /** @param mapped List of mapped audio channels from a Film.
  *  @param channels Total number of channels in the Film.
  *  @return First: number of non-LFE soundtrack channels (L/R/C/Ls/Rs/Lc/Rc/Bsl/Bsr), second: number of LFE channels.
@@ -845,6 +879,7 @@ audio_channel_types (list<int> mapped, int channels)
        return make_pair (non_lfe, lfe);
 }
 
+
 shared_ptr<AudioBuffers>
 remap (shared_ptr<const AudioBuffers> input, int output_channels, AudioMapping map)
 {
@@ -869,6 +904,7 @@ remap (shared_ptr<const AudioBuffers> input, int output_channels, AudioMapping m
        return mapped;
 }
 
+
 Eyes
 increment_eyes (Eyes e)
 {
@@ -879,6 +915,7 @@ increment_eyes (Eyes e)
        return Eyes::LEFT;
 }
 
+
 void
 checked_fwrite (void const * ptr, size_t size, FILE* stream, boost::filesystem::path path)
 {
@@ -894,6 +931,7 @@ checked_fwrite (void const * ptr, size_t size, FILE* stream, boost::filesystem::
        }
 }
 
+
 void
 checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path path)
 {
@@ -909,6 +947,7 @@ checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path pat
        }
 }
 
+
 size_t
 utf8_strlen (string s)
 {
@@ -928,6 +967,7 @@ utf8_strlen (string s)
        return N;
 }
 
+
 string
 day_of_week_to_string (boost::gregorian::greg_weekday d)
 {
@@ -951,13 +991,14 @@ day_of_week_to_string (boost::gregorian::greg_weekday d)
        return d.as_long_string ();
 }
 
+
 /** @param size Size of picture that the subtitle will be overlaid onto */
 void
 emit_subtitle_image (ContentTimePeriod period, dcp::SubtitleImage sub, dcp::Size size, shared_ptr<TextDecoder> decoder)
 {
        /* XXX: this is rather inefficient; decoding the image just to get its size */
        FFmpegImageProxy proxy (sub.png_image());
-       auto image = proxy.image(Image::Alignment::COMPACT).image;
+       auto image = proxy.image(Image::Alignment::PADDED).image;
        /* set up rect with height and width */
        dcpomatic::Rect<double> rect(0, 0, image->size().width / double(size.width), image->size().height / double(size.height));
 
@@ -990,6 +1031,7 @@ emit_subtitle_image (ContentTimePeriod period, dcp::SubtitleImage sub, dcp::Size
        decoder->emit_bitmap (period, image, rect);
 }
 
+
 bool
 show_jobs_on_console (bool progress)
 {
@@ -1045,6 +1087,7 @@ show_jobs_on_console (bool progress)
        return error;
 }
 
+
 /** XXX: could use mmap? */
 void
 copy_in_bits (boost::filesystem::path from, boost::filesystem::path to, std::function<void (float)> progress)
@@ -1097,12 +1140,14 @@ copy_in_bits (boost::filesystem::path from, boost::filesystem::path to, std::fun
        free (buffer);
 }
 
+
 double
 db_to_linear (double db)
 {
        return pow(10, db / 20);
 }
 
+
 double
 linear_to_db (double linear)
 {
@@ -1111,7 +1156,7 @@ linear_to_db (double linear)
 
 
 dcp::Size
-scale_for_display (dcp::Size s, dcp::Size display_container, dcp::Size film_container)
+scale_for_display (dcp::Size s, dcp::Size display_container, dcp::Size film_container, PixelQuanta quanta)
 {
        /* Now scale it down if the display container is smaller than the film container */
        if (display_container != film_container) {
@@ -1122,6 +1167,7 @@ scale_for_display (dcp::Size s, dcp::Size display_container, dcp::Size film_cont
 
                s.width = lrintf (s.width * scale);
                s.height = lrintf (s.height * scale);
+               s = quanta.round (s);
        }
 
        return s;