More enum class additions.
[dcpomatic.git] / src / lib / writer.cc
index ca0a570e727dfefbd8a4ca56bbf963272ea680ca..e47fa973b8841b21c5cfdebdba8784f770b51123 100644 (file)
 #include "cross.h"
 #include "audio_buffers.h"
 #include "version.h"
-#include "font.h"
+#include "font_data.h"
 #include "util.h"
 #include "reel_writer.h"
 #include "text_content.h"
 #include <dcp/cpl.h>
 #include <dcp/locale_convert.h>
-#include <dcp/reel_mxf.h>
-#include <boost/foreach.hpp>
+#include <dcp/reel_file_asset.h>
 #include <fstream>
 #include <cerrno>
 #include <iostream>
@@ -59,9 +58,9 @@ using std::map;
 using std::min;
 using std::max;
 using std::vector;
-using boost::shared_ptr;
-using boost::weak_ptr;
-using boost::dynamic_pointer_cast;
+using std::shared_ptr;
+using std::weak_ptr;
+using std::dynamic_pointer_cast;
 using boost::optional;
 #if BOOST_VERSION >= 106100
 using namespace boost::placeholders;
@@ -70,7 +69,19 @@ using dcp::Data;
 using dcp::ArrayData;
 using namespace dcpomatic;
 
-Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j)
+
+static
+void
+ignore_progress (float)
+{
+
+}
+
+
+/** @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)
@@ -82,14 +93,15 @@ 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)
+       , _have_subtitles (false)
 {
        shared_ptr<Job> job = _job.lock ();
-       DCPOMATIC_ASSERT (job);
 
        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()));
+       auto const reels = film()->reels();
+       for (auto p: reels) {
+               _reels.push_back (ReelWriter(weak_film, p, job, reel_index++, reels.size(), text_only));
        }
 
        _last_written.resize (reels.size());
@@ -99,7 +111,7 @@ Writer::Writer (weak_ptr<const Film> weak_film, weak_ptr<Job> j)
        */
        _audio_reel = _reels.begin ();
        _subtitle_reel = _reels.begin ();
-       BOOST_FOREACH (DCPTextTrack i, film()->closed_caption_tracks()) {
+       for (auto i: film()->closed_caption_tracks()) {
                _caption_reels[i] = _reels.begin ();
        }
        _atmos_reel = _reels.begin ();
@@ -114,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.
@@ -149,12 +165,12 @@ Writer::write (shared_ptr<const Data> encoded, Frame frame, Eyes eyes)
        qi.reel = video_reel (frame);
        qi.frame = frame - _reels[qi.reel].start ();
 
-       if (film()->three_d() && eyes == EYES_BOTH) {
+       if (film()->three_d() && eyes == Eyes::BOTH) {
                /* 2D material in a 3D DCP; fake the 3D */
-               qi.eyes = EYES_LEFT;
+               qi.eyes = Eyes::LEFT;
                _queue.push_back (qi);
                ++_queued_full_in_memory;
-               qi.eyes = EYES_RIGHT;
+               qi.eyes = Eyes::RIGHT;
                _queue.push_back (qi);
                ++_queued_full_in_memory;
        } else {
@@ -194,10 +210,10 @@ Writer::repeat (Frame frame, Eyes eyes)
        qi.type = QueueItem::REPEAT;
        qi.reel = video_reel (frame);
        qi.frame = frame - _reels[qi.reel].start ();
-       if (film()->three_d() && eyes == EYES_BOTH) {
-               qi.eyes = EYES_LEFT;
+       if (film()->three_d() && eyes == Eyes::BOTH) {
+               qi.eyes = Eyes::LEFT;
                _queue.push_back (qi);
-               qi.eyes = EYES_RIGHT;
+               qi.eyes = Eyes::RIGHT;
                _queue.push_back (qi);
        } else {
                qi.eyes = eyes;
@@ -234,10 +250,10 @@ Writer::fake_write (Frame frame, Eyes eyes)
 
        qi.reel = reel;
        qi.frame = frame_in_reel;
-       if (film()->three_d() && eyes == EYES_BOTH) {
-               qi.eyes = EYES_LEFT;
+       if (film()->three_d() && eyes == Eyes::BOTH) {
+               qi.eyes = Eyes::LEFT;
                _queue.push_back (qi);
-               qi.eyes = EYES_RIGHT;
+               qi.eyes = Eyes::RIGHT;
                _queue.push_back (qi);
        } else {
                qi.eyes = eyes;
@@ -339,18 +355,18 @@ Writer::have_sequenced_image_at_queue_head ()
 bool
 Writer::LastWritten::next (QueueItem qi) const
 {
-       if (qi.eyes == EYES_BOTH) {
+       if (qi.eyes == Eyes::BOTH) {
                /* 2D */
                return qi.frame == (_frame + 1);
        }
 
        /* 3D */
 
-       if (_eyes == EYES_LEFT && qi.frame == _frame && qi.eyes == EYES_RIGHT) {
+       if (_eyes == Eyes::LEFT && qi.frame == _frame && qi.eyes == Eyes::RIGHT) {
                return true;
        }
 
-       if (_eyes == EYES_RIGHT && qi.frame == (_frame + 1) && qi.eyes == EYES_LEFT) {
+       if (_eyes == Eyes::RIGHT && qi.frame == (_frame + 1) && qi.eyes == Eyes::LEFT) {
                return true;
        }
 
@@ -396,7 +412,7 @@ try
                        /* (Hopefully temporarily) log anything that was not written */
                        if (!_queue.empty() && !have_sequenced_image_at_queue_head()) {
                                LOG_WARNING (N_("Finishing writer with a left-over queue of %1:"), _queue.size());
-                               BOOST_FOREACH (QueueItem const& i, _queue) {
+                               for (auto const& i: _queue) {
                                        if (i.type == QueueItem::FULL) {
                                                LOG_WARNING (N_("- type FULL, frame %1, eyes %2"), i.frame, (int) i.eyes);
                                        } else {
@@ -508,26 +524,25 @@ Writer::terminate_thread (bool can_throw)
        }
 }
 
+
+/** @param output_dcp Path to DCP folder to write */
 void
-Writer::finish ()
+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) {
-               i.finish ();
+       for (auto& i: _reels) {
+               i.finish (output_dcp);
        }
 
        LOG_GENERAL_NC ("Writing XML");
 
-       dcp::DCP dcp (film()->dir(film()->dcp_name()));
+       dcp::DCP dcp (output_dcp);
 
        shared_ptr<dcp::CPL> cpl (
                new dcp::CPL (
@@ -541,7 +556,9 @@ Writer::finish ()
        /* Calculate digests for each reel in parallel */
 
        shared_ptr<Job> job = _job.lock ();
-       job->sub (_("Computing digests"));
+       if (job) {
+               job->sub (_("Computing digests"));
+       }
 
        boost::asio::io_service service;
        boost::thread_group pool;
@@ -554,8 +571,14 @@ Writer::finish ()
                pool.create_thread (boost::bind (&boost::asio::io_service::run, &service));
        }
 
-       boost::function<void (float)> set_progress = boost::bind (&Writer::set_digest_progress, this, job.get(), _1);
-       BOOST_FOREACH (ReelWriter& i, _reels) {
+       boost::function<void (float)> set_progress;
+       if (job) {
+               set_progress = boost::bind (&Writer::set_digest_progress, this, job.get(), _1);
+       } else {
+               set_progress = &ignore_progress;
+       }
+
+       for (auto& i: _reels) {
                service.post (boost::bind (&ReelWriter::calculate_digests, &i, set_progress));
        }
        service.post (boost::bind (&Writer::calculate_referenced_digests, this, set_progress));
@@ -566,8 +589,8 @@ Writer::finish ()
 
        /* Add reels */
 
-       BOOST_FOREACH (ReelWriter& i, _reels) {
-               cpl->add (i.create_reel (_reel_assets, _fonts));
+       for (auto& i: _reels) {
+               cpl->add (i.create_reel(_reel_assets, _fonts, output_dcp, _have_subtitles, _have_closed_captions));
        }
 
        /* Add metadata */
@@ -585,7 +608,7 @@ Writer::finish ()
        cpl->set_ratings (film()->ratings());
 
        vector<dcp::ContentVersion> cv;
-       BOOST_FOREACH (string i, film()->content_versions()) {
+       for (auto i: film()->content_versions()) {
                cv.push_back (dcp::ContentVersion(i));
        }
        cpl->set_content_versions (cv);
@@ -600,15 +623,15 @@ Writer::finish ()
        cpl->set_facility (film()->facility());
        cpl->set_luminance (film()->luminance());
 
-       list<int> ac = film()->mapped_audio_channels();
+       auto ac = film()->mapped_audio_channels();
        dcp::MCASoundField field = (
-               find(ac.begin(), ac.end(), static_cast<int>(dcp::BSL)) != ac.end() ||
-               find(ac.begin(), ac.end(), static_cast<int>(dcp::BSR)) != ac.end()
-               ) ? dcp::SEVEN_POINT_ONE : dcp::FIVE_POINT_ONE;
+               find(ac.begin(), ac.end(), static_cast<int>(dcp::Channel::BSL)) != ac.end() ||
+               find(ac.begin(), ac.end(), static_cast<int>(dcp::Channel::BSR)) != ac.end()
+               ) ? dcp::MCASoundField::SEVEN_POINT_ONE : dcp::MCASoundField::FIVE_POINT_ONE;
 
        dcp::MainSoundConfiguration msc (field, film()->audio_channels());
-       BOOST_FOREACH (int i, ac) {
-               if (i < film()->audio_channels()) {
+       for (auto i: ac) {
+               if (static_cast<int>(i) < film()->audio_channels()) {
                        msc.set_mapping (i, static_cast<dcp::Channel>(i));
                }
        }
@@ -632,11 +655,11 @@ Writer::finish ()
        }
 
        dcp.write_xml (
-               film()->interop() ? dcp::INTEROP : dcp::SMPTE,
+               film()->interop() ? dcp::Standard::INTEROP : dcp::Standard::SMPTE,
                issuer,
                creator,
                dcp::LocalTime().as_string(),
-               String::compose("Created by libdcp %1", dcp::version),
+               film()->dcp_name(),
                signer,
                Config::instance()->dcp_metadata_filename_format()
                );
@@ -645,11 +668,11 @@ Writer::finish ()
                N_("Wrote %1 FULL, %2 FAKE, %3 REPEAT, %4 pushed to disk"), _full_written, _fake_written, _repeat_written, _pushed_to_disk
                );
 
-       write_cover_sheet ();
+       write_cover_sheet (output_dcp);
 }
 
 void
-Writer::write_cover_sheet ()
+Writer::write_cover_sheet (boost::filesystem::path output_dcp)
 {
        boost::filesystem::path const cover = film()->file("COVER_SHEET.txt");
        FILE* f = fopen_boost (cover, "w");
@@ -672,7 +695,7 @@ Writer::write_cover_sheet ()
 
        boost::uintmax_t size = 0;
        for (
-               boost::filesystem::recursive_directory_iterator i = boost::filesystem::recursive_directory_iterator(film()->dir(film()->dcp_name()));
+               boost::filesystem::recursive_directory_iterator i = boost::filesystem::recursive_directory_iterator(output_dcp);
                i != boost::filesystem::recursive_directory_iterator();
                ++i) {
                if (boost::filesystem::is_regular_file (i->path ())) {
@@ -737,20 +760,22 @@ Writer::can_fake_write (Frame frame) const
        return (frame != 0 && frame < reel.first_nonexistant_frame());
 }
 
-/** @param track Closed caption track if type == TEXT_CLOSED_CAPTION */
+/** @param track Closed caption track if type == TextType::CLOSED_CAPTION */
 void
 Writer::write (PlayerText text, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
 {
-       vector<ReelWriter>::iterator* reel = 0;
+       vector<ReelWriter>::iterator* reel = nullptr;
 
        switch (type) {
-       case TEXT_OPEN_SUBTITLE:
+       case TextType::OPEN_SUBTITLE:
                reel = &_subtitle_reel;
+               _have_subtitles = true;
                break;
-       case TEXT_CLOSED_CAPTION:
+       case TextType::CLOSED_CAPTION:
                DCPOMATIC_ASSERT (track);
                DCPOMATIC_ASSERT (_caption_reels.find(*track) != _caption_reels.end());
                reel = &_caption_reels[*track];
+               _have_closed_captions.insert (*track);
                break;
        default:
                DCPOMATIC_ASSERT (false);
@@ -766,14 +791,14 @@ Writer::write (PlayerText text, TextType type, optional<DCPTextTrack> track, DCP
 }
 
 void
-Writer::write (list<shared_ptr<Font> > fonts)
+Writer::write (vector<FontData> fonts)
 {
        /* Just keep a list of unique fonts and we'll deal with them in ::finish */
 
-       BOOST_FOREACH (shared_ptr<Font> i, fonts) {
+       for (auto const& i: fonts) {
                bool got = false;
-               BOOST_FOREACH (shared_ptr<Font> j, _fonts) {
-                       if (*i == *j) {
+               for (auto& j: _fonts) {
+                       if (i == j) {
                                got = true;
                        }
                }
@@ -821,7 +846,7 @@ Writer::write (ReferencedReelAsset asset)
 size_t
 Writer::video_reel (int frame) const
 {
-       DCPTime t = DCPTime::from_frames (frame, film()->video_frame_rate());
+       auto t = DCPTime::from_frames (frame, film()->video_frame_rate());
        size_t i = 0;
        while (i < _reels.size() && !_reels[i].period().contains (t)) {
                ++i;
@@ -838,8 +863,8 @@ Writer::set_digest_progress (Job* job, float progress)
 
        _digest_progresses[boost::this_thread::get_id()] = progress;
        float min_progress = FLT_MAX;
-       for (map<boost::thread::id, float>::const_iterator i = _digest_progresses.begin(); i != _digest_progresses.end(); ++i) {
-               min_progress = min (min_progress, i->second);
+       for (auto const& i: _digest_progresses) {
+               min_progress = min (min_progress, i.second);
        }
 
        job->set_progress (min_progress);
@@ -853,11 +878,11 @@ Writer::set_digest_progress (Job* job, float progress)
 void
 Writer::calculate_referenced_digests (boost::function<void (float)> set_progress)
 {
-       BOOST_FOREACH (ReferencedReelAsset const& i, _reel_assets) {
-               shared_ptr<dcp::ReelMXF> mxf = dynamic_pointer_cast<dcp::ReelMXF>(i.asset);
-               if (mxf && !mxf->hash()) {
-                       mxf->asset_ref().asset()->hash (set_progress);
-                       mxf->set_hash (mxf->asset_ref().asset()->hash());
+       for (auto const& i: _reel_assets) {
+               auto file = dynamic_pointer_cast<dcp::ReelFileAsset>(i.asset);
+               if (file && !file->hash()) {
+                       file->asset_ref().asset()->hash (set_progress);
+                       file->set_hash (file->asset_ref().asset()->hash());
                }
        }
 }