Make burnt-in subtitle outline width configurable (#940).
[dcpomatic.git] / src / lib / reel_writer.cc
index bc0013ff130ec7230f324f80563114f7f131f8f5..d742818ae08849bfe81e191c3f922deb01a3b6c0 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -23,7 +23,7 @@
 #include "cross.h"
 #include "job.h"
 #include "log.h"
-#include "md5_digester.h"
+#include "digester.h"
 #include "font.h"
 #include "compose.hpp"
 #include "audio_buffers.h"
@@ -60,13 +60,18 @@ using dcp::Data;
 
 int const ReelWriter::_info_size = 48;
 
-ReelWriter::ReelWriter (shared_ptr<const Film> film, DCPTimePeriod period, shared_ptr<Job> job)
+ReelWriter::ReelWriter (
+       shared_ptr<const Film> film, DCPTimePeriod period, shared_ptr<Job> job, int reel_index, int reel_count, optional<string> content_summary
+       )
        : _film (film)
        , _period (period)
        , _first_nonexistant_frame (0)
        , _last_written_video_frame (-1)
        , _last_written_eyes (EYES_RIGHT)
        , _total_written_audio_frames (0)
+       , _reel_index (reel_index)
+       , _reel_count (reel_count)
+       , _content_summary (content_summary)
 {
        /* Create our picture asset in a subdirectory, named according to those
           film's parameters which affect the video output.  We will hard-link
@@ -107,11 +112,13 @@ ReelWriter::ReelWriter (shared_ptr<const Film> film, DCPTimePeriod period, share
                        _sound_asset->set_key (_film->key ());
                }
 
+               DCPOMATIC_ASSERT (_film->directory());
+
                /* 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() / audio_asset_filename (_sound_asset),
+                       _film->directory().get() / audio_asset_filename (_sound_asset, _reel_index, _reel_count, _content_summary),
                        _film->interop() ? dcp::INTEROP : dcp::SMPTE
                        );
        }
@@ -123,13 +130,14 @@ ReelWriter::write_frame_info (Frame frame, Eyes eyes, dcp::FrameInfo info) const
 {
        FILE* file = 0;
        boost::filesystem::path info_file = _film->info_file (_period);
-       if (boost::filesystem::exists (info_file)) {
+       bool const read = boost::filesystem::exists (info_file);
+       if (read) {
                file = fopen_boost (info_file, "r+b");
        } else {
                file = fopen_boost (info_file, "wb");
        }
        if (!file) {
-               throw OpenFileError (info_file);
+               throw OpenFileError (info_file, errno, read);
        }
        dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
        fwrite (&info.offset, sizeof (info.offset), 1, file);
@@ -175,12 +183,13 @@ void
 ReelWriter::check_existing_picture_asset ()
 {
        /* Try to open the existing asset */
-       FILE* asset_file = fopen_boost (_picture_asset->file(), "rb");
+       DCPOMATIC_ASSERT (_picture_asset->file());
+       FILE* asset_file = fopen_boost (_picture_asset->file().get(), "rb");
        if (!asset_file) {
-               LOG_GENERAL ("Could not open existing asset at %1 (errno=%2)", _picture_asset->file().string(), errno);
+               LOG_GENERAL ("Could not open existing asset at %1 (errno=%2)", _picture_asset->file()->string(), errno);
                return;
        } else {
-               LOG_GENERAL ("Opened existing asset at %1", _picture_asset->file().string());
+               LOG_GENERAL ("Opened existing asset at %1", _picture_asset->file()->string());
        }
 
        /* Offset of the last dcp::FrameInfo in the info file */
@@ -263,10 +272,11 @@ ReelWriter::finish ()
 
        /* Hard-link any video asset file into the DCP */
        if (_picture_asset) {
-               boost::filesystem::path video_from = _picture_asset->file ();
+               DCPOMATIC_ASSERT (_picture_asset->file());
+               boost::filesystem::path video_from = _picture_asset->file().get();
                boost::filesystem::path video_to;
                video_to /= _film->dir (_film->dcp_name());
-               video_to /= video_asset_filename (_picture_asset);
+               video_to /= video_asset_filename (_picture_asset, _reel_index, _reel_count, _content_summary);
 
                boost::system::error_code ec;
                boost::filesystem::create_hard_link (video_from, video_to, ec);
@@ -286,13 +296,14 @@ ReelWriter::finish ()
        if (_sound_asset) {
                boost::filesystem::path audio_to;
                audio_to /= _film->dir (_film->dcp_name ());
-               audio_to /= audio_asset_filename (_sound_asset);
+               string const aaf = audio_asset_filename (_sound_asset, _reel_index, _reel_count, _content_summary);
+               audio_to /= aaf;
 
                boost::system::error_code ec;
-               boost::filesystem::rename (_film->file (audio_asset_filename (_sound_asset)), audio_to, ec);
+               boost::filesystem::rename (_film->file (aaf), audio_to, ec);
                if (ec) {
                        throw FileError (
-                               String::compose (_("could not move audio asset into the DCP (%1)"), ec.value ()), audio_asset_filename (_sound_asset)
+                               String::compose (_("could not move audio asset into the DCP (%1)"), ec.value ()), aaf
                                );
                }
 
@@ -328,7 +339,9 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
                }
        }
 
+       DCPOMATIC_ASSERT (reel_picture_asset);
        reel->add (reel_picture_asset);
+
        /* If we have a hash for this asset in the CPL, assume that it is correct */
        if (reel_picture_asset->hash()) {
                reel_picture_asset->asset_ref()->set_hash (reel_picture_asset->hash().get());
@@ -417,16 +430,14 @@ ReelWriter::create_reel (list<ReferencedReelAsset> const & refs, list<shared_ptr
 }
 
 void
-ReelWriter::calculate_digests (shared_ptr<Job> job)
+ReelWriter::calculate_digests (boost::function<void (float)> set_progress)
 {
-       job->sub (_("Computing image digest"));
        if (_picture_asset) {
-               _picture_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
+               _picture_asset->hash (set_progress);
        }
 
        if (_sound_asset) {
-               job->sub (_("Computing audio digest"));
-               _sound_asset->hash (boost::bind (&Job::set_progress, job.get(), _1, false));
+               _sound_asset->hash (set_progress);
        }
 }
 
@@ -473,11 +484,14 @@ ReelWriter::write (PlayerSubtitles subs)
                        s->set_reel_number (1);
                        s->set_time_code_rate (_film->video_frame_rate ());
                        s->set_start_time (dcp::Time ());
+                       if (_film->encrypted ()) {
+                               s->set_key (_film->key ());
+                       }
                        _subtitle_asset = s;
                }
        }
 
-       BOOST_FOREACH (dcp::SubtitleString i, subs.text) {
+       BOOST_FOREACH (SubtitleString i, subs.text) {
                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));
                _subtitle_asset->add (i);
@@ -505,7 +519,7 @@ ReelWriter::existing_picture_frame_ok (FILE* asset_file, FILE* info_file) const
                LOG_GENERAL ("Existing frame %1 is incomplete", _first_nonexistant_frame);
                ok = false;
        } else {
-               MD5Digester digester;
+               Digester digester;
                digester.add (data.data().get(), data.size());
                LOG_GENERAL ("Hash %1 vs %2", digester.get(), info.hash);
                if (digester.get() != info.hash) {