Tidying.
[dcpomatic.git] / src / lib / util.cc
index 65bfd4534b4b9ae51be736e258275e306e5d341c..106a565413a965e838688b7d83af58fe8a6a6335 100644 (file)
  *  @brief Some utility functions and classes.
  */
 
-#include "util.h"
-#include "exceptions.h"
-#include "dcp_content_type.h"
-#include "filter.h"
+
+#define UNICODE 1
+
+
+#include "audio_buffers.h"
+#include "audio_processor.h"
 #include "cinema_sound_processor.h"
+#include "compose.hpp"
 #include "config.h"
-#include "ratio.h"
-#include "job.h"
 #include "cross.h"
-#include "video_content.h"
-#include "rect.h"
-#include "digester.h"
-#include "audio_processor.h"
 #include "crypto.h"
-#include "compose.hpp"
-#include "audio_buffers.h"
-#include "string_text.h"
-#include "font.h"
-#include "render_text.h"
+#include "dcp_content_type.h"
+#include "digester.h"
+#include "exceptions.h"
 #include "ffmpeg_image_proxy.h"
+#include "filter.h"
+#include "font.h"
 #include "image.h"
-#include "text_decoder.h"
+#include "job.h"
 #include "job_manager.h"
+#include "ratio.h"
+#include "rect.h"
+#include "render_text.h"
+#include "string_text.h"
+#include "text_decoder.h"
+#include "util.h"
+#include "video_content.h"
 #include "warnings.h"
 #include <dcp/decrypted_kdm.h>
 #include <dcp/locale_convert.h>
 #include <dcp/sound_asset.h>
 #include <dcp/subtitle_asset.h>
 #include <dcp/atmos_asset.h>
+DCPOMATIC_DISABLE_WARNINGS
 extern "C" {
 #include <libavfilter/avfilter.h>
 #include <libavformat/avformat.h>
 #include <libavcodec/avcodec.h>
 }
+DCPOMATIC_ENABLE_WARNINGS
 #include <curl/curl.h>
 #include <glib.h>
 #include <pangomm/init.h>
@@ -89,35 +95,34 @@ DCPOMATIC_ENABLE_WARNINGS
 
 #include "i18n.h"
 
-using std::string;
-using std::wstring;
-using std::setfill;
-using std::ostream;
+
+using std::bad_alloc;
+using std::cout;
 using std::endl;
-using std::vector;
-using std::min;
-using std::max;
-using std::map;
-using std::list;
-using std::multimap;
 using std::istream;
+using std::list;
+using std::make_pair;
+using std::make_shared;
+using std::map;
+using std::min;
+using std::ostream;
 using std::pair;
-using std::cout;
-using std::bad_alloc;
 using std::set_terminate;
-using std::make_pair;
 using std::shared_ptr;
-using std::make_shared;
-using boost::thread;
-using boost::optional;
-using boost::lexical_cast;
+using std::string;
+using std::vector;
+using std::wstring;
 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;
 
+
 /** Path to our executable, required by the stacktrace stuff and filled
  *  in during App::onInit().
  */
@@ -126,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.
  *
@@ -146,6 +152,7 @@ seconds_to_hms (int s)
        return buffer;
 }
 
+
 string
 time_to_hmsf (DCPTime time, Frame rate)
 {
@@ -162,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")
  */
@@ -220,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 */
@@ -237,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.
@@ -297,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
@@ -331,6 +344,7 @@ terminate ()
        abort();
 }
 
+
 void
 dcpomatic_setup_path_encoding ()
 {
@@ -352,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.
  */
@@ -365,10 +380,12 @@ dcpomatic_setup ()
        SetUnhandledExceptionFilter(exception_handler);
 #endif
 
+#ifdef DCPOMATIC_HAVE_AVREGISTER
 DCPOMATIC_DISABLE_WARNINGS
        av_register_all ();
        avfilter_register_all ();
 DCPOMATIC_ENABLE_WARNINGS
+#endif
 
 #ifdef DCPOMATIC_OSX
        /* Add our library directory to the libltdl search path so that
@@ -399,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);
@@ -417,6 +434,7 @@ DCPOMATIC_ENABLE_WARNINGS
        ui_thread = boost::this_thread::get_id ();
 }
 
+
 #ifdef DCPOMATIC_WINDOWS
 boost::filesystem::path
 mo_path ()
@@ -431,6 +449,7 @@ mo_path ()
 }
 #endif
 
+
 #ifdef DCPOMATIC_OSX
 boost::filesystem::path
 mo_path ()
@@ -439,6 +458,7 @@ mo_path ()
 }
 #endif
 
+
 void
 dcpomatic_setup_gettext_i18n (string lang)
 {
@@ -471,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)
@@ -522,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.
@@ -535,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 ()
@@ -542,6 +572,7 @@ ensure_ui_thread ()
        DCPOMATIC_ASSERT (boost::this_thread::get_id() == ui_thread);
 }
 
+
 string
 audio_channel_name (int c)
 {
@@ -571,6 +602,7 @@ audio_channel_name (int c)
        return channels[c];
 }
 
+
 string
 short_audio_channel_name (int c)
 {
@@ -595,7 +627,7 @@ short_audio_channel_name (int c)
                _("BsR"),
                _("DBP"),
                _("DBS"),
-               "",
+               _("Sign"),
                ""
        };
 
@@ -620,6 +652,7 @@ valid_image_file (boost::filesystem::path f)
                );
 }
 
+
 bool
 valid_sound_file (boost::filesystem::path f)
 {
@@ -632,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)
 {
@@ -640,6 +674,7 @@ valid_j2k_file (boost::filesystem::path f)
        return (ext == ".j2k" || ext == ".j2c" || ext == ".jp2");
 }
 
+
 string
 tidy_for_filename (string f)
 {
@@ -647,6 +682,7 @@ tidy_for_filename (string f)
        return f;
 }
 
+
 dcp::Size
 fit_ratio_within (float ratio, dcp::Size full_frame)
 {
@@ -657,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)
 {
@@ -667,6 +704,7 @@ wrapped_av_malloc (size_t s)
        return p;
 }
 
+
 map<string, string>
 split_get_request (string url)
 {
@@ -713,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)
 {
@@ -726,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)
 {
@@ -796,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.
@@ -838,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)
 {
@@ -862,6 +904,7 @@ remap (shared_ptr<const AudioBuffers> input, int output_channels, AudioMapping m
        return mapped;
 }
 
+
 Eyes
 increment_eyes (Eyes e)
 {
@@ -872,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)
 {
@@ -887,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)
 {
@@ -902,6 +947,7 @@ checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path pat
        }
 }
 
+
 size_t
 utf8_strlen (string s)
 {
@@ -921,6 +967,7 @@ utf8_strlen (string s)
        return N;
 }
 
+
 string
 day_of_week_to_string (boost::gregorian::greg_weekday d)
 {
@@ -944,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(), VideoRange::FULL);
-       auto image = proxy.image().image;
+       FFmpegImageProxy proxy (sub.png_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));
 
@@ -983,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)
 {
@@ -1038,9 +1087,10 @@ show_jobs_on_console (bool progress)
        return error;
 }
 
+
 /** XXX: could use mmap? */
 void
-copy_in_bits (boost::filesystem::path from, boost::filesystem::path to, boost::function<void (float)> progress)
+copy_in_bits (boost::filesystem::path from, boost::filesystem::path to, std::function<void (float)> progress)
 {
        auto f = fopen_boost (from, "rb");
        if (!f) {
@@ -1090,12 +1140,14 @@ copy_in_bits (boost::filesystem::path from, boost::filesystem::path to, boost::f
        free (buffer);
 }
 
+
 double
 db_to_linear (double db)
 {
        return pow(10, db / 20);
 }
 
+
 double
 linear_to_db (double linear)
 {
@@ -1104,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) {
@@ -1115,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;
@@ -1193,3 +1246,4 @@ start_of_thread (string)
 
 }
 #endif
+