Make show audio work on the whole DCP, not individual content.
[dcpomatic.git] / src / lib / film.cc
index e5904ca1e988d3b6da451d62e1bda96154511abe..590acedfd370f56dd04a5b2077c3a5758b78fc9f 100644 (file)
@@ -32,7 +32,6 @@
 #include "exceptions.h"
 #include "examine_content_job.h"
 #include "config.h"
-#include "ui_signaller.h"
 #include "playlist.h"
 #include "player.h"
 #include "dcp_content_type.h"
 #include "cinema.h"
 #include "safe_stringstream.h"
 #include "environment_info.h"
+#include "raw_convert.h"
+#include "audio_processor.h"
+#include "md5_digester.h"
 #include <libcxml/cxml.h>
 #include <dcp/cpl.h>
 #include <dcp/signer.h>
 #include <dcp/util.h>
 #include <dcp/local_time.h>
-#include <dcp/raw_convert.h>
 #include <dcp/decrypted_kdm.h>
 #include <libxml++/libxml++.h>
 #include <boost/filesystem.hpp>
@@ -86,8 +87,6 @@ using boost::optional;
 using boost::is_any_of;
 using dcp::Size;
 using dcp::Signer;
-using dcp::raw_convert;
-using dcp::raw_convert;
 
 #define LOG_GENERAL(...) log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
 #define LOG_GENERAL_NC(...) log()->log (__VA_ARGS__, Log::TYPE_GENERAL);
@@ -129,6 +128,7 @@ Film::Film (boost::filesystem::path dir, bool log)
        , _sequence_video (true)
        , _interop (false)
        , _burn_subtitles (false)
+       , _audio_processor (0)
        , _state_version (current_state_version)
        , _dirty (false)
 {
@@ -209,14 +209,14 @@ Film::video_identifier () const
        return s.str ();
 }
          
-/** @return The path to the directory to write video frame info files to */
+/** @return The file to write video frame info to */
 boost::filesystem::path
-Film::info_dir () const
+Film::info_file () const
 {
        boost::filesystem::path p;
        p /= "info";
        p /= video_identifier ();
-       return dir (p);
+       return file (p);
 }
 
 boost::filesystem::path
@@ -231,24 +231,6 @@ Film::internal_video_mxf_filename () const
        return video_identifier() + ".mxf";
 }
 
-boost::filesystem::path
-Film::video_mxf_filename () const
-{
-       return filename_safe_name() + "_video.mxf";
-}
-
-boost::filesystem::path
-Film::audio_mxf_filename () const
-{
-       return filename_safe_name() + "_audio.mxf";
-}
-
-boost::filesystem::path
-Film::subtitle_xml_filename () const
-{
-       return filename_safe_name() + "_subtitle.xml";
-}
-
 string
 Film::filename_safe_name () const
 {
@@ -266,9 +248,28 @@ Film::filename_safe_name () const
 }
 
 boost::filesystem::path
-Film::audio_analysis_dir () const
+Film::audio_analysis_path (shared_ptr<const Playlist> playlist) const
 {
-       return dir ("analysis");
+       boost::filesystem::path p = dir ("analysis");
+
+       MD5Digester digester;
+       BOOST_FOREACH (shared_ptr<Content> i, playlist->content ()) {
+               shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (i);
+               if (!ac) {
+                       continue;
+               }
+               
+               digester.add (ac->digest ());
+               digester.add (ac->audio_mapping().digest ());
+               digester.add (ac->audio_gain ());
+       }
+
+       if (audio_processor ()) {
+               digester.add (audio_processor()->id ());
+       }
+
+       p /= digester.get ();
+       return p;
 }
 
 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
@@ -318,25 +319,6 @@ Film::send_dcp_to_tms ()
        JobManager::instance()->add (j);
 }
 
-/** Count the number of frames that have been encoded for this film.
- *  @return frame count.
- */
-int
-Film::encoded_frames () const
-{
-       if (container() == 0) {
-               return 0;
-       }
-
-       int N = 0;
-       for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
-               ++N;
-               boost::this_thread::interruption_point ();
-       }
-
-       return N;
-}
-
 shared_ptr<xmlpp::Document>
 Film::metadata () const
 {
@@ -368,6 +350,9 @@ Film::metadata () const
        root->add_child("Signed")->add_child_text (_signed ? "1" : "0");
        root->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
        root->add_child("Key")->add_child_text (_key.hex ());
+       if (_audio_processor) {
+               root->add_child("AudioProcessor")->add_child_text (_audio_processor->id ());
+       }
        _playlist->as_xml (root->add_child ("Playlist"));
 
        return doc;
@@ -448,6 +433,12 @@ Film::read_metadata ()
        }
        _key = dcp::Key (f.string_child ("Key"));
 
+       if (f.optional_string_child ("AudioProcessor")) {
+               _audio_processor = AudioProcessor::from_id (f.string_child ("AudioProcessor"));
+       } else {
+               _audio_processor = 0;
+       }
+
        list<string> notes;
        /* This method is the only one that can return notes (so far) */
        _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"), _state_version, notes);
@@ -581,11 +572,12 @@ Film::isdcf_name (bool if_created_now) const
                d << "_" << container()->isdcf_name();
        }
 
+       ContentList cl = content ();
+       
        /* XXX: this uses the first bit of content only */
 
        /* The standard says we don't do this for trailers, for some strange reason */
        if (dcp_content_type() && dcp_content_type()->libdcp_kind() != dcp::TRAILER) {
-               ContentList cl = content ();
                Ratio const * content_ratio = 0;
                for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
                        shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
@@ -621,25 +613,39 @@ Film::isdcf_name (bool if_created_now) const
                }
        }
 
-       switch (audio_channels ()) {
-       case 1:
-               d << "_10";
-               break;
-       case 2:
-               d << "_20";
-               break;
-       case 3:
-               d << "_30";
-               break;
-       case 4:
-               d << "_40";
-               break;
-       case 5:
-               d << "_50";
-               break;
-       case 6:
-               d << "_51";
-               break;
+       /* Find all mapped channels */
+
+       list<int> mapped;
+       for (ContentList::const_iterator i = cl.begin(); i != cl.end(); ++i) {
+               shared_ptr<const AudioContent> ac = dynamic_pointer_cast<const AudioContent> (*i);
+               if (ac) {
+                       list<int> c = ac->audio_mapping().mapped_output_channels ();
+                       copy (c.begin(), c.end(), back_inserter (mapped));
+               }
+       }
+
+       mapped.sort ();
+       mapped.unique ();
+       
+       /* Count them */
+                       
+       int non_lfe = 0;
+       int lfe = 0;
+       for (list<int>::const_iterator i = mapped.begin(); i != mapped.end(); ++i) {
+               if (*i >= audio_channels()) {
+                       /* This channel is mapped but is not included in the DCP */
+                       continue;
+               }
+               
+               if (static_cast<dcp::Channel> (*i) == dcp::LFE) {
+                       ++lfe;
+               } else {
+                       ++non_lfe;
+               }
+       }
+
+       if (non_lfe) {
+               d << "_" << non_lfe << lfe;
        }
 
        /* XXX: HI/VI */
@@ -681,11 +687,26 @@ Film::isdcf_name (bool if_created_now) const
 string
 Film::dcp_name (bool if_created_now) const
 {
+       string unfiltered;
        if (use_isdcf_name()) {
-               return isdcf_name (if_created_now);
+               unfiltered = isdcf_name (if_created_now);
+       } else {
+               unfiltered = name ();
        }
 
-       return name();
+       /* Filter out `bad' characters which cause problems with some systems.
+          There's no apparent list of what really is allowed, so this is a guess.
+       */
+
+       string filtered;
+       string const allowed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_";
+       for (size_t i = 0; i < unfiltered.size(); ++i) {
+               if (allowed.find (unfiltered[i]) != string::npos) {
+                       filtered += unfiltered[i];
+               }
+       }
+       
+       return filtered;
 }
 
 void
@@ -779,6 +800,13 @@ Film::set_burn_subtitles (bool b)
        signal_changed (BURN_SUBTITLES);
 }
 
+void
+Film::set_audio_processor (AudioProcessor const * processor)
+{
+       _audio_processor = processor;
+       signal_changed (AUDIO_PROCESSOR);
+}
+
 void
 Film::signal_changed (Property p)
 {
@@ -796,9 +824,7 @@ Film::signal_changed (Property p)
                break;
        }
 
-       if (ui_signaller) {
-               ui_signaller->emit (boost::bind (boost::ref (Changed), p));
-       }
+       emit (boost::bind (boost::ref (Changed), p));
 }
 
 void
@@ -807,32 +833,6 @@ Film::set_isdcf_date_today ()
        _isdcf_date = boost::gregorian::day_clock::local_day ();
 }
 
-boost::filesystem::path
-Film::info_path (int f, Eyes e) const
-{
-       boost::filesystem::path p;
-       p /= info_dir ();
-
-       SafeStringStream s;
-       s.width (8);
-       s << setfill('0') << f;
-
-       if (e == EYES_LEFT) {
-               s << ".L";
-       } else if (e == EYES_RIGHT) {
-               s << ".R";
-       }
-
-       s << ".md5";
-       
-       p /= s.str();
-
-       /* info_dir() will already have added any initial bit of the path,
-          so don't call file() on this.
-       */
-       return p;
-}
-
 boost::filesystem::path
 Film::j2c_path (int f, Eyes e, bool t) const
 {
@@ -913,6 +913,13 @@ Film::set_encrypted (bool e)
        signal_changed (ENCRYPTED);
 }
 
+void
+Film::set_key (dcp::Key key)
+{
+       _key = key;
+       signal_changed (KEY);
+}
+
 shared_ptr<Playlist>
 Film::playlist () const
 {
@@ -1014,17 +1021,18 @@ Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
 {
        if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
                set_video_frame_rate (_playlist->best_dcp_frame_rate ());
-       } 
-
-       if (ui_signaller) {
-               ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
+       } else if (p == AudioContentProperty::AUDIO_STREAMS) {
+               signal_changed (NAME);
        }
+
+       emit (boost::bind (boost::ref (ContentChanged), c, p));
 }
 
 void
 Film::playlist_changed ()
 {
        signal_changed (CONTENT);
+       signal_changed (NAME);
 }      
 
 int
@@ -1061,7 +1069,7 @@ Film::full_frame () const
 dcp::Size
 Film::frame_size () const
 {
-       return fit_ratio_within (container()->ratio(), full_frame (), 1);
+       return fit_ratio_within (container()->ratio(), full_frame ());
 }
 
 dcp::EncryptedKDM
@@ -1121,10 +1129,29 @@ Film::required_disk_space () const
  *  Note: the decision made by this method isn't, of course, 100% reliable.
  */
 bool
-Film::should_be_enough_disk_space (double& required, double& available) const
-{
+Film::should_be_enough_disk_space (double& required, double& available, bool& can_hard_link) const
+{
+       /* Create a test file and see if we can hard-link it */
+       boost::filesystem::path test = internal_video_mxf_dir() / "test";
+       boost::filesystem::path test2 = internal_video_mxf_dir() / "test2";
+       can_hard_link = true;
+       FILE* f = fopen_boost (test, "w");
+       if (f) {
+               fclose (f);
+               boost::system::error_code ec;
+               boost::filesystem::create_hard_link (test, test2, ec);
+               if (ec) {
+                       can_hard_link = false;
+               }
+               boost::filesystem::remove (test);
+               boost::filesystem::remove (test2);
+       }
+
        boost::filesystem::space_info s = boost::filesystem::space (internal_video_mxf_dir ());
        required = double (required_disk_space ()) / 1073741824.0f;
+       if (!can_hard_link) {
+               required *= 2;
+       }
        available = double (s.available) / 1073741824.0f;
        return (available - required) > 1;
 }
@@ -1153,3 +1180,53 @@ Film::subtitle_language () const
 
        return all;
 }
+
+/** Change the gains of the supplied AudioMapping to make it a default
+ *  for this film.  The defaults are guessed based on what processor (if any)
+ *  is in use and the number of input channels.
+ */
+void
+Film::make_audio_mapping_default (AudioMapping& mapping) const
+{
+       if (audio_processor ()) {
+               audio_processor()->make_audio_mapping_default (mapping);
+       } else {
+               mapping.make_zero ();
+               if (mapping.input_channels() == 1) {
+                       /* Mono -> Centre */
+                       mapping.set (0, static_cast<int> (dcp::CENTRE), 1);
+               } else {
+                       /* 1:1 mapping */
+                       for (int i = 0; i < min (mapping.input_channels(), mapping.output_channels()); ++i) {
+                               mapping.set (i, i, 1);
+                       }
+               }
+       }
+}
+
+/** @return The names of the channels that audio contents' outputs are passed into;
+ *  this is either the DCP or a AudioProcessor.
+ */
+vector<string>
+Film::audio_output_names () const
+{
+       if (audio_processor ()) {
+               return audio_processor()->input_names ();
+       }
+       
+       vector<string> n;
+       n.push_back (_("L"));
+       n.push_back (_("R"));
+       n.push_back (_("C"));
+       n.push_back (_("Lfe"));
+       n.push_back (_("Ls"));
+       n.push_back (_("Rs"));
+       n.push_back (_("HI"));
+       n.push_back (_("VI"));
+       n.push_back (_("Lc"));
+       n.push_back (_("Rc"));
+       n.push_back (_("BsL"));
+       n.push_back (_("BsR"));
+
+       return vector<string> (n.begin(), n.begin() + audio_channels ());
+}