X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Freel_writer.cc;h=a247349d5a08c0f75e89dfcff5ec7ed05e5a3341;hp=f9e29a16f4c15537358f68398b09c8f11b8fdea7;hb=HEAD;hpb=37b381e63e202c4b6cb86a22fd9f5c256f1671f2 diff --git a/src/lib/reel_writer.cc b/src/lib/reel_writer.cc index f9e29a16f..1b33cae85 100644 --- a/src/lib/reel_writer.cc +++ b/src/lib/reel_writer.cc @@ -22,10 +22,12 @@ #include "audio_buffers.h" #include "compose.hpp" #include "config.h" +#include "constants.h" #include "cross.h" #include "dcpomatic_log.h" #include "digester.h" #include "film.h" +#include "film_util.h" #include "image.h" #include "image_png.h" #include "job.h" @@ -36,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -114,6 +117,7 @@ ReelWriter::ReelWriter ( , _content_summary (film()->content_summary(period)) , _job (job) , _text_only (text_only) + , _font_metrics(film()->frame_size().height) { /* Create or find our picture asset in a subdirectory, named according to those film's parameters which affect the video @@ -133,15 +137,15 @@ ReelWriter::ReelWriter ( to change its contents (if only by changing the IDs); see #1126. */ - if (boost::filesystem::exists(asset) && boost::filesystem::hard_link_count(asset) > 1) { + if (dcp::filesystem::exists(asset) && dcp::filesystem::hard_link_count(asset) > 1) { if (job) { job->sub (_("Copying old video file")); copy_in_bits (asset, asset.string() + ".tmp", bind(&Job::set_progress, job.get(), _1, false)); } else { - boost::filesystem::copy_file (asset, asset.string() + ".tmp"); + dcp::filesystem::copy_file(asset, asset.string() + ".tmp"); } - boost::filesystem::remove (asset); - boost::filesystem::rename (asset.string() + ".tmp", asset); + dcp::filesystem::remove(asset); + dcp::filesystem::rename(asset.string() + ".tmp", asset); } @@ -160,7 +164,7 @@ ReelWriter::ReelWriter ( } _picture_asset->set_file (asset); - _picture_asset_writer = _picture_asset->start_write (asset, _first_nonexistent_frame > 0); + _picture_asset_writer = _picture_asset->start_write(asset, _first_nonexistent_frame > 0 ? dcp::PictureAsset::Behaviour::OVERWRITE_EXISTING : dcp::PictureAsset::Behaviour::MAKE_NEW); } 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? */ @@ -189,12 +193,21 @@ ReelWriter::ReelWriter ( DCPOMATIC_ASSERT (film()->directory()); + std::vector extra_active_channels; + for (auto channel: std::vector{dcp::Channel::HI, dcp::Channel::VI, dcp::Channel::BSL, dcp::Channel::BSR}) { + if (channel_is_mapped(film(), channel)) { + extra_active_channels.push_back(channel); + } + } + /* Write the sound asset into the film directory so that we leave the creation of the DCP directory until the last minute. */ _sound_asset_writer = _sound_asset->start_write ( film()->directory().get() / audio_asset_filename (_sound_asset, _reel_index, _reel_count, _content_summary), - film()->contains_atmos_content() + extra_active_channels, + film()->contains_atmos_content() ? dcp::SoundAsset::AtmosSync::ENABLED : dcp::SoundAsset::AtmosSync::DISABLED, + film()->limit_to_smpte_bv20() ? dcp::SoundAsset::MCASubDescriptors::DISABLED : dcp::SoundAsset::MCASubDescriptors::ENABLED ); } @@ -277,8 +290,8 @@ ReelWriter::check_existing_picture_asset (boost::filesystem::path asset) } /* Offset of the last dcp::FrameInfo in the info file */ - int const n = (boost::filesystem::file_size(info_file->get().path()) / _info_size) - 1; - LOG_GENERAL ("The last FI is %1; info file is %2, info size %3", n, boost::filesystem::file_size(info_file->get().path()), _info_size); + int const n = (dcp::filesystem::file_size(info_file->get().path()) / _info_size) - 1; + LOG_GENERAL ("The last FI is %1; info file is %2, info size %3", n, dcp::filesystem::file_size(info_file->get().path()), _info_size); Frame first_nonexistent_frame; if (film()->three_d()) { @@ -384,9 +397,9 @@ ReelWriter::finish (boost::filesystem::path output_dcp) changing any video. */ boost::system::error_code ec; - boost::filesystem::remove (video_to, ec); + dcp::filesystem::remove(video_to, ec); - boost::filesystem::create_hard_link (video_from, video_to, ec); + dcp::filesystem::create_hard_link(video_from, video_to, ec); if (ec) { LOG_WARNING("Hard-link failed (%1); copying instead", error_details(ec)); auto job = _job.lock (); @@ -399,7 +412,7 @@ ReelWriter::finish (boost::filesystem::path output_dcp) throw FileError (e.what(), video_from); } } else { - boost::filesystem::copy_file (video_from, video_to, ec); + dcp::filesystem::copy_file(video_from, video_to, ec); if (ec) { LOG_ERROR("Failed to copy video file from %1 to %2 (%3)", video_from.string(), video_to.string(), error_details(ec)); throw FileError (ec.message(), video_from); @@ -417,7 +430,7 @@ ReelWriter::finish (boost::filesystem::path output_dcp) audio_to /= aaf; boost::system::error_code ec; - boost::filesystem::rename (film()->file(aaf), audio_to, ec); + dcp::filesystem::rename(film()->file(aaf), audio_to, ec); if (ec) { throw FileError ( String::compose(_("could not move audio asset into the DCP (%1)"), error_details(ec)), aaf @@ -434,7 +447,7 @@ ReelWriter::finish (boost::filesystem::path output_dcp) atmos_to /= aaf; boost::system::error_code ec; - boost::filesystem::rename (film()->file(aaf), atmos_to, ec); + dcp::filesystem::rename(film()->file(aaf), atmos_to, ec); if (ec) { throw FileError ( String::compose(_("could not move atmos asset into the DCP (%1)"), error_details(ec)), aaf @@ -459,9 +472,6 @@ maybe_add_text ( int reel_count, optional content_summary, list const & refs, - FontIdMap const& fonts, - shared_ptr chosen_interop_font, - dcp::ArrayData default_font, shared_ptr film, DCPTimePeriod period, boost::filesystem::path output_dcp, @@ -473,22 +483,9 @@ maybe_add_text ( shared_ptr reel_asset; if (asset) { - if (film->interop()) { - if (chosen_interop_font) { - /* We only add one font, as Interop will ignore subsequent ones (and some validators will - * complain if they are even present) - */ - asset->add_font(fonts.get(chosen_interop_font), chosen_interop_font->data().get_value_or(default_font)); - } - } else { - for (auto const& font: fonts.map()) { - asset->add_font(font.second, font.first->data().get_value_or(default_font)); - } - } - if (auto interop = dynamic_pointer_cast(asset)) { auto directory = output_dcp / interop->id (); - boost::filesystem::create_directories (directory); + dcp::filesystem::create_directories(directory); interop->write (directory / subtitle_asset_filename(asset, reel_index, reel_count, content_summary, ".xml")); reel_asset = make_shared ( interop, @@ -643,8 +640,6 @@ void ReelWriter::create_reel_text ( shared_ptr reel, list const & refs, - FontIdMap const& fonts, - shared_ptr chosen_interop_font, int64_t duration, boost::filesystem::path output_dcp, bool ensure_subtitles, @@ -652,15 +647,10 @@ ReelWriter::create_reel_text ( ) const { auto subtitle = maybe_add_text ( - _subtitle_asset, duration, reel, _reel_index, _reel_count, _content_summary, refs, fonts, chosen_interop_font, _default_font, film(), _period, output_dcp, _text_only + _subtitle_asset, duration, reel, _reel_index, _reel_count, _content_summary, refs, film(), _period, output_dcp, _text_only ); - if (subtitle) { - /* We have a subtitle asset that we either made or are referencing */ - if (auto main_language = film()->subtitle_languages().first) { - subtitle->set_language (*main_language); - } - } else if (ensure_subtitles) { + if (!subtitle && 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(TextType::OPEN_SUBTITLE, optional(), true), @@ -670,9 +660,6 @@ ReelWriter::create_reel_text ( _reel_count, _content_summary, refs, - fonts, - chosen_interop_font, - _default_font, film(), _period, output_dcp, @@ -680,9 +667,16 @@ ReelWriter::create_reel_text ( ); } + if (subtitle) { + /* We have a subtitle asset that we either made or are referencing */ + if (auto main_language = film()->subtitle_languages().first) { + subtitle->set_language (*main_language); + } + } + for (auto const& i: _closed_caption_assets) { auto a = maybe_add_text ( - i.second, duration, reel, _reel_index, _reel_count, _content_summary, refs, fonts, chosen_interop_font, _default_font, film(), _period, output_dcp, _text_only + i.second, duration, reel, _reel_index, _reel_count, _content_summary, refs, film(), _period, output_dcp, _text_only ); DCPOMATIC_ASSERT (a); a->set_annotation_text (i.first.name); @@ -703,9 +697,6 @@ ReelWriter::create_reel_text ( _reel_count, _content_summary, refs, - fonts, - chosen_interop_font, - _default_font, film(), _period, output_dcp, @@ -750,8 +741,6 @@ ReelWriter::create_reel_markers (shared_ptr reel) const shared_ptr ReelWriter::create_reel ( list const & refs, - FontIdMap const & fonts, - shared_ptr chosen_interop_font, boost::filesystem::path output_dcp, bool ensure_subtitles, set ensure_closed_captions @@ -770,10 +759,12 @@ ReelWriter::create_reel ( auto reel_picture_asset = create_reel_picture (reel, refs); duration = reel_picture_asset->actual_duration (); create_reel_sound (reel, refs); - create_reel_markers (reel); + if (!film()->interop()) { + create_reel_markers(reel); + } } - create_reel_text (reel, refs, fonts, chosen_interop_font, duration, output_dcp, ensure_subtitles, ensure_closed_captions); + create_reel_text(reel, refs, duration, output_dcp, ensure_subtitles, ensure_closed_captions); if (_atmos_asset) { reel->add (make_shared(_atmos_asset, 0)); @@ -782,21 +773,39 @@ ReelWriter::create_reel ( return reel; } + +/** @param set_progress Method to call with progress; first parameter is the number of bytes + * done, second parameter is the number of bytes in total. + */ void -ReelWriter::calculate_digests (std::function set_progress) +ReelWriter::calculate_digests(std::function set_progress) try { + vector> assets; + if (_picture_asset) { - _picture_asset->hash (set_progress); + assets.push_back(_picture_asset); } - if (_sound_asset) { - _sound_asset->hash (set_progress); + assets.push_back(_sound_asset); } - if (_atmos_asset) { - _atmos_asset->hash (set_progress); + assets.push_back(_atmos_asset); } + + int64_t total_size = 0; + for (auto asset: assets) { + total_size += asset->file() ? boost::filesystem::file_size(*asset->file()) : 0; + } + + int64_t total_done = 0; + for (auto asset: assets) { + asset->hash([&total_done, total_size, set_progress](int64_t done, int64_t) { + set_progress(total_done + done, total_size); + }); + total_done += asset->file() ? boost::filesystem::file_size(*asset->file()) : 0; + } + } catch (boost::thread_interrupted) { /* set_progress contains an interruption_point, so any of these methods * may throw thread_interrupted, at which point we just give up. @@ -819,7 +828,7 @@ ReelWriter::write (shared_ptr audio) } DCPOMATIC_ASSERT (audio); - _sound_asset_writer->write (audio->data(), audio->frames()); + _sound_asset_writer->write(audio->data(), audio->channels(), audio->frames()); } @@ -827,6 +836,7 @@ shared_ptr ReelWriter::empty_text_asset (TextType type, optional track, bool with_dummy) const { shared_ptr asset; + optional font; auto lang = film()->subtitle_languages(); if (film()->interop()) { @@ -855,42 +865,78 @@ ReelWriter::empty_text_asset (TextType type, optional track, bool if (film()->encrypted()) { s->set_key (film()->key()); } - if (with_dummy) { - s->add ( - std::make_shared( - optional(), - false, - false, - false, - dcp::Colour(), - 42, - 1.0, - dcp::Time(0, 0, 0, 0, 24), - dcp::Time(0, 0, 1, 0, 24), - 0.5, - dcp::HAlign::CENTER, - 0.5, - dcp::VAlign::CENTER, - 0, - dcp::Direction::LTR, - " ", - dcp::Effect::NONE, - dcp::Colour(), - dcp::Time(), - dcp::Time(), - 0 - ) - ); - } asset = s; } + if (with_dummy) { + asset->add( + std::make_shared( + font, + false, + false, + false, + dcp::Colour(), + 42, + 1.0, + dcp::Time(0, 0, 0, 0, 24), + dcp::Time(0, 0, 1, 0, 24), + 0.5, + dcp::HAlign::CENTER, + 0.5, + dcp::VAlign::CENTER, + 0, + dcp::Direction::LTR, + " ", + dcp::Effect::NONE, + dcp::Colour(), + dcp::Time(), + dcp::Time(), + 0, + std::vector() + ) + ); + + if (!film()->interop()) { + /* We must have a LoadFont since we have a Text */ + font = "font"; + asset->ensure_font(*font, _default_font); + } + } + return asset; } +float +ReelWriter::convert_vertical_position(StringText const& subtitle, dcp::SubtitleStandard to) const +{ + if (dcp::uses_baseline(subtitle.valign_standard) == dcp::uses_baseline(to)) { + /* The from and to standards use the same alignment reference */ + return subtitle.v_position(); + } + + auto const baseline_to_bottom = _font_metrics.baseline_to_bottom(subtitle); + auto const height = _font_metrics.height(subtitle); + + float correction = 0; + switch (subtitle.v_align()) { + case dcp::VAlign::TOP: + correction = height - baseline_to_bottom; + break; + case dcp::VAlign::CENTER: + correction = (height / 2) - baseline_to_bottom; + break; + case dcp::VAlign::BOTTOM: + correction = baseline_to_bottom; + break; + } + + return subtitle.v_position() + (dcp::uses_bounding_box(subtitle.valign_standard) ? correction : -correction); +} + + void -ReelWriter::write (PlayerText subs, TextType type, optional track, DCPTimePeriod period, FontIdMap const& fonts) +ReelWriter::write(PlayerText subs, TextType type, optional track, DCPTimePeriod period, FontIdMap const& fonts, shared_ptr chosen_interop_font) { shared_ptr asset; @@ -928,11 +974,20 @@ ReelWriter::write (PlayerText subs, TextType type, optional track, for (auto i: subs.string) { i.set_in (dcp::Time(period.from.seconds() - _period.from.seconds(), tcr)); i.set_out (dcp::Time(period.to.seconds() - _period.from.seconds(), tcr)); + i.set_v_position(convert_vertical_position(i, film()->interop() ? dcp::SubtitleStandard::INTEROP : dcp::SubtitleStandard::SMPTE_2014)); auto sub = make_shared(i); - if (type == TextType::OPEN_SUBTITLE) { - sub->set_font(fonts.get(i.font)); - } + /* i.font is a shared_ptr which uniquely identifies the font we want, + * though if we are Interop we can only have one font, so we'll use the chosen + * one instead. + */ + auto font = film()->interop() ? chosen_interop_font : i.font; + /* We can get the corresponding ID from fonts */ + auto const font_id_to_use = fonts.get(font); + /* Give this subtitle the correct font ID */ + sub->set_font(font_id_to_use); asset->add(sub); + /* Make sure the asset LoadFonts the font we just asked for */ + asset->ensure_font(font_id_to_use, font->data().get_value_or(_default_font)); } for (auto i: subs.bitmap) {