Split Format into Fixed and Variable so that sources can be unstretched.
[dcpomatic.git] / src / lib / film.cc
index d2a1948ca1a1f3ae2e7038cf726bdf060c7dc123..330ae9e5dcfd8e322dec323005922d591bb38045 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <stdexcept>
 #include <iostream>
+#include <algorithm>
 #include <fstream>
 #include <cstdlib>
 #include <sstream>
@@ -47,6 +48,8 @@
 #include "scaler.h"
 #include "decoder_factory.h"
 #include "config.h"
+#include "check_hashes_job.h"
+#include "version.h"
 
 using namespace std;
 using namespace boost;
@@ -68,7 +71,7 @@ Film::Film (string d, bool must_exist)
        
        filesystem::path p (filesystem::system_complete (d));
        filesystem::path result;
-       for(filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
+       for (filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
                if (*i == "..") {
                        if (filesystem::is_symlink (result) || result.filename() == "..") {
                                result /= *i;
@@ -82,13 +85,17 @@ Film::Film (string d, bool must_exist)
 
        _state.directory = result.string ();
        
-       if (must_exist && !filesystem::exists (_state.directory)) {
-               throw OpenFileError (_state.directory);
+       if (!filesystem::exists (_state.directory)) {
+               if (must_exist) {
+                       throw OpenFileError (_state.directory);
+               } else {
+                       filesystem::create_directory (_state.directory);
+               }
        }
 
        read_metadata ();
 
-       _log = new Log (_state.file ("log"));
+       _log = new FileLog (_state.file ("log"));
 }
 
 /** Copy constructor */
@@ -122,6 +129,10 @@ Film::read_metadata ()
                        continue;
                }
 
+               if (line[line.size() - 1] == '\r') {
+                       line = line.substr (0, line.size() - 1);
+               }
+
                size_t const s = line.find (' ');
                if (s == string::npos) {
                        continue;
@@ -165,7 +176,24 @@ Film::set_name (string n)
 void
 Film::set_content (string c)
 {
-       if (filesystem::path(c).has_root_directory () && starts_with (c, _state.directory)) {
+       string check = _state.directory;
+
+#if BOOST_FILESYSTEM_VERSION == 3
+       filesystem::path slash ("/");
+       string platform_slash = slash.make_preferred().string ();
+#else
+#ifdef DVDOMATIC_WINDOWS
+       string platform_slash = "\\";
+#else
+       string platform_slash = "/";
+#endif
+#endif 
+
+       if (!ends_with (check, platform_slash)) {
+               check += platform_slash;
+       }
+       
+       if (filesystem::path(c).has_root_directory () && starts_with (c, check)) {
                c = c.substr (_state.directory.length() + 1);
        }
 
@@ -190,7 +218,7 @@ Film::set_content (string c)
        _state.audio_sample_rate = d->audio_sample_rate ();
        _state.audio_sample_format = d->audio_sample_format ();
 
-       _state.content_digest = md5_digest (c);
+       _state.content_digest = md5_digest (s->content_path ());
        _state.content = c;
        
        signal_changed (SIZE);
@@ -412,7 +440,6 @@ Film::j2k_dir () const
 
        filesystem::path p;
 
-
        /* Start with j2c */
        p /= "j2c";
 
@@ -461,18 +488,12 @@ Film::make_dcp (bool transcode, int freq)
                throw BadSettingError ("name", "cannot contain slashes");
        }
        
-       {
-               stringstream s;
-               s << "DVD-o-matic " << DVDOMATIC_VERSION << " using " << dependency_version_summary ();
-               log()->log (s.str ());
-       }
+       log()->log (String::compose ("DVD-o-matic %1 git %2 using %3", dvdomatic_version, dvdomatic_git_commit, dependency_version_summary()));
 
        {
                char buffer[128];
                gethostname (buffer, sizeof (buffer));
-               stringstream s;
-               s << "Starting to make a DCP on " << buffer;
-               log()->log (s.str ());
+               log()->log (String::compose ("Starting to make DCP on %1", buffer));
        }
                
        if (format() == 0) {
@@ -513,8 +534,8 @@ Film::make_dcp (bool transcode, int freq)
        }
        
        o->decode_video_frequency = freq;
-       o->padding = format()->dcp_padding ();
-       o->ratio = format()->ratio_as_float ();
+       o->padding = format()->dcp_padding (this);
+       o->ratio = format()->ratio_as_float (this);
 
        if (transcode) {
                if (_state.dcp_ab) {
@@ -523,7 +544,8 @@ Film::make_dcp (bool transcode, int freq)
                        JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (fs, o, log ())));
                }
        }
-       
+
+       JobManager::instance()->add (shared_ptr<Job> (new CheckHashesJob (fs, o, log ())));
        JobManager::instance()->add (shared_ptr<Job> (new MakeDCPJob (fs, o, log ())));
 }
 
@@ -624,3 +646,18 @@ Film::copy_from_dvd ()
        JobManager::instance()->add (j);
 }
 
+int
+Film::encoded_frames () const
+{
+       if (format() == 0) {
+               return 0;
+       }
+
+       int N = 0;
+       for (filesystem::directory_iterator i = filesystem::directory_iterator (j2k_dir ()); i != filesystem::directory_iterator(); ++i) {
+               ++N;
+               this_thread::interruption_point ();
+       }
+
+       return N;
+}