X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Futil.cc;h=6a353818cb2e26d42e74c1289826df44543b5205;hb=380cda69c5806cd38906ae3bd144e71aa17c38f5;hp=c165a5129bae42bfe646b772d56d246a7102935f;hpb=ccd69987ab861ea4521836fa2a5227e2dfac3479;p=dcpomatic.git diff --git a/src/lib/util.cc b/src/lib/util.cc index c165a5129..6a353818c 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -34,6 +34,7 @@ #include "cross.h" #include "crypto.h" #include "dcp_content_type.h" +#include "dcpomatic_log.h" #include "digester.h" #include "exceptions.h" #include "ffmpeg_image_proxy.h" @@ -49,22 +50,22 @@ #include "text_decoder.h" #include "util.h" #include "video_content.h" -#include "warnings.h" +#include #include #include -#include -#include #include +#include #include #include -#include -DCPOMATIC_DISABLE_WARNINGS +#include +#include +LIBDCP_DISABLE_WARNINGS extern "C" { #include #include #include } -DCPOMATIC_ENABLE_WARNINGS +LIBDCP_ENABLE_WARNINGS #include #include #include @@ -75,9 +76,9 @@ DCPOMATIC_ENABLE_WARNINGS #include #include #include -DCPOMATIC_DISABLE_WARNINGS +LIBDCP_DISABLE_WARNINGS #include -DCPOMATIC_ENABLE_WARNINGS +LIBDCP_ENABLE_WARNINGS #ifdef DCPOMATIC_WINDOWS #include #include @@ -242,7 +243,7 @@ addr2line (void const * const addr) return system(addr2line_cmd); } -DCPOMATIC_DISABLE_WARNINGS +LIBDCP_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. * Adapted from code here: http://spin.atomicobject.com/2013/01/13/exceptions-stack-traces-c/ @@ -250,9 +251,11 @@ DCPOMATIC_DISABLE_WARNINGS LONG WINAPI exception_handler(struct _EXCEPTION_POINTERS * info) { - auto f = fopen_boost (backtrace_file, "w"); - fprintf (f, "C-style exception %d\n", info->ExceptionRecord->ExceptionCode); - fclose(f); + dcp::File f(backtrace_file, "w"); + if (f) { + fprintf(f.get(), "C-style exception %d\n", info->ExceptionRecord->ExceptionCode); + f.close(); + } if (info->ExceptionRecord->ExceptionCode != EXCEPTION_STACK_OVERFLOW) { CONTEXT* context = info->ContextRecord; @@ -299,7 +302,7 @@ exception_handler(struct _EXCEPTION_POINTERS * info) return EXCEPTION_CONTINUE_SEARCH; } -DCPOMATIC_ENABLE_WARNINGS +LIBDCP_ENABLE_WARNINGS #endif void @@ -371,10 +374,10 @@ dcpomatic_setup () #endif #ifdef DCPOMATIC_HAVE_AVREGISTER -DCPOMATIC_DISABLE_WARNINGS +LIBDCP_DISABLE_WARNINGS av_register_all (); avfilter_register_all (); -DCPOMATIC_ENABLE_WARNINGS +LIBDCP_ENABLE_WARNINGS #endif #ifdef DCPOMATIC_OSX @@ -399,7 +402,7 @@ DCPOMATIC_ENABLE_WARNINGS #endif Pango::init (); - dcp::init (tags_path()); + dcp::init (libdcp_resources_path()); #if defined(DCPOMATIC_WINDOWS) || defined(DCPOMATIC_OSX) /* Render something to fontconfig to create its cache */ @@ -408,8 +411,8 @@ DCPOMATIC_ENABLE_WARNINGS optional(), 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(), 0 ); - subs.push_back (StringText(ss, 0)); - render_text (subs, list>(), dcp::Size(640, 480), DCPTime(), 24); + subs.push_back (StringText(ss, 0, {}, dcp::Standard::SMPTE)); + render_text (subs, dcp::Size(640, 480), DCPTime(), 24); #endif Ratio::setup_ratios (); @@ -422,6 +425,8 @@ DCPOMATIC_ENABLE_WARNINGS curl_global_init (CURL_GLOBAL_ALL); ui_thread = boost::this_thread::get_id (); + + capture_asdcp_logs (); } #ifdef DCPOMATIC_WINDOWS @@ -490,16 +495,15 @@ digest_head_tail (vector files, boost::uintmax_t size) char* p = buffer.get (); int i = 0; while (i < int64_t (files.size()) && to_do > 0) { - auto f = fopen_boost (files[i], "rb"); + dcp::File f(files[i], "rb"); if (!f) { throw OpenFileError (files[i].string(), errno, OpenFileError::READ); } boost::uintmax_t this_time = min (to_do, boost::filesystem::file_size (files[i])); - checked_fread (p, this_time, f, files[i]); + f.checked_read(p, this_time); p += this_time; to_do -= this_time; - fclose (f); ++i; } @@ -510,17 +514,16 @@ digest_head_tail (vector files, boost::uintmax_t size) p = buffer.get (); i = files.size() - 1; while (i >= 0 && to_do > 0) { - auto f = fopen_boost (files[i], "rb"); + dcp::File f(files[i], "rb"); if (!f) { throw OpenFileError (files[i].string(), errno, OpenFileError::READ); } boost::uintmax_t this_time = min (to_do, boost::filesystem::file_size (files[i])); - dcpomatic_fseek (f, -this_time, SEEK_END); - checked_fread (p, this_time, f, files[i]); + f.seek(-this_time, SEEK_END); + f.checked_read(p, this_time); p += this_time; to_do -= this_time; - fclose (f); --i; } @@ -529,19 +532,14 @@ digest_head_tail (vector files, boost::uintmax_t size) return digester.get (); } -/** Round a number up to the nearest multiple of another number. - * @param c Index. - * @param stride Array of numbers to round, indexed by c. - * @param t Multiple to round to. - * @return Rounded number. - */ -int -stride_round_up (int c, int const * stride, int t) + +string +simple_digest (vector paths) { - int const a = stride[c] + (t - 1); - return a - (a % t); + return digest_head_tail(paths, 1000000) + raw_convert(boost::filesystem::file_size(paths.front())); } + /** Trip an assert if the caller is not in the UI thread */ void ensure_ui_thread () @@ -596,14 +594,14 @@ short_audio_channel_name (int c) _("Rs"), _("HI"), _("VI"), - _("Lc"), - _("Rc"), + _("9"), + _("10"), _("BsL"), _("BsR"), _("DBP"), _("DBS"), _("Sign"), - "" + _("16") }; return channels[c]; @@ -664,16 +662,6 @@ 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) -{ - auto p = av_malloc (s); - if (!p) { - throw bad_alloc (); - } - return p; -} - map split_get_request (string url) { @@ -720,44 +708,47 @@ split_get_request (string url) return r; } + +static string -video_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary) +asset_filename (shared_ptr asset, string type, int reel_index, int reel_count, optional summary, string extension) { dcp::NameFormat::Map values; - values['t'] = "j2c"; - values['r'] = raw_convert (reel_index + 1); - values['n'] = raw_convert (reel_count); + values['t'] = type; + values['r'] = raw_convert(reel_index + 1); + values['n'] = raw_convert(reel_count); if (summary) { - values['c'] = careful_string_filter (summary.get()); + values['c'] = careful_string_filter(summary.get()); } - return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + ".mxf"); + return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + extension); +} + + +string +video_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary) +{ + return asset_filename(asset, "j2c", reel_index, reel_count, summary, ".mxf"); } + string audio_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary) { - dcp::NameFormat::Map values; - values['t'] = "pcm"; - values['r'] = raw_convert (reel_index + 1); - values['n'] = raw_convert (reel_count); - if (summary) { - values['c'] = careful_string_filter (summary.get()); - } - return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + ".mxf"); + return asset_filename(asset, "pcm", reel_index, reel_count, summary, ".mxf"); +} + + +string +subtitle_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary, string extension) +{ + return asset_filename(asset, "sub", reel_index, reel_count, summary, extension); } string atmos_asset_filename (shared_ptr asset, int reel_index, int reel_count, optional summary) { - dcp::NameFormat::Map values; - values['t'] = "atmos"; - values['r'] = raw_convert (reel_index + 1); - values['n'] = raw_convert (reel_count); - if (summary) { - values['c'] = careful_string_filter (summary.get()); - } - return Config::instance()->dcp_asset_filename_format().get(values, "_" + asset->id() + ".mxf"); + return asset_filename(asset, "atmos", reel_index, reel_count, summary, ".mxf"); } @@ -782,19 +773,32 @@ careful_string_filter (string s) */ /* First transliterate using libicu to try to remove accents in a "nice" way */ - auto icu_utf16 = icu::UnicodeString::fromUTF8(icu::StringPiece(s)); + auto transliterated = icu::UnicodeString::fromUTF8(icu::StringPiece(s)); auto status = U_ZERO_ERROR; auto transliterator = icu::Transliterator::createInstance("NFD; [:M:] Remove; NFC", UTRANS_FORWARD, status); - transliterator->transliterate(icu_utf16); - s.clear (); - icu_utf16.toUTF8String(s); + transliterator->transliterate(transliterated); + + /* Some things are missed by ICU's transliterator */ + std::map replacements = { + { L'ł', L'l' }, + { L'Ł', L'L' } + }; + + icu::UnicodeString transliterated_more; + for (int i = 0; i < transliterated.length(); ++i) { + auto replacement = replacements.find(transliterated[i]); + if (replacement != replacements.end()) { + transliterated_more += replacement->second; + } else { + transliterated_more += transliterated[i]; + } + } /* Then remove anything that's not in a very limited character set */ - wstring ws = boost::locale::conv::utf_to_utf(s); - string out; - string const allowed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_%.+"; - for (size_t i = 0; i < ws.size(); ++i) { - wchar_t c = ws[i]; + wstring out; + wstring const allowed = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_%.+"; + for (int i = 0; i < transliterated_more.length(); ++i) { + wchar_t c = transliterated_more[i]; if (allowed.find(c) != string::npos) { out += c; } @@ -869,45 +873,6 @@ remap (shared_ptr input, int output_channels, AudioMapping m return mapped; } -Eyes -increment_eyes (Eyes e) -{ - if (e == Eyes::LEFT) { - return Eyes::RIGHT; - } - - return Eyes::LEFT; -} - -void -checked_fwrite (void const * ptr, size_t size, FILE* stream, boost::filesystem::path path) -{ - size_t N = fwrite (ptr, 1, size, stream); - if (N != size) { - if (ferror(stream)) { - fclose (stream); - throw FileError (String::compose("fwrite error %1", errno), path); - } else { - fclose (stream); - throw FileError ("Unexpected short write", path); - } - } -} - -void -checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path path) -{ - size_t N = fread (ptr, 1, size, stream); - if (N != size) { - if (ferror(stream)) { - fclose (stream); - throw FileError (String::compose("fread error %1", errno), path); - } else { - fclose (stream); - throw FileError ("Unexpected short read", path); - } - } -} size_t utf8_strlen (string s) @@ -1049,64 +1014,38 @@ show_jobs_on_console (bool progress) void copy_in_bits (boost::filesystem::path from, boost::filesystem::path to, std::function progress) { - auto f = fopen_boost (from, "rb"); + dcp::File f(from, "rb"); if (!f) { throw OpenFileError (from, errno, OpenFileError::READ); } - auto t = fopen_boost (to, "wb"); + dcp::File t(to, "wb"); if (!t) { - fclose (f); throw OpenFileError (to, errno, OpenFileError::WRITE); } /* on the order of a second's worth of copying */ boost::uintmax_t const chunk = 20 * 1024 * 1024; - auto buffer = static_cast (malloc(chunk)); - if (!buffer) { - throw std::bad_alloc (); - } + std::vector buffer(chunk); boost::uintmax_t const total = boost::filesystem::file_size (from); boost::uintmax_t remaining = total; while (remaining) { boost::uintmax_t this_time = min (chunk, remaining); - size_t N = fread (buffer, 1, chunk, f); + size_t N = f.read(buffer.data(), 1, chunk); if (N < this_time) { - fclose (f); - fclose (t); - free (buffer); throw ReadFileError (from, errno); } - N = fwrite (buffer, 1, this_time, t); + N = t.write(buffer.data(), 1, this_time); if (N < this_time) { - fclose (f); - fclose (t); - free (buffer); throw WriteFileError (to, errno); } progress (1 - float(remaining) / total); remaining -= this_time; } - - fclose (f); - fclose (t); - free (buffer); -} - -double -db_to_linear (double db) -{ - return pow(10, db / 20); -} - -double -linear_to_db (double linear) -{ - return 20 * log10(linear); } @@ -1172,6 +1111,9 @@ default_font_file () if (!boost::filesystem::exists(liberation_normal)) { liberation_normal = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"; } + if (!boost::filesystem::exists(liberation_normal)) { + liberation_normal = "/usr/share/fonts/liberation-sans/LiberationSans-Regular.ttf"; + } return liberation_normal; } @@ -1202,3 +1144,36 @@ start_of_thread (string) } #endif + +class LogSink : public Kumu::ILogSink +{ +public: + LogSink () {} + LogSink (LogSink const&) = delete; + LogSink& operator= (LogSink const&) = delete; + + void WriteEntry(const Kumu::LogEntry& entry) override { + Kumu::AutoMutex L(m_lock); + WriteEntryToListeners(entry); + if (entry.TestFilter(m_filter)) { + string buffer; + entry.CreateStringWithOptions(buffer, m_options); + LOG_GENERAL("asdcplib: %1", buffer); + } + } +}; + + +void +capture_asdcp_logs () +{ + static LogSink log_sink; + Kumu::SetDefaultLogSink(&log_sink); +} + + +string +error_details(boost::system::error_code ec) +{ + return String::compose("%1:%2:%3", ec.category().name(), ec.value(), ec.message()); +}