Fix incorrect alpha channel scaling with RGBA64.
[dcpomatic.git] / src / lib / hints.cc
index b23f32238182e1f603655ada06a2e24d51bc1807..7537317d40b77d89b80876699cadb48230621622 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2016-2021 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2016-2022 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
-#include "dcp_content_type.h"
-#include "hints.h"
-#include "types.h"
-#include "film.h"
-#include "content.h"
-#include "video_content.h"
-#include "text_content.h"
-#include "audio_processor.h"
-#include "font.h"
-#include "font_data.h"
-#include "ratio.h"
+
 #include "audio_analysis.h"
+#include "audio_content.h"
+#include "audio_processor.h"
 #include "compose.hpp"
-#include "util.h"
+#include "config.h"
+#include "constants.h"
+#include "content.h"
 #include "cross.h"
+#include "dcp_content_type.h"
+#include "film.h"
+#include "font.h"
+#include "hints.h"
+#include "maths_util.h"
 #include "player.h"
+#include "ratio.h"
+#include "text_content.h"
+#include "video_content.h"
 #include "writer.h"
 #include <dcp/cpl.h>
+#include <dcp/filesystem.h>
 #include <dcp/raw_convert.h>
 #include <dcp/reel.h>
 #include <dcp/reel_closed_caption_asset.h>
 
 #include "i18n.h"
 
+
 using std::cout;
 using std::make_shared;
 using std::max;
-using std::min;
-using std::pair;
 using std::shared_ptr;
 using std::string;
-using std::vector;
 using std::weak_ptr;
 using boost::optional;
 using boost::bind;
@@ -82,12 +83,14 @@ Hints::Hints (weak_ptr<const Film> weak_film)
 
 }
 
+
 void
 Hints::start ()
 {
        _thread = boost::thread (bind(&Hints::thread, this));
 }
 
+
 Hints::~Hints ()
 {
        boost::this_thread::disable_interruption dis;
@@ -152,7 +155,7 @@ Hints::check_unusual_container ()
 {
        auto const film_container = film()->container()->id();
        if (film_container != "185" && film_container != "239") {
-               hint (_("Your DCP uses an unusual container ratio.  This may cause problems on some projectors.  If possible, use Flat or Scope for the DCP container ratio"));
+               hint (_("Your DCP uses an unusual container ratio.  This may cause problems on some projectors.  If possible, use Flat or Scope for the DCP container ratio."));
        }
 }
 
@@ -193,7 +196,7 @@ Hints::check_frame_rate ()
        case 50:
        case 60:
                /* You almost certainly want to go to half frame rate */
-               hint (String::compose(_("You are set up for a DCP at a frame rate of %1 fps.  This frame rate is not supported by all projectors.  You are advised to change the DCP frame rate to %2 fps."), f->video_frame_rate(), f->video_frame_rate() / 2));
+               hint (String::compose(_("You are set up for a DCP at a frame rate of %1 fps.  This frame rate is not supported by all projectors.  It is advisable to change the DCP frame rate to %2 fps."), f->video_frame_rate(), f->video_frame_rate() / 2));
                break;
        }
 }
@@ -233,7 +236,7 @@ Hints::check_speed_up ()
        }
 
        if (worst_speed_up > 25.5/24.0) {
-               hint (_("There is a large difference between the frame rate of your DCP and that of some of your content.  This will cause your audio to play back at a much lower or higher pitch than it should.  You are advised to set your DCP frame rate to one closer to your content, provided that your target projection systems support your chosen DCP rate."));
+               hint (_("There is a large difference between the frame rate of your DCP and that of some of your content.  This will cause your audio to play back at a much lower or higher pitch than it should.  It is advisable to set your DCP frame rate to one closer to your content, provided that your target projection systems support your chosen DCP rate."));
        }
 
 }
@@ -243,7 +246,7 @@ void
 Hints::check_interop ()
 {
        if (film()->interop()) {
-               hint (_("In general it is now advisable to make SMPTE DCPs unless you have a particular reason to use Interop.  You are advised to set your DCP to use the SMPTE standard in the \"DCP\" tab."));
+               hint (_("In general it is now advisable to make SMPTE DCPs unless you have a particular reason to use Interop.  It is advisable to set your DCP to use the SMPTE standard in the \"DCP\" tab."));
        }
 }
 
@@ -256,8 +259,8 @@ Hints::check_big_font_files ()
                for (auto i: film()->content()) {
                        for (auto j: i->text) {
                                for (auto k: j->fonts()) {
-                                       optional<boost::filesystem::path> const p = k->file ();
-                                       if (p && boost::filesystem::file_size(p.get()) >= (MAX_FONT_FILE_SIZE - SIZE_SLACK)) {
+                                       auto const p = k->file ();
+                                       if (p && dcp::filesystem::file_size(p.get()) >= (MAX_FONT_FILE_SIZE - SIZE_SLACK)) {
                                                big_font_files = true;
                                        }
                                }
@@ -276,7 +279,7 @@ Hints::check_vob ()
 {
        int vob = 0;
        for (auto i: film()->content()) {
-               if (boost::algorithm::starts_with (i->path(0).filename().string(), "VTS_")) {
+               if (boost::algorithm::starts_with(i->path(0).filename().string(), "VTS_")) {
                        ++vob;
                }
        }
@@ -308,7 +311,7 @@ bool
 Hints::check_loudness ()
 {
        auto path = film()->audio_analysis_path(film()->playlist());
-       if (!boost::filesystem::exists(path)) {
+       if (!dcp::filesystem::exists(path)) {
                return false;
        }
 
@@ -330,8 +333,8 @@ Hints::check_loudness ()
 
                ch = ch.substr (0, ch.length() - 2);
 
-               if (!ch.empty ()) {
-                       hint (String::compose (
+               if (!ch.empty()) {
+                       hint(String::compose(
                                        _("Your audio level is very high (on %1).  You should reduce the gain of your audio content."),
                                        ch
                                        )
@@ -350,7 +353,7 @@ static
 bool
 subtitle_mxf_too_big (shared_ptr<dcp::SubtitleAsset> asset)
 {
-       return asset && asset->file() && boost::filesystem::file_size(*asset->file()) >= (MAX_TEXT_MXF_SIZE - SIZE_SLACK);
+       return asset && asset->file() && dcp::filesystem::file_size(*asset->file()) >= (MAX_TEXT_MXF_SIZE - SIZE_SLACK);
 }
 
 
@@ -367,71 +370,105 @@ Hints::check_out_of_range_markers ()
 
 
 void
-Hints::thread ()
-try
+Hints::scan_content(shared_ptr<const Film> film)
 {
-       auto film = _film.lock ();
-       if (!film) {
+       auto const check_loudness_done = check_loudness();
+
+       auto content = film->playlist()->content();
+       auto iter = std::find_if(content.begin(), content.end(), [](shared_ptr<const Content> content) {
+               auto text_iter = std::find_if(content->text.begin(), content->text.end(), [](shared_ptr<const TextContent> text) {
+                       return text->use();
+               });
+               return text_iter != content->text.end();
+       });
+
+       auto const have_text = iter != content.end();
+
+       if (check_loudness_done && !have_text) {
+               /* We don't need to check loudness, and we don't have any active text to check,
+                * so a scan of the content is pointless.
+                */
                return;
        }
 
-       auto content = film->content ();
-
-       check_interop ();
-       check_big_font_files ();
-       check_few_audio_channels ();
-       check_upmixers ();
-       check_incorrect_container ();
-       check_unusual_container ();
-       check_high_j2k_bandwidth ();
-       check_frame_rate ();
-       check_4k_3d ();
-       check_speed_up ();
-       check_vob ();
-       check_3d_in_2d ();
-       auto const check_loudness_done = check_loudness ();
-       check_ffec_and_ffmc_in_smpte_feature ();
-       check_out_of_range_markers ();
-       check_text_languages ();
-
-       if (check_loudness_done) {
+       if (check_loudness_done && have_text) {
                emit (bind(boost::ref(Progress), _("Examining subtitles and closed captions")));
+       } else if (!check_loudness_done && !have_text) {
+               emit (bind(boost::ref(Progress), _("Examining audio")));
        } else {
                emit (bind(boost::ref(Progress), _("Examining audio, subtitles and closed captions")));
        }
 
-       auto player = make_shared<Player>(film);
-       player->set_ignore_video ();
-       if (check_loudness_done) {
+       auto player = make_shared<Player>(film, Image::Alignment::COMPACT);
+       player->set_ignore_video();
+       if (check_loudness_done || _disable_audio_analysis) {
                /* We don't need to analyse audio because we already loaded a suitable analysis */
-               player->set_ignore_audio ();
+               player->set_ignore_audio();
+       } else {
+               /* Send auto to the analyser to check loudness */
+               player->Audio.connect(bind(&Hints::audio, this, _1, _2));
        }
-       player->Audio.connect (bind(&Hints::audio, this, _1, _2));
-       player->Text.connect (bind(&Hints::text, this, _1, _2, _3, _4));
+       player->Text.connect(bind(&Hints::text, this, _1, _2, _3, _4));
 
        struct timeval last_pulse;
-       gettimeofday (&last_pulse, 0);
+       gettimeofday(&last_pulse, 0);
+
+       _writer->write(player->get_subtitle_fonts());
 
        while (!player->pass()) {
 
                struct timeval now;
-               gettimeofday (&now, 0);
+               gettimeofday(&now, 0);
                if ((seconds(now) - seconds(last_pulse)) > 1) {
                        if (_stop) {
                                return;
                        }
-                       emit (bind (boost::ref(Pulse)));
+                       emit(bind(boost::ref(Pulse)));
                        last_pulse = now;
                }
        }
 
        if (!check_loudness_done) {
-               _analyser.finish ();
+               _analyser.finish();
                _analyser.get().write(film->audio_analysis_path(film->playlist()));
-               check_loudness ();
+               check_loudness();
        }
+}
 
-       _writer->write (player->get_subtitle_fonts());
+
+void
+Hints::thread ()
+try
+{
+       start_of_thread ("Hints");
+
+       auto film = _film.lock ();
+       if (!film) {
+               return;
+       }
+
+       auto content = film->content ();
+
+       check_certificates ();
+       check_interop ();
+       check_big_font_files ();
+       check_few_audio_channels ();
+       check_upmixers ();
+       check_incorrect_container ();
+       check_unusual_container ();
+       check_high_j2k_bandwidth ();
+       check_frame_rate ();
+       check_4k_3d ();
+       check_speed_up ();
+       check_vob ();
+       check_3d_in_2d ();
+       check_ffec_and_ffmc_in_smpte_feature ();
+       check_out_of_range_markers ();
+       check_subtitle_languages();
+       check_audio_language ();
+       check_8_or_16_audio_channels();
+
+       scan_content(film);
 
        if (_long_subtitle && !_very_long_subtitle) {
                hint (_("At least one of your subtitle lines has more than 52 characters.  It is recommended to make each line 52 characters at most in length."));
@@ -443,8 +480,8 @@ try
        bool ccap_mxf_too_big = false;
        bool subs_mxf_too_big = false;
 
-       boost::filesystem::path dcp_dir = film->dir("hints") / dcpomatic::get_process_id();
-       boost::filesystem::remove_all (dcp_dir);
+       auto dcp_dir = film->dir("hints") / dcpomatic::get_process_id();
+       dcp::filesystem::remove_all(dcp_dir);
 
        _writer->finish (film->dir("hints") / dcpomatic::get_process_id());
 
@@ -476,7 +513,7 @@ try
                        subs_mxf_too_big = true;
                }
        }
-       boost::filesystem::remove_all (dcp_dir);
+       dcp::filesystem::remove_all(dcp_dir);
 
        emit (bind(boost::ref(Finished)));
 }
@@ -489,6 +526,7 @@ catch (...)
        store_current ();
 }
 
+
 void
 Hints::hint (string h)
 {
@@ -576,7 +614,30 @@ Hints::open_subtitle (PlayerText text, DCPTimePeriod period)
                hint (_("At least one of your subtitles starts less than 2 frames after the previous one.  It is advisable to make the gap between subtitles at least 2 frames."));
        }
 
-       if (text.string.size() > 3 && !_too_many_subtitle_lines) {
+       struct VPos
+       {
+       public:
+               dcp::VAlign align;
+               float position;
+
+               bool operator<(VPos const& other) const {
+                       if (static_cast<int>(align) != static_cast<int>(other.align)) {
+                               return static_cast<int>(align) < static_cast<int>(other.align);
+                       }
+                       return position < other.position;
+               }
+       };
+
+       /* This is rather an approximate way to count distinct lines, but I guess it will do;
+        * to make it better we need to take into account font metrics, and the SMPTE alignment
+        * debacle, and so on.
+        */
+       std::set<VPos> lines;
+       for (auto const& line: text.string) {
+               lines.insert({ line.v_align(), line.v_position() });
+       }
+
+       if (lines.size() > 3 && !_too_many_subtitle_lines) {
                _too_many_subtitle_lines = true;
                hint (_("At least one of your subtitles has more than 3 lines.  It is advisable to use no more than 3 lines."));
        }
@@ -616,16 +677,71 @@ Hints::join ()
 
 
 void
-Hints::check_text_languages ()
+Hints::check_subtitle_languages()
 {
        for (auto i: film()->content()) {
                for (auto j: i->text) {
-                       if (j->use() && !j->language()) {
-                               hint (_("At least one piece of subtitle or closed caption content has no specified language.  "
-                                       "It is advisable to set the language for each piece of subtitle or closed caption content "
-                                       "in the \"Content→Timed text\", \"Content→Open subtitles\" or \"Content→Closed captions\" tab."));
+                       if (j->use() && j->type() == TextType::OPEN_SUBTITLE && !j->language()) {
+                               hint (_("At least one piece of subtitle content has no specified language.  "
+                                       "It is advisable to set the language for each piece of subtitle content "
+                                       "in the \"Content→Timed text\" or \"Content→Open subtitles\" tab."));
                                return;
                        }
                }
        }
 }
+
+
+void
+Hints::check_audio_language ()
+{
+       auto content = film()->content();
+       auto mapped_audio =
+               std::find_if(content.begin(), content.end(), [](shared_ptr<const Content> c) {
+                       return c->audio && !c->audio->mapping().mapped_output_channels().empty();
+               });
+
+       if (mapped_audio != content.end() && !film()->audio_language()) {
+               hint (_("Some of your content has audio but you have not set the audio language.  It is advisable to set the audio language "
+                       "in the \"DCP\" tab unless your audio has no spoken parts."));
+       }
+}
+
+
+void
+Hints::check_certificates ()
+{
+       auto bad = Config::instance()->check_certificates();
+       if (!bad) {
+               return;
+       }
+
+       switch (*bad) {
+       case Config::BAD_SIGNER_UTF8_STRINGS:
+               hint(_("The certificate chain that DCP-o-matic uses for signing DCPs and KDMs contains a small error "
+                      "which will prevent DCPs from being validated correctly on some systems.  It is advisable to "
+                      "re-create the signing certificate chain by clicking the \"Re-make certificates and key...\" "
+                      "button in the Keys page of Preferences."));
+               break;
+       case Config::BAD_SIGNER_VALIDITY_TOO_LONG:
+               hint(_("The certificate chain that DCP-o-matic uses for signing DCPs and KDMs has a validity period "
+                      "that is too long.  This will cause problems playing back DCPs on some systems. "
+                      "It is advisable to re-create the signing certificate chain by clicking the "
+                      "\"Re-make certificates and key...\" button in the Keys page of Preferences."));
+               break;
+       default:
+               /* Some bad situations can't happen here as DCP-o-matic would have refused to start until they are fixed */
+               break;
+       }
+}
+
+
+void
+Hints::check_8_or_16_audio_channels()
+{
+       auto const channels = film()->audio_channels();
+       if (channels != 8 && channels != 16) {
+               hint(String::compose(_("Your DCP has %1 audio channels, rather than 8 or 16.  This may cause some distributors to raise QC errors when they check your DCP.  To avoid this, set the DCP audio channels to 8 or 16."), channels));
+       }
+}
+