X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Freel_writer.cc;h=1d2ec557fd15907f86016b46578885246657e7cc;hb=28111007e2e6fd62f5810be780706ae1618bd33f;hp=e12628c74ad24ac6f6596b035150591fa1bfc289;hpb=46b4349fb5a19523e5105812bf79fd0e7df9c51f;p=dcpomatic.git diff --git a/src/lib/reel_writer.cc b/src/lib/reel_writer.cc index e12628c74..1d2ec557f 100644 --- a/src/lib/reel_writer.cc +++ b/src/lib/reel_writer.cc @@ -25,7 +25,7 @@ #include "log.h" #include "dcpomatic_log.h" #include "digester.h" -#include "font.h" +#include "font_data.h" #include "compose.hpp" #include "config.h" #include "audio_buffers.h" @@ -51,7 +51,6 @@ #include #include #include -#include #include "i18n.h" @@ -60,14 +59,16 @@ using std::string; using std::cout; using std::exception; using std::map; +using std::set; using std::vector; -using boost::shared_ptr; +using std::shared_ptr; +using std::make_shared; using boost::optional; -using boost::dynamic_pointer_cast; +using std::dynamic_pointer_cast; #if BOOST_VERSION >= 106100 using namespace boost::placeholders; #endif -using boost::weak_ptr; +using std::weak_ptr; using dcp::ArrayData; using dcp::Data; using dcp::raw_convert; @@ -92,9 +93,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 weak_film, DCPTimePeriod period, shared_ptr job, int reel_index, int reel_count + weak_ptr weak_film, DCPTimePeriod period, shared_ptr job, int reel_index, int reel_count, bool text_only ) : WeakConstFilm (weak_film) , _period (period) @@ -102,13 +107,14 @@ 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 output. We will hard-link it into the DCP later. */ - dcp::Standard const standard = film()->interop() ? dcp::INTEROP : dcp::SMPTE; + dcp::Standard const standard = film()->interop() ? dcp::Standard::INTEROP : dcp::Standard::SMPTE; boost::filesystem::path const asset = film()->internal_video_asset_dir() / film()->internal_video_asset_filename(_period); @@ -149,7 +155,7 @@ ReelWriter::ReelWriter ( _picture_asset->set_file (asset); _picture_asset_writer = _picture_asset->start_write (asset, _first_nonexistant_frame > 0); - } else { + } else if (!text_only) { /* We already have a complete picture asset that we can just re-use */ /* XXX: what about if the encryption key changes? */ if (film()->three_d()) { @@ -173,7 +179,7 @@ ReelWriter::ReelWriter ( DCPOMATIC_ASSERT (film()->directory()); vector active; - BOOST_FOREACH (int i, film()->mapped_audio_channels()) { + for (auto i: film()->mapped_audio_channels()) { active.push_back (static_cast(i)); } @@ -186,6 +192,8 @@ ReelWriter::ReelWriter ( film()->contains_atmos_content() ); } + + _default_font = dcp::ArrayData(default_font_file()); } /** @param frame reel-relative frame */ @@ -438,10 +446,12 @@ maybe_add_text ( int64_t picture_duration, shared_ptr reel, list const & refs, - list > const & fonts, + vector const & fonts, + dcp::ArrayData default_font, shared_ptr 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()); @@ -450,8 +460,8 @@ maybe_add_text ( if (asset) { /* Add the font to the subtitle content */ - BOOST_FOREACH (shared_ptr j, fonts) { - asset->add_font (j->id(), j->file().get_value_or(default_font_file())); + for (auto const& j: fonts) { + asset->add_font (j.id, j.data.get_value_or(default_font)); } if (dynamic_pointer_cast (asset)) { @@ -481,7 +491,7 @@ maybe_add_text ( ); } else { /* We don't have a subtitle asset of our own; hopefully we have one to reference */ - BOOST_FOREACH (ReferencedReelAsset j, refs) { + for (auto j: refs) { shared_ptr k = dynamic_pointer_cast (j.asset); if (k && j.period == period) { reel_asset = k; @@ -494,7 +504,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) @@ -526,7 +536,7 @@ ReelWriter::create_reel_picture (shared_ptr reel, list k = dynamic_pointer_cast (j.asset); if (k) { LOG_GENERAL ("candidate picture asset period is %1-%2", j.period.from.get(), j.period.to.get()); @@ -568,7 +578,7 @@ ReelWriter::create_reel_sound (shared_ptr reel, list k = dynamic_pointer_cast (j.asset); if (k) { LOG_GENERAL ("candidate sound asset period is %1-%2", j.period.from.get(), j.period.to.get()); @@ -608,27 +618,60 @@ void ReelWriter::create_reel_text ( shared_ptr reel, list const & refs, - list > const& fonts, + vector const& fonts, int64_t duration, - boost::filesystem::path output_dcp + boost::filesystem::path output_dcp, + bool ensure_subtitles, + set ensure_closed_captions ) const { shared_ptr subtitle = maybe_add_text ( - _subtitle_asset, duration, reel, refs, fonts, film(), _period, output_dcp + _subtitle_asset, duration, reel, refs, fonts, _default_font, film(), _period, output_dcp, _text_only ); - if (subtitle && !film()->subtitle_languages().empty()) { - subtitle->set_language (film()->subtitle_languages().front()); + + if (subtitle) { + /* We have a subtitle asset that we either made or are referencing */ + if (!film()->subtitle_languages().empty()) { + subtitle->set_language (film()->subtitle_languages().front()); + } + } else if (ensure_subtitles) { + /* We had no subtitle asset, but we've been asked to make sure there is one */ + subtitle = maybe_add_text( + empty_text_asset(TEXT_OPEN_SUBTITLE, optional()), + duration, + reel, + refs, + fonts, + _default_font, + film(), + _period, + output_dcp, + _text_only + ); } for (map >::const_iterator i = _closed_caption_assets.begin(); i != _closed_caption_assets.end(); ++i) { shared_ptr a = maybe_add_text ( - i->second, duration, reel, refs, fonts, film(), _period, output_dcp + i->second, duration, reel, refs, fonts, _default_font, film(), _period, output_dcp, _text_only ); - if (a) { - a->set_annotation_text (i->first.name); - if (!i->first.language.empty()) { - a->set_language (dcp::LanguageTag(i->first.language)); - } + DCPOMATIC_ASSERT (a); + a->set_annotation_text (i->first.name); + if (!i->first.language.empty()) { + a->set_language (dcp::LanguageTag(i->first.language)); + } + + ensure_closed_captions.erase (i->first); + } + + /* Make empty tracks for anything we've been asked to ensure but that we haven't added */ + for (auto i: ensure_closed_captions) { + shared_ptr a = maybe_add_text ( + empty_text_asset(TEXT_CLOSED_CAPTION, i), duration, reel, refs, fonts, _default_font, film(), _period, output_dcp, _text_only + ); + DCPOMATIC_ASSERT (a); + a->set_annotation_text (i.name); + if (!i.language.empty()) { + a->set_language (dcp::LanguageTag(i.language)); } } } @@ -648,7 +691,7 @@ ReelWriter::create_reel_markers (shared_ptr reel) const } if (!reel_markers.empty ()) { - shared_ptr ma (new dcp::ReelMarkersAsset(dcp::Fraction(film()->video_frame_rate(), 1), 0)); + auto ma = make_shared(dcp::Fraction(film()->video_frame_rate(), 1), reel->duration(), 0); for (map::const_iterator i = reel_markers.begin(); i != reel_markers.end(); ++i) { int h, m, s, f; DCPTime relative = i->second - _period.from; @@ -660,16 +703,35 @@ ReelWriter::create_reel_markers (shared_ptr reel) const } +/** @param ensure_subtitles true to make sure the reel has a subtitle asset. + * @param ensure_closed_captions make sure the reel has these closed caption tracks. + */ shared_ptr -ReelWriter::create_reel (list const & refs, list > const & fonts, boost::filesystem::path output_dcp) +ReelWriter::create_reel ( + list const & refs, + vector const & fonts, + boost::filesystem::path output_dcp, + bool ensure_subtitles, + set ensure_closed_captions + ) { LOG_GENERAL ("create_reel for %1-%2; %3 of %4", _period.from.get(), _period.to.get(), _reel_index, _reel_count); shared_ptr reel (new dcp::Reel()); - shared_ptr 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 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, ensure_subtitles, ensure_closed_captions); if (_atmos_asset) { reel->add (shared_ptr(new dcp::ReelAtmosAsset(_atmos_asset, 0))); @@ -712,6 +774,46 @@ ReelWriter::write (shared_ptr audio) _sound_asset_writer->write (audio->data(), audio->frames()); } + +shared_ptr +ReelWriter::empty_text_asset (TextType type, optional track) const +{ + shared_ptr asset; + + vector lang = film()->subtitle_languages(); + if (film()->interop()) { + shared_ptr s (new dcp::InteropSubtitleAsset ()); + s->set_movie_title (film()->name()); + if (type == TEXT_OPEN_SUBTITLE) { + s->set_language (lang.empty() ? "Unknown" : lang.front().to_string()); + } else if (!track->language.empty()) { + s->set_language (track->language); + } + s->set_reel_number (raw_convert (_reel_index + 1)); + asset = s; + } else { + shared_ptr s (new dcp::SMPTESubtitleAsset ()); + s->set_content_title_text (film()->name()); + s->set_metadata (mxf_metadata()); + if (type == TEXT_OPEN_SUBTITLE && !lang.empty()) { + s->set_language (lang.front()); + } else if (track && !track->language.empty()) { + s->set_language (dcp::LanguageTag(track->language)); + } + s->set_edit_rate (dcp::Fraction (film()->video_frame_rate(), 1)); + s->set_reel_number (_reel_index + 1); + s->set_time_code_rate (film()->video_frame_rate()); + s->set_start_time (dcp::Time ()); + if (film()->encrypted()) { + s->set_key (film()->key()); + } + asset = s; + } + + return asset; +} + + void ReelWriter::write (PlayerText subs, TextType type, optional track, DCPTimePeriod period) { @@ -730,35 +832,7 @@ ReelWriter::write (PlayerText subs, TextType type, optional track, } if (!asset) { - vector lang = film()->subtitle_languages(); - if (film()->interop()) { - shared_ptr s (new dcp::InteropSubtitleAsset ()); - s->set_movie_title (film()->name()); - if (type == TEXT_OPEN_SUBTITLE) { - s->set_language (lang.empty() ? "Unknown" : lang.front().to_string()); - } else if (!track->language.empty()) { - s->set_language (track->language); - } - s->set_reel_number (raw_convert (_reel_index + 1)); - asset = s; - } else { - shared_ptr s (new dcp::SMPTESubtitleAsset ()); - s->set_content_title_text (film()->name()); - s->set_metadata (mxf_metadata()); - if (type == TEXT_OPEN_SUBTITLE && !lang.empty()) { - s->set_language (lang.front()); - } else if (track && !track->language.empty()) { - s->set_language (dcp::LanguageTag(track->language)); - } - s->set_edit_rate (dcp::Fraction (film()->video_frame_rate(), 1)); - s->set_reel_number (_reel_index + 1); - s->set_time_code_rate (film()->video_frame_rate()); - s->set_start_time (dcp::Time ()); - if (film()->encrypted()) { - s->set_key (film()->key()); - } - asset = s; - } + asset = empty_text_asset (type, track); } switch (type) { @@ -773,21 +847,21 @@ ReelWriter::write (PlayerText subs, TextType type, optional track, DCPOMATIC_ASSERT (false); } - BOOST_FOREACH (StringText i, subs.string) { + for (auto i: subs.string) { /* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */ i.set_in (i.in() - dcp::Time (_period.from.seconds(), i.in().tcr)); i.set_out (i.out() - dcp::Time (_period.from.seconds(), i.out().tcr)); asset->add (shared_ptr(new dcp::SubtitleString(i))); } - BOOST_FOREACH (BitmapText i, subs.bitmap) { + for (auto i: subs.bitmap) { asset->add ( shared_ptr( new dcp::SubtitleImage( i.image->as_png(), dcp::Time(period.from.seconds() - _period.from.seconds(), film()->video_frame_rate()), dcp::Time(period.to.seconds() - _period.from.seconds(), film()->video_frame_rate()), - i.rectangle.x, dcp::HALIGN_LEFT, i.rectangle.y, dcp::VALIGN_TOP, + i.rectangle.x, dcp::HAlign::LEFT, i.rectangle.y, dcp::VAlign::TOP, dcp::Time(), dcp::Time() ) )