X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Futil.cc;h=ec8b0f7a52571c18f93c99f48ead29bba668d196;hb=80fafad9c11e0cd8cf9d6ce17deb83be6f680f2d;hp=290dd20ef1f7d186f7844caa9222c175058a14e4;hpb=0b6c6de07f9a3aa28c2e8ca8ef30340e3fa1bfc6;p=dcpomatic.git diff --git a/src/lib/util.cc b/src/lib/util.cc index 290dd20ef..ec8b0f7a5 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -43,13 +43,18 @@ #endif #include #include +#include +#ifdef DCPOMATIC_IMAGE_MAGICK #include +#else +#include +#include +#endif #include -#include -#include -#include -#include -#include +#include +#include +#include +#include extern "C" { #include #include @@ -62,13 +67,15 @@ extern "C" { #include "scaler.h" #include "dcp_content_type.h" #include "filter.h" -#include "sound_processor.h" +#include "cinema_sound_processor.h" #include "config.h" #include "ratio.h" #include "job.h" #include "cross.h" #include "video_content.h" +#include "rect.h" #include "md5_digester.h" +#include "audio_processor.h" #include "safe_stringstream.h" #ifdef DCPOMATIC_WINDOWS #include "stack.hpp" @@ -99,8 +106,8 @@ using std::set_terminate; using boost::shared_ptr; using boost::thread; using boost::optional; -using libdcp::Size; -using libdcp::raw_convert; +using dcp::Size; +using dcp::raw_convert; static boost::thread::id ui_thread; static boost::filesystem::path backtrace_file; @@ -149,14 +156,11 @@ seconds_to_approximate_hms (int s) if (hours) { if (m > 30 && !minutes) { - ap << (h + 1) << N_(" ") << _("hours"); + /* TRANSLATORS: h here is an abbreviation for hours */ + ap << (h + 1) << _("h"); } else { - ap << h << N_(" "); - if (h == 1) { - ap << _("hour"); - } else { - ap << _("hours"); - } + /* TRANSLATORS: h here is an abbreviation for hours */ + ap << h << _("h"); } if (minutes | seconds) { @@ -167,14 +171,11 @@ seconds_to_approximate_hms (int s) if (minutes) { /* Minutes */ if (s > 30 && !seconds) { - ap << (m + 1) << N_(" ") << _("minutes"); + /* TRANSLATORS: m here is an abbreviation for minutes */ + ap << (m + 1) << _("m"); } else { - ap << m << N_(" "); - if (m == 1) { - ap << _("minute"); - } else { - ap << _("minutes"); - } + /* TRANSLATORS: m here is an abbreviation for minutes */ + ap << m << _("m"); } if (seconds) { @@ -184,12 +185,8 @@ seconds_to_approximate_hms (int s) if (seconds) { /* Seconds */ - ap << s << N_(" "); - if (s == 1) { - ap << _("second"); - } else { - ap << _("seconds"); - } + /* TRANSLATORS: s here is an abbreviation for seconds */ + ap << s << _("s"); } return ap.str (); @@ -265,24 +262,6 @@ ffmpeg_version_to_string (int v) return s.str (); } -/** Return a user-readable string summarising the versions of our dependencies */ -string -dependency_version_summary () -{ - 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_(", ") - << N_("libavformat ") << ffmpeg_version_to_string (avformat_version()) << N_(", ") - << N_("libavutil ") << ffmpeg_version_to_string (avutil_version()) << N_(", ") - << N_("libswscale ") << ffmpeg_version_to_string (swscale_version()) << N_(", ") - << MagickVersion << N_(", ") - << N_("libssh ") << ssh_version (0) << N_(", ") - << N_("libdcp ") << libdcp::version << N_(" git ") << libdcp::git_commit; - - return s.str (); -} - double seconds (struct timeval t) { @@ -303,6 +282,12 @@ LONG WINAPI exception_handler(struct _EXCEPTION_POINTERS *) } #endif +void +set_backtrace_file (boost::filesystem::path p) +{ + backtrace_file = p; +} + /* From http://stackoverflow.com/questions/2443135/how-do-i-find-where-an-exception-was-thrown-in-c */ void terminate () @@ -338,8 +323,9 @@ void dcpomatic_setup () { #ifdef DCPOMATIC_WINDOWS - backtrace_file /= g_get_user_config_dir (); - backtrace_file /= "backtrace.txt"; + boost::filesystem::path p = g_get_user_config_dir (); + p /= "backtrace.txt"; + set_backtrace_file (p); SetUnhandledExceptionFilter(exception_handler); /* Dark voodoo which, I think, gets boost::filesystem::path to @@ -371,14 +357,16 @@ dcpomatic_setup () set_terminate (terminate); - libdcp::init (); + Pango::init (); + dcp::init (); Ratio::setup_ratios (); VideoContentScale::setup_scales (); DCPContentType::setup_dcp_content_types (); Scaler::setup_scalers (); Filter::setup_filters (); - SoundProcessor::setup_sound_processors (); + CinemaSoundProcessor::setup_cinema_sound_processors (); + AudioProcessor::setup_audio_processors (); ui_thread = boost::this_thread::get_id (); } @@ -401,7 +389,7 @@ mo_path () boost::filesystem::path mo_path () { - return "DCP-o-matic.app/Contents/Resources"; + return "DCP-o-matic 2.app/Contents/Resources"; } #endif @@ -486,7 +474,10 @@ md5_digest (vector files, shared_ptr job) while (remaining > 0) { int const t = min (remaining, buffer_size); - fread (buffer, 1, t, f); + int const r = fread (buffer, 1, t, f); + if (r != t) { + throw ReadFileError (files[i], errno); + } digester.add (buffer, t); remaining -= t; @@ -656,6 +647,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 @@ -760,17 +762,6 @@ ensure_ui_thread () assert (boost::this_thread::get_id() == ui_thread); } -/** @param v Content video frame. - * @param audio_sample_rate Source audio sample rate. - * @param frames_per_second Number of video frames per second. - * @return Equivalent number of audio frames for `v'. - */ -int64_t -video_frames_to_audio_frames (VideoContent::Frame v, float audio_sample_rate, float frames_per_second) -{ - return ((int64_t) v * audio_sample_rate / frames_per_second); -} - string audio_channel_name (int c) { @@ -806,6 +797,14 @@ valid_image_file (boost::filesystem::path f) return (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".tga" || ext == ".dpx"); } +bool +valid_j2k_file (boost::filesystem::path f) +{ + string ext = f.extension().string(); + transform (ext.begin(), ext.end(), ext.begin(), ::tolower); + return (ext == ".j2k" || ext == ".j2c"); +} + string tidy_for_filename (string f) { @@ -821,59 +820,6 @@ tidy_for_filename (string f) return t; } -shared_ptr -make_signer () -{ - boost::filesystem::path const sd = Config::instance()->signer_chain_directory (); - - /* Remake the chain if any of it is missing */ - - list files; - files.push_back ("ca.self-signed.pem"); - files.push_back ("intermediate.signed.pem"); - files.push_back ("leaf.signed.pem"); - files.push_back ("leaf.key"); - - list::const_iterator i = files.begin(); - while (i != files.end()) { - boost::filesystem::path p (sd); - p /= *i; - if (!boost::filesystem::exists (p)) { - boost::filesystem::remove_all (sd); - boost::filesystem::create_directories (sd); - libdcp::make_signer_chain (sd, openssl_path ()); - break; - } - - ++i; - } - - libdcp::CertificateChain chain; - - { - boost::filesystem::path p (sd); - p /= "ca.self-signed.pem"; - chain.add (shared_ptr (new libdcp::Certificate (p))); - } - - { - boost::filesystem::path p (sd); - p /= "intermediate.signed.pem"; - chain.add (shared_ptr (new libdcp::Certificate (p))); - } - - { - boost::filesystem::path p (sd); - p /= "leaf.signed.pem"; - chain.add (shared_ptr (new libdcp::Certificate (p))); - } - - boost::filesystem::path signer_key (sd); - signer_key /= "leaf.key"; - - return shared_ptr (new libdcp::Signer (chain, signer_key)); -} - map split_get_request (string url) { @@ -920,14 +866,14 @@ split_get_request (string url) return r; } -libdcp::Size -fit_ratio_within (float ratio, libdcp::Size full_frame) +dcp::Size +fit_ratio_within (float ratio, dcp::Size full_frame, int round) { if (ratio < full_frame.ratio ()) { - return libdcp::Size (rint (full_frame.height * ratio), full_frame.height); + return dcp::Size (round_to (full_frame.height * ratio, round), full_frame.height); } - return libdcp::Size (full_frame.width, rint (full_frame.width / ratio)); + return dcp::Size (full_frame.width, round_to (full_frame.width / ratio, round)); } void * @@ -958,12 +904,34 @@ divide_with_round (int64_t a, int64_t b) } } +/** Return a user-readable string summarising the versions of our dependencies */ +string +dependency_version_summary () +{ + 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_(", ") + << N_("libavformat ") << ffmpeg_version_to_string (avformat_version()) << N_(", ") + << N_("libavutil ") << ffmpeg_version_to_string (avutil_version()) << N_(", ") + << N_("libswscale ") << ffmpeg_version_to_string (swscale_version()) << N_(", ") + << MagickVersion << N_(", ") + << N_("libssh ") << ssh_version (0) << N_(", ") + << N_("libdcp ") << dcp::version << N_(" git ") << dcp::git_commit; + + 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 +939,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 +956,7 @@ ScopedTemporary::open (char const * params) return _open; } +/** Close the file */ void ScopedTemporary::close () { @@ -992,3 +965,16 @@ ScopedTemporary::close () _open = 0; } } + +ContentTimePeriod +subtitle_period (AVSubtitle const & sub) +{ + ContentTime const packet_time = ContentTime::from_seconds (static_cast (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; +}