Supporters update.
[dcpomatic.git] / src / lib / reel_writer.cc
index aae2a6273f9835594ad12b3dac63277c2ffd1b4f..1b33cae852eedcbb1ed5330d9e20bf52bc77bd03 100644 (file)
@@ -27,6 +27,7 @@
 #include "dcpomatic_log.h"
 #include "digester.h"
 #include "film.h"
+#include "film_util.h"
 #include "image.h"
 #include "image_png.h"
 #include "job.h"
@@ -37,6 +38,7 @@
 #include <dcp/certificate_chain.h>
 #include <dcp/cpl.h>
 #include <dcp/dcp.h>
+#include <dcp/filesystem.h>
 #include <dcp/interop_subtitle_asset.h>
 #include <dcp/mono_picture_asset.h>
 #include <dcp/raw_convert.h>
@@ -135,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);
                }
 
 
@@ -178,8 +180,7 @@ ReelWriter::ReelWriter (
                _sound_asset = make_shared<dcp::SoundAsset> (
                        dcp::Fraction(film()->video_frame_rate(), 1),
                        film()->audio_frame_rate(),
-                       /* Always make 16-channel sound assets for SMPTE; libdcp will pad unused channels for us */
-                       standard == dcp::Standard::SMPTE ? MAX_DCP_AUDIO_CHANNELS : film()->audio_channels(),
+                       film()->audio_channels(),
                        lang ? *lang : dcp::LanguageTag("en-US"),
                        standard
                        );
@@ -192,18 +193,12 @@ ReelWriter::ReelWriter (
 
                DCPOMATIC_ASSERT (film()->directory());
 
-               auto mapped = film()->mapped_audio_channels();
                std::vector<dcp::Channel> extra_active_channels;
-               auto add_if_mapped = [mapped, &extra_active_channels](dcp::Channel channel) {
-                       if (std::find(mapped.begin(), mapped.end(), static_cast<int>(channel)) != mapped.end()) {
+               for (auto channel: std::vector<dcp::Channel>{dcp::Channel::HI, dcp::Channel::VI, dcp::Channel::BSL, dcp::Channel::BSR}) {
+                       if (channel_is_mapped(film(), channel)) {
                                extra_active_channels.push_back(channel);
                        }
-               };
-
-               add_if_mapped(dcp::Channel::HI);
-               add_if_mapped(dcp::Channel::VI);
-               add_if_mapped(dcp::Channel::BSL);
-               add_if_mapped(dcp::Channel::BSR);
+               }
 
                /* Write the sound asset into the film directory so that we leave the creation
                   of the DCP directory until the last minute.
@@ -295,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()) {
@@ -402,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 ();
@@ -417,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);
@@ -435,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
@@ -452,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
@@ -490,7 +485,7 @@ maybe_add_text (
        if (asset) {
                if (auto interop = dynamic_pointer_cast<dcp::InteropSubtitleAsset>(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> (
                                interop,
@@ -778,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<void (float)> set_progress)
+ReelWriter::calculate_digests(std::function<void (int64_t, int64_t)> set_progress)
 try
 {
+       vector<shared_ptr<const dcp::Asset>> 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.
@@ -823,6 +836,7 @@ shared_ptr<dcp::SubtitleAsset>
 ReelWriter::empty_text_asset (TextType type, optional<DCPTextTrack> track, bool with_dummy) const
 {
        shared_ptr<dcp::SubtitleAsset> asset;
+       optional<string> font;
 
        auto lang = film()->subtitle_languages();
        if (film()->interop()) {
@@ -857,7 +871,7 @@ ReelWriter::empty_text_asset (TextType type, optional<DCPTextTrack> track, bool
        if (with_dummy) {
                asset->add(
                        std::make_shared<dcp::SubtitleString>(
-                               optional<std::string>(),
+                               font,
                                false,
                                false,
                                false,
@@ -877,9 +891,16 @@ ReelWriter::empty_text_asset (TextType type, optional<DCPTextTrack> track, bool
                                dcp::Colour(),
                                dcp::Time(),
                                dcp::Time(),
-                               0
+                               0,
+                               std::vector<dcp::Ruby>()
                                )
                       );
+
+               if (!film()->interop()) {
+                       /* We must have a LoadFont since we have a Text */
+                       font = "font";
+                       asset->ensure_font(*font, _default_font);
+               }
        }
 
        return asset;