Emit no audio from DCPs if none is mapped
[dcpomatic.git] / src / lib / hints.cc
index 2dd11899303bab56013dfcaaeaf81786e5c411c5..bbd5ae5d594e5a3b1b81ac4f3ad1ab7bb4ba10ae 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;
@@ -73,28 +74,23 @@ using namespace boost::placeholders;
  */
 
 
-Hints::Hints (weak_ptr<const Film> film)
-       : WeakConstFilm (film)
-       , _writer (new Writer(film, weak_ptr<Job>(), true))
-       , _long_ccap (false)
-       , _overlap_ccap (false)
-       , _too_many_ccap_lines (false)
-       , _early_subtitle (false)
-       , _short_subtitle (false)
-       , _subtitles_too_close (false)
-       , _too_many_subtitle_lines (false)
-       , _long_subtitle (false)
+Hints::Hints (weak_ptr<const Film> weak_film)
+       : WeakConstFilm (weak_film)
+       , _writer (new Writer(weak_film, weak_ptr<Job>(), true))
+       , _analyser (film(), film()->playlist(), true, [](float) {})
        , _stop (false)
 {
 
 }
 
+
 void
 Hints::start ()
 {
        _thread = boost::thread (bind(&Hints::thread, this));
 }
 
+
 Hints::~Hints ()
 {
        boost::this_thread::disable_interruption dis;
@@ -132,8 +128,8 @@ Hints::check_incorrect_container ()
        int narrower_than_scope = 0;
        int scope = 0;
        for (auto i: film()->content()) {
-               if (i->video) {
-                       Ratio const * r = Ratio::nearest_from_ratio(i->video->scaled_size(film()->frame_size()).ratio());
+               if (i->video && i->video->size()) {
+                       auto const r = Ratio::nearest_from_ratio(i->video->scaled_size(film()->frame_size())->ratio());
                        if (r && r->id() == "239") {
                                ++scope;
                        } else if (r && r->id() != "239" && r->id() != "235" && r->id() != "190") {
@@ -159,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."));
        }
 }
 
@@ -200,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;
        }
 }
@@ -240,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."));
        }
 
 }
@@ -250,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."));
        }
 }
 
@@ -263,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;
                                        }
                                }
@@ -283,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;
                }
        }
@@ -310,42 +306,46 @@ Hints::check_3d_in_2d ()
 }
 
 
-void
+/** @return true if the loudness could be checked, false if it could not because no analysis was available */
+bool
 Hints::check_loudness ()
 {
        auto path = film()->audio_analysis_path(film()->playlist());
-       if (boost::filesystem::exists (path)) {
-               try {
-                       auto an = make_shared<AudioAnalysis>(path);
+       if (!dcp::filesystem::exists(path)) {
+               return false;
+       }
 
-                       string ch;
+       try {
+               auto an = make_shared<AudioAnalysis>(path);
 
-                       auto sample_peak = an->sample_peak ();
-                       auto true_peak = an->true_peak ();
+               string ch;
 
-                       for (size_t i = 0; i < sample_peak.size(); ++i) {
-                               float const peak = max (sample_peak[i].peak, true_peak.empty() ? 0 : true_peak[i]);
-                               float const peak_dB = linear_to_db(peak) + an->gain_correction(film()->playlist());
-                               if (peak_dB > -3) {
-                                       ch += dcp::raw_convert<string> (short_audio_channel_name (i)) + ", ";
-                               }
+               auto sample_peak = an->sample_peak ();
+               auto true_peak = an->true_peak ();
+
+               for (size_t i = 0; i < sample_peak.size(); ++i) {
+                       float const peak = max (sample_peak[i].peak, true_peak.empty() ? 0 : true_peak[i]);
+                       float const peak_dB = linear_to_db(peak) + an->gain_correction(film()->playlist());
+                       if (peak_dB > -3) {
+                               ch += dcp::raw_convert<string>(short_audio_channel_name(i)) + ", ";
                        }
+               }
 
-                       ch = ch.substr (0, ch.length() - 2);
+               ch = ch.substr (0, ch.length() - 2);
 
-                       if (!ch.empty ()) {
-                               hint (String::compose (
-                                             _("Your audio level is very high (on %1).  You should reduce the gain of your audio content."),
-                                             ch
-                                             )
-                                       );
-                       }
-               } catch (OldFormatError& e) {
-                       /* The audio analysis is too old to load in; just skip this hint as if
-                          it had never been run.
-                       */
+               if (!ch.empty()) {
+                       hint(String::compose(
+                                       _("Your audio level is very high (on %1).  You should reduce the gain of your audio content."),
+                                       ch
+                                       )
+                            );
                }
+       } catch (OldFormatError& e) {
+               /* The audio analysis is too old to load in */
+               return false;
        }
+
+       return true;
 }
 
 
@@ -353,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);
 }
 
 
@@ -369,9 +369,79 @@ Hints::check_out_of_range_markers ()
 }
 
 
+void
+Hints::scan_content(shared_ptr<const Film> 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;
+       }
+
+       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, 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();
+       } else {
+               /* Send auto to the analyser to check loudness */
+               player->Audio.connect(bind(&Hints::audio, this, _1, _2));
+       }
+       player->Text.connect(bind(&Hints::text, this, _1, _2, _3, _4));
+
+       struct timeval last_pulse;
+       gettimeofday(&last_pulse, 0);
+
+       _writer->write(player->get_subtitle_fonts());
+
+       while (!player->pass()) {
+
+               struct timeval now;
+               gettimeofday(&now, 0);
+               if ((seconds(now) - seconds(last_pulse)) > 1) {
+                       if (_stop) {
+                               return;
+                       }
+                       emit(bind(boost::ref(Pulse)));
+                       last_pulse = now;
+               }
+       }
+
+       if (!check_loudness_done) {
+               _analyser.finish();
+               _analyser.get().write(film->audio_analysis_path(film->playlist()));
+               check_loudness();
+       }
+}
+
+
 void
 Hints::thread ()
+try
 {
+       start_of_thread ("Hints");
+
        auto film = _film.lock ();
        if (!film) {
                return;
@@ -379,6 +449,7 @@ Hints::thread ()
 
        auto content = film->content ();
 
+       check_certificates ();
        check_interop ();
        check_big_font_files ();
        check_few_audio_channels ();
@@ -391,54 +462,28 @@ Hints::thread ()
        check_speed_up ();
        check_vob ();
        check_3d_in_2d ();
-       check_loudness ();
        check_ffec_and_ffmc_in_smpte_feature ();
        check_out_of_range_markers ();
-       check_text_languages ();
+       check_subtitle_languages();
+       check_audio_language ();
+       check_8_or_16_audio_channels();
 
-       emit (bind(boost::ref(Progress), _("Examining closed captions")));
-
-       auto player = make_shared<Player>(film);
-       player->set_ignore_video ();
-       player->set_ignore_audio ();
-       player->Text.connect (bind(&Hints::text, this, _1, _2, _3, _4));
-
-       struct timeval last_pulse;
-       gettimeofday (&last_pulse, 0);
+       scan_content(film);
 
-       try {
-               while (!player->pass()) {
-
-                       struct timeval now;
-                       gettimeofday (&now, 0);
-                       if ((seconds(now) - seconds(last_pulse)) > 1) {
-                               if (_stop) {
-                                       break;
-                               }
-                               emit (bind (boost::ref(Pulse)));
-                               last_pulse = now;
-                       }
-               }
-       } catch (...) {
-               store_current ();
+       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."));
+       } else if (_very_long_subtitle) {
+               hint (_("At least one of your subtitle lines has more than 79 characters.  You should make each line 79 characters at most in length."));
        }
 
-       _writer->write (player->get_subtitle_fonts());
-
        bool ccap_xml_too_big = false;
        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);
 
-       try {
-               _writer->finish (film->dir("hints") / dcpomatic::get_process_id());
-       } catch (...) {
-               store_current ();
-               emit (bind(boost::ref(Finished)));
-               return;
-       }
+       _writer->finish (film->dir("hints") / dcpomatic::get_process_id());
 
        dcp::DCP dcp (dcp_dir);
        dcp.read ();
@@ -468,10 +513,19 @@ Hints::thread ()
                        subs_mxf_too_big = true;
                }
        }
-       boost::filesystem::remove_all (dcp_dir);
+       dcp::filesystem::remove_all(dcp_dir);
 
        emit (bind(boost::ref(Finished)));
 }
+catch (boost::thread_interrupted)
+{
+       /* The Hints object is being destroyed before it has finished, so just give up */
+}
+catch (...)
+{
+       store_current ();
+}
+
 
 void
 Hints::hint (string h)
@@ -479,6 +533,14 @@ Hints::hint (string h)
        emit(bind(boost::ref(Hint), h));
 }
 
+
+void
+Hints::audio (shared_ptr<AudioBuffers> audio, DCPTime time)
+{
+       _analyser.analyse (audio, time);
+}
+
+
 void
 Hints::text (PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
 {
@@ -552,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."));
        }
@@ -562,9 +647,12 @@ Hints::open_subtitle (PlayerText text, DCPTimePeriod period)
                longest_line = max (longest_line, i.text().length());
        }
 
-       if (longest_line > 52 && !_long_subtitle) {
+       if (longest_line > 52) {
                _long_subtitle = true;
-               hint (_("At least one of your subtitle lines has more than 52 characters.  It is advisable to make each line 52 characters at most in length."));
+       }
+
+       if (longest_line > 79) {
+               _very_long_subtitle = true;
        }
 
        _last_subtitle = period;
@@ -589,15 +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\" 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));
+       }
+}
+