Write subtitles and closed captions to a test DCP in the hints thread,
authorCarl Hetherington <cth@carlh.net>
Mon, 7 Dec 2020 00:18:38 +0000 (01:18 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 8 Dec 2020 13:09:02 +0000 (14:09 +0100)
then check the result for Bv2.1 violations (part of #1800).

src/lib/hints.cc
src/lib/hints.h
src/lib/reel_writer.cc
src/lib/reel_writer.h
src/lib/util.h
src/lib/writer.cc
src/lib/writer.h
test/data
test/hints_test.cc
test/reel_writer_test.cc

index 5c9d3d8a41964fc96a33384dce8a2561f7129ffd..13154803579eb5a9379b872471a573c367ca8c1b 100644 (file)
 #include "util.h"
 #include "cross.h"
 #include "player.h"
+#include "writer.h"
+#include <dcp/cpl.h>
 #include <dcp/raw_convert.h>
+#include <dcp/reel.h>
+#include <dcp/reel_closed_caption_asset.h>
+#include <dcp/reel_subtitle_asset.h>
 #include <boost/foreach.hpp>
 #include <boost/algorithm/string.hpp>
 #include <iostream>
@@ -55,8 +60,16 @@ using namespace dcpomatic;
 using namespace boost::placeholders;
 #endif
 
+
+/* When checking to see if things are too big, we'll say they are if they
+ * are more than the target size minus this "slack."
+ */
+#define SIZE_SLACK 4096
+
+
 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)
@@ -226,7 +239,7 @@ Hints::check_big_font_files ()
                        BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
                                BOOST_FOREACH (shared_ptr<Font> k, j->fonts()) {
                                        optional<boost::filesystem::path> const p = k->file ();
-                                       if (p && boost::filesystem::file_size(p.get()) >= (640 * 1024)) {
+                                       if (p && boost::filesystem::file_size(p.get()) >= (MAX_FONT_FILE_SIZE - SIZE_SLACK)) {
                                                big_font_files = true;
                                        }
                                }
@@ -311,6 +324,14 @@ Hints::check_loudness ()
 }
 
 
+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);
+}
+
+
 void
 Hints::thread ()
 {
@@ -339,7 +360,7 @@ Hints::thread ()
        shared_ptr<Player> player (new Player(film));
        player->set_ignore_video ();
        player->set_ignore_audio ();
-       player->Text.connect (bind(&Hints::text, this, _1, _2, _4));
+       player->Text.connect (bind(&Hints::text, this, _1, _2, _3, _4));
 
        struct timeval last_pulse;
        gettimeofday (&last_pulse, 0);
@@ -361,6 +382,45 @@ Hints::thread ()
                store_current ();
        }
 
+       _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);
+       _writer->finish (film->dir("hints") / dcpomatic::get_process_id());
+       dcp::DCP dcp (dcp_dir);
+       dcp.read ();
+       DCPOMATIC_ASSERT (dcp.cpls().size() == 1);
+       BOOST_FOREACH (shared_ptr<dcp::Reel> reel, dcp.cpls().front()->reels()) {
+               BOOST_FOREACH (shared_ptr<dcp::ReelClosedCaptionAsset> ccap, reel->closed_captions()) {
+                       if (ccap->asset() && ccap->asset()->xml_as_string().length() > static_cast<size_t>(MAX_CLOSED_CAPTION_XML_SIZE - SIZE_SLACK) && !ccap_xml_too_big) {
+                               hint (_(
+                                               "At least one of your closed caption files' XML part is larger than " MAX_CLOSED_CAPTION_XML_SIZE_TEXT
+                                               ".  You should divide the DCP into shorter reels."
+                                      ));
+                               ccap_xml_too_big = true;
+                       }
+                       if (subtitle_mxf_too_big(ccap->asset()) && !ccap_mxf_too_big) {
+                               hint (_(
+                                               "At least one of your closed caption files is larger than " MAX_TEXT_MXF_SIZE_TEXT
+                                               " in total.  You should divide the DCP into shorter reels."
+                                      ));
+                               ccap_mxf_too_big = true;
+                       }
+               }
+               if (reel->main_subtitle() && subtitle_mxf_too_big(reel->main_subtitle()->asset()) && !subs_mxf_too_big) {
+                       hint (_(
+                                       "At least one of your subtitle files is larger than " MAX_TEXT_MXF_SIZE_TEXT " in total.  "
+                                       "You should divide the DCP into shorter reels."
+                              ));
+                       subs_mxf_too_big = true;
+               }
+       }
+       boost::filesystem::remove_all (dcp_dir);
+
        emit (bind(boost::ref(Finished)));
 }
 
@@ -371,8 +431,10 @@ Hints::hint (string h)
 }
 
 void
-Hints::text (PlayerText text, TextType type, DCPTimePeriod period)
+Hints::text (PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
 {
+       _writer->write (text, type, track, period);
+
        switch (type) {
        case TEXT_CLOSED_CAPTION:
                closed_caption (text, period);
index b8a8313014c34ebd18855ef0cbf89f87d36b48ff..9164e210659b4b278cab8b200fcad0bc767968a6 100644 (file)
 #include <vector>
 #include <string>
 
+
 class Film;
+class Writer;
+
 
 class Hints : public Signaller, public ExceptionStore, public WeakConstFilm
 {
@@ -53,7 +56,7 @@ private:
 
        void thread ();
        void hint (std::string h);
-       void text (PlayerText text, TextType type, dcpomatic::DCPTimePeriod period);
+       void text (PlayerText text, TextType type, boost::optional<DCPTextTrack> track, dcpomatic::DCPTimePeriod period);
        void closed_caption (PlayerText text, dcpomatic::DCPTimePeriod period);
        void open_subtitle (PlayerText text, dcpomatic::DCPTimePeriod period);
 
@@ -71,6 +74,11 @@ private:
        void check_ffec_and_ffmc_in_smpte_feature ();
 
        boost::thread _thread;
+       /** This is used to make a partial DCP containing only the subtitles and closed captions that
+        *  our final DCP will have.  This means we can see how big the files will be and warn if they
+        *  will be too big.
+        */
+       boost::shared_ptr<Writer> _writer;
 
        bool _long_ccap;
        bool _overlap_ccap;
index e12628c74ad24ac6f6596b035150591fa1bfc289..8e437052624499318620e9e272d9bfad5cce017b 100644 (file)
@@ -92,9 +92,13 @@ mxf_metadata ()
        return meta;
 }
 
-/** @param job Related job, or 0 */
+/** @param job Related job, or 0.
+ *  @param text_only true to enable a special mode where the writer will expect only subtitles and closed captions to be written
+ *  (no picture nor sound) and not give errors in that case.  This is used by the hints system to check the potential sizes of
+ *  subtitle / closed caption files.
+ */
 ReelWriter::ReelWriter (
-       weak_ptr<const Film> weak_film, DCPTimePeriod period, shared_ptr<Job> job, int reel_index, int reel_count
+       weak_ptr<const Film> weak_film, DCPTimePeriod period, shared_ptr<Job> job, int reel_index, int reel_count, bool text_only
        )
        : WeakConstFilm (weak_film)
        , _period (period)
@@ -102,6 +106,7 @@ ReelWriter::ReelWriter (
        , _reel_count (reel_count)
        , _content_summary (film()->content_summary(period))
        , _job (job)
+       , _text_only (text_only)
 {
        /* Create or find our picture asset in a subdirectory, named
           according to those film's parameters which affect the video
@@ -441,7 +446,8 @@ maybe_add_text (
        list<shared_ptr<Font> > const & fonts,
        shared_ptr<const Film> film,
        DCPTimePeriod period,
-       boost::filesystem::path output_dcp
+       boost::filesystem::path output_dcp,
+       bool text_only
        )
 {
        Frame const period_duration = period.duration().frames_round(film->video_frame_rate());
@@ -494,7 +500,7 @@ maybe_add_text (
        }
 
        if (reel_asset) {
-               if (reel_asset->actual_duration() != period_duration) {
+               if (!text_only && reel_asset->actual_duration() != period_duration) {
                        throw ProgrammingError (
                                __FILE__, __LINE__,
                                String::compose ("%1 vs %2", reel_asset->actual_duration(), period_duration)
@@ -614,7 +620,7 @@ ReelWriter::create_reel_text (
        ) const
 {
        shared_ptr<dcp::ReelSubtitleAsset> subtitle = maybe_add_text<dcp::ReelSubtitleAsset> (
-               _subtitle_asset, duration, reel, refs, fonts, film(), _period, output_dcp
+               _subtitle_asset, duration, reel, refs, fonts, film(), _period, output_dcp, _text_only
                );
        if (subtitle && !film()->subtitle_languages().empty()) {
                subtitle->set_language (film()->subtitle_languages().front());
@@ -622,7 +628,7 @@ ReelWriter::create_reel_text (
 
        for (map<DCPTextTrack, shared_ptr<dcp::SubtitleAsset> >::const_iterator i = _closed_caption_assets.begin(); i != _closed_caption_assets.end(); ++i) {
                shared_ptr<dcp::ReelClosedCaptionAsset> a = maybe_add_text<dcp::ReelClosedCaptionAsset> (
-                       i->second, duration, reel, refs, fonts, film(), _period, output_dcp
+                       i->second, duration, reel, refs, fonts, film(), _period, output_dcp, _text_only
                        );
                if (a) {
                        a->set_annotation_text (i->first.name);
@@ -666,10 +672,20 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
        LOG_GENERAL ("create_reel for %1-%2; %3 of %4", _period.from.get(), _period.to.get(), _reel_index, _reel_count);
 
        shared_ptr<dcp::Reel> reel (new dcp::Reel());
-       shared_ptr<dcp::ReelPictureAsset> reel_picture_asset = create_reel_picture (reel, refs);
-       create_reel_sound (reel, refs);
-       create_reel_text (reel, refs, fonts, reel_picture_asset->actual_duration(), output_dcp);
-       create_reel_markers (reel);
+
+       /* This is a bit of a hack; in the strange `_text_only' mode we have no picture, so we don't know
+        * how long the subtitle / CCAP assets should be.  However, since we're only writing them to see
+        * how big they are, we don't care about that.
+        */
+       int64_t duration = 0;
+       if (!_text_only) {
+               shared_ptr<dcp::ReelPictureAsset> reel_picture_asset = create_reel_picture (reel, refs);
+               duration = reel_picture_asset->actual_duration ();
+               create_reel_sound (reel, refs);
+               create_reel_markers (reel);
+       }
+
+       create_reel_text (reel, refs, fonts, duration, output_dcp);
 
        if (_atmos_asset) {
                reel->add (shared_ptr<dcp::ReelAtmosAsset>(new dcp::ReelAtmosAsset(_atmos_asset, 0)));
index c65364567c792f05e1ced6eabebb3963d4334530..6237c2943fdacd5b23f99906f7a4327185d48413 100644 (file)
@@ -64,7 +64,8 @@ public:
                dcpomatic::DCPTimePeriod period,
                boost::shared_ptr<Job> job,
                int reel_index,
-               int reel_count
+               int reel_count,
+               bool text_only
                );
 
        void write (boost::shared_ptr<const dcp::Data> encoded, Frame frame, Eyes eyes);
@@ -122,6 +123,7 @@ private:
        int _reel_count;
        boost::optional<std::string> _content_summary;
        boost::weak_ptr<Job> _job;
+       bool _text_only;
 
        boost::shared_ptr<dcp::PictureAsset> _picture_asset;
        /** picture asset writer, or 0 if we are not writing any picture because we already have one */
index 548dd0475ff5f790fae8ea322755f4adb9da07dd..e23eff07c28575aac81786b067b5edeb0ce6fe96 100644 (file)
@@ -65,6 +65,15 @@ namespace dcp {
 #define MAX_CLOSED_CAPTION_LINES 3
 /** Maximum line length of closed caption viewers, according to SMPTE Bv2.1 */
 #define MAX_CLOSED_CAPTION_LENGTH 32
+/** Maximum size of a subtitle / closed caption MXF in bytes, according to SMPTE Bv2.1 */
+#define MAX_TEXT_MXF_SIZE (115 * 1024 * 1024)
+#define MAX_TEXT_MXF_SIZE_TEXT "115MB"
+/** Maximum size of a font file, in bytes */
+#define MAX_FONT_FILE_SIZE (640 * 1024)
+#define MAX_FONT_FILE_SIZE_TEXT "640KB"
+/** Maximum size of the XML part of a closed caption file, according to SMPTE Bv2.1 */
+#define MAX_CLOSED_CAPTION_XML_SIZE (256 * 1024)
+#define MAX_CLOSED_CAPTION_XML_SIZE_TEXT "256KB"
 
 extern std::string program_name;
 extern bool is_batch_converter;
index 17184918fd733da9edfed66a846a37dc5ebb9ba5..0b85a7f32d8afc177e5045733afa2879cbaa3118 100644 (file)
@@ -79,8 +79,10 @@ ignore_progress (float)
 }
 
 
-/** @param j Job to report progress to, or 0 */
-Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j)
+/** @param j Job to report progress to, or 0.
+ *  @param text_only true to enable only the text (subtitle/ccap) parts of the writer.
+ */
+Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j, bool text_only)
        : WeakConstFilm (weak_film)
        , _job (j)
        , _finish (false)
@@ -92,13 +94,14 @@ Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j)
        , _fake_written (0)
        , _repeat_written (0)
        , _pushed_to_disk (0)
+       , _text_only (text_only)
 {
        shared_ptr<Job> job = _job.lock ();
 
        int reel_index = 0;
        list<DCPTimePeriod> const reels = film()->reels();
        BOOST_FOREACH (DCPTimePeriod p, reels) {
-               _reels.push_back (ReelWriter(weak_film, p, job, reel_index++, reels.size()));
+               _reels.push_back (ReelWriter(weak_film, p, job, reel_index++, reels.size(), text_only));
        }
 
        _last_written.resize (reels.size());
@@ -123,15 +126,19 @@ Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j)
 void
 Writer::start ()
 {
-       _thread = boost::thread (boost::bind(&Writer::thread, this));
+       if (!_text_only) {
+               _thread = boost::thread (boost::bind(&Writer::thread, this));
 #ifdef DCPOMATIC_LINUX
-       pthread_setname_np (_thread.native_handle(), "writer");
+               pthread_setname_np (_thread.native_handle(), "writer");
 #endif
+       }
 }
 
 Writer::~Writer ()
 {
-       terminate_thread (false);
+       if (!_text_only) {
+               terminate_thread (false);
+       }
 }
 
 /** Pass a video frame to the writer for writing to disk at some point.
@@ -522,14 +529,11 @@ Writer::terminate_thread (bool can_throw)
 void
 Writer::finish (boost::filesystem::path output_dcp)
 {
-       if (!_thread.joinable()) {
-               return;
+       if (_thread.joinable()) {
+               LOG_GENERAL_NC ("Terminating writer thread");
+               terminate_thread (true);
        }
 
-       LOG_GENERAL_NC ("Terminating writer thread");
-
-       terminate_thread (true);
-
        LOG_GENERAL_NC ("Finishing ReelWriters");
 
        BOOST_FOREACH (ReelWriter& i, _reels) {
index 1c290e6ca1b82d3082ace04219452f25c0e1a11f..3b5cc3dc3cd98fabb4125f70997f45c7517cb51d 100644 (file)
@@ -99,7 +99,7 @@ bool operator== (QueueItem const & a, QueueItem const & b);
 class Writer : public ExceptionStore, public boost::noncopyable, public WeakConstFilm
 {
 public:
-       Writer (boost::weak_ptr<const Film>, boost::weak_ptr<Job>);
+       Writer (boost::weak_ptr<const Film>, boost::weak_ptr<Job>, bool text_only = false);
        ~Writer ();
 
        void start ();
@@ -189,6 +189,8 @@ private:
        */
        int _pushed_to_disk;
 
+       bool _text_only;
+
        boost::mutex _digest_progresses_mutex;
        std::map<boost::thread::id, float> _digest_progresses;
 
index 66e5e6e6a7bd9fe817a00011c1c1c32c6f4e01c7..578c16ee95b416d4dcec31c72219dac91e632169 160000 (submodule)
--- a/test/data
+++ b/test/data
@@ -1 +1 @@
-Subproject commit 66e5e6e6a7bd9fe817a00011c1c1c32c6f4e01c7
+Subproject commit 578c16ee95b416d4dcec31c72219dac91e632169
index 0e47125d0a8edef8d59cb19eca6a679d05f96239..aabcaaeace0e8f8376c29495e054f0ea90db2356 100644 (file)
@@ -21,7 +21,9 @@
 
 #include "lib/content.h"
 #include "lib/content_factory.h"
+#include "lib/cross.h"
 #include "lib/film.h"
+#include "lib/font.h"
 #include "lib/hints.h"
 #include "lib/text_content.h"
 #include "lib/util.h"
@@ -32,6 +34,7 @@
 
 using std::string;
 using std::vector;
+using boost::optional;
 using boost::shared_ptr;
 
 
@@ -62,7 +65,7 @@ get_hints (shared_ptr<Film> film)
 
 static
 void
-check (TextType type, string name, string expected_hint)
+check (TextType type, string name, optional<string> expected_hint = optional<string>())
 {
        shared_ptr<Film> film = new_test_film2 (name);
        shared_ptr<Content> content = content_factory("test/data/" + name + ".srt").front();
@@ -71,8 +74,12 @@ check (TextType type, string name, string expected_hint)
        BOOST_REQUIRE (!wait_for_jobs());
        vector<string> hints = get_hints (film);
 
-       BOOST_REQUIRE_EQUAL (hints.size(), 1);
-       BOOST_CHECK_EQUAL (hints[0], expected_hint);
+       if (expected_hint) {
+               BOOST_REQUIRE_EQUAL (hints.size(), 1);
+               BOOST_CHECK_EQUAL (hints[0], *expected_hint);
+       } else {
+               BOOST_CHECK (hints.empty());
+       }
 }
 
 
@@ -101,7 +108,7 @@ BOOST_AUTO_TEST_CASE (hint_subtitle_too_early)
        check (
                TEXT_OPEN_SUBTITLE,
                "hint_subtitle_too_early",
-               "It is advisable to put your first subtitle at least 4 seconds after the start of the DCP to make sure it is seen."
+               string("It is advisable to put your first subtitle at least 4 seconds after the start of the DCP to make sure it is seen.")
                );
 }
 
@@ -111,7 +118,7 @@ BOOST_AUTO_TEST_CASE (hint_short_subtitles)
        check (
                TEXT_OPEN_SUBTITLE,
                "hint_short_subtitles",
-               "At least one of your subtitles lasts less than 15 frames.  It is advisable to make each subtitle at least 15 frames long."
+               string("At least one of your subtitles lasts less than 15 frames.  It is advisable to make each subtitle at least 15 frames long.")
                );
 }
 
@@ -121,7 +128,7 @@ BOOST_AUTO_TEST_CASE (hint_subtitles_too_close)
        check (
                TEXT_OPEN_SUBTITLE,
                "hint_subtitles_too_close",
-               "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."
+               string("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.")
              );
 }
 
@@ -131,7 +138,7 @@ BOOST_AUTO_TEST_CASE (hint_many_subtitle_lines)
        check (
                TEXT_OPEN_SUBTITLE,
                "hint_many_subtitle_lines",
-               "At least one of your subtitles has more than 3 lines.  It is advisable to use no more than 3 lines."
+               string("At least one of your subtitles has more than 3 lines.  It is advisable to use no more than 3 lines.")
              );
 }
 
@@ -141,7 +148,64 @@ BOOST_AUTO_TEST_CASE (hint_subtitle_too_long)
        check (
                TEXT_OPEN_SUBTITLE,
                "hint_subtitle_too_long",
-               "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."
+               string("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.")
              );
 }
 
+
+BOOST_AUTO_TEST_CASE (hint_subtitle_mxf_too_big)
+{
+       string const name = "hint_subtitle_mxf_too_big";
+
+       shared_ptr<Film> film = new_test_film2 (name);
+       shared_ptr<Content> content = content_factory("test/data/" + name + ".srt").front();
+       content->text.front()->set_type (TEXT_OPEN_SUBTITLE);
+       for (int i = 1; i < 512; ++i) {
+               shared_ptr<dcpomatic::Font> font(new dcpomatic::Font(String::compose("font_%1", i)));
+               font->set_file ("test/data/LiberationSans-Regular.ttf");
+               content->text.front()->add_font(font);
+       }
+       film->examine_and_add_content (content);
+       BOOST_REQUIRE (!wait_for_jobs());
+       vector<string> hints = get_hints (film);
+
+       BOOST_REQUIRE_EQUAL (hints.size(), 1);
+       BOOST_CHECK_EQUAL (
+               hints[0],
+               "At least one of your subtitle files is larger than " MAX_TEXT_MXF_SIZE_TEXT " in total.  "
+               "You should divide the DCP into shorter reels."
+               );
+}
+
+
+BOOST_AUTO_TEST_CASE (hint_closed_caption_xml_too_big)
+{
+       string const name = "hint_closed_caption_xml_too_big";
+
+       shared_ptr<Film> film = new_test_film2 (name);
+
+       FILE* ccap = fopen_boost (String::compose("build/test/%1.srt", name), "w");
+       BOOST_REQUIRE (ccap);
+       for (int i = 0; i < 2048; ++i) {
+               fprintf(ccap, "%d\n", i + 1);
+               int second = i * 2;
+               int minute = second % 60;
+               fprintf(ccap, "00:%02d:%02d,000 --> 00:%02d:%02d,000\n", minute, second, minute, second + 1);
+               fprintf(ccap, "Here are some closed captions.\n\n");
+       }
+       fclose (ccap);
+
+       shared_ptr<Content> content = content_factory("build/test/" + name + ".srt").front();
+       content->text.front()->set_type (TEXT_CLOSED_CAPTION);
+       film->examine_and_add_content (content);
+       BOOST_REQUIRE (!wait_for_jobs());
+       vector<string> hints = get_hints (film);
+
+       BOOST_REQUIRE_EQUAL (hints.size(), 1);
+       BOOST_CHECK_EQUAL (
+               hints[0],
+               "At least one of your closed caption files' XML part is larger than " MAX_CLOSED_CAPTION_XML_SIZE_TEXT ".  "
+               "You should divide the DCP into shorter reels."
+               );
+}
+
index 21e924380c66f7b938512c2862ab9c504ca31769..a5a3ed83bda1b38a91b4278d6b1169f71c0bf0df 100644 (file)
@@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE (write_frame_info_test)
 {
        shared_ptr<Film> film = new_test_film2 ("write_frame_info_test");
        dcpomatic::DCPTimePeriod const period (dcpomatic::DCPTime(0), dcpomatic::DCPTime(96000));
-       ReelWriter writer (film, period, shared_ptr<Job>(), 0, 1);
+       ReelWriter writer (film, period, shared_ptr<Job>(), 0, 1, false);
 
        /* Write the first one */