Merge branch 'master' into 12bit
[dcpomatic.git] / src / lib / film.cc
index 609003bbae7f2f632c1d190da567b70ce6133e18..54503ef72c6c7bf5b8c21184fd736d5ba7d949cf 100644 (file)
@@ -22,7 +22,6 @@
 #include <algorithm>
 #include <fstream>
 #include <cstdlib>
-#include <sstream>
 #include <iomanip>
 #include <unistd.h>
 #include <boost/filesystem.hpp>
 #include "ratio.h"
 #include "cross.h"
 #include "cinema.h"
+#include "safe_stringstream.h"
 
 #include "i18n.h"
 
 using std::string;
-using std::stringstream;
 using std::multimap;
 using std::pair;
 using std::map;
@@ -77,6 +76,7 @@ using boost::to_upper_copy;
 using boost::ends_with;
 using boost::starts_with;
 using boost::optional;
+using boost::is_any_of;
 using libdcp::Size;
 using libdcp::Signer;
 using libdcp::raw_convert;
@@ -91,9 +91,11 @@ using libdcp::raw_convert;
  * 7 -> 8
  * Use <Scale> tag in <VideoContent> rather than <Ratio>.
  * 8 -> 9
- * DCI -> ISDCF
+ * DCI -> ISDCF.
+ * 9 -> 10
+ * Subtitle X and Y scale.
  */
-int const Film::current_state_version = 9;
+int const Film::current_state_version = 10;
 
 /** Construct a Film object in a given directory.
  *
@@ -158,7 +160,7 @@ Film::video_identifier () const
 {
        assert (container ());
 
-       stringstream s;
+       SafeStringStream s;
        s.imbue (std::locale::classic ());
        
        s << container()->id()
@@ -492,19 +494,42 @@ Film::file (boost::filesystem::path f) const
 string
 Film::isdcf_name (bool if_created_now) const
 {
-       stringstream d;
+       SafeStringStream d;
 
        string raw_name = name ();
+
+       /* Split the raw name up into words */
+       vector<string> words;
+       split (words, raw_name, is_any_of (" "));
+
        string fixed_name;
-       bool cap_next = true;
-       for (size_t i = 0; i < raw_name.length(); ++i) {
-               if (raw_name[i] == ' ') {
-                       cap_next = true;
-               } else if (cap_next) {
-                       fixed_name += toupper (raw_name[i]);
-                       cap_next = false;
-               } else {
-                       fixed_name += tolower (raw_name[i]);
+       
+       /* Add each word to fixed_name */
+       for (vector<string>::const_iterator i = words.begin(); i != words.end(); ++i) {
+               string w = *i;
+
+               /* First letter is always capitalised */
+               w[0] = toupper (w[0]);
+
+               /* Count caps in w */
+               size_t caps = 0;
+               for (size_t i = 0; i < w.size(); ++i) {
+                       if (isupper (w[i])) {
+                               ++caps;
+                       }
+               }
+               
+               /* If w is all caps make the rest of it lower case, otherwise
+                  leave it alone.
+               */
+               if (caps == w.size ()) {
+                       for (size_t i = 1; i < w.size(); ++i) {
+                               w[i] = tolower (w[i]);
+                       }
+               }
+
+               for (size_t i = 0; i < w.size(); ++i) {
+                       fixed_name += w[i];
                }
        }
 
@@ -557,18 +582,22 @@ Film::isdcf_name (bool if_created_now) const
                d << "_" << container()->isdcf_name();
        }
 
-       /* XXX: this only works for content which has been scaled to a given ratio,
-          and uses the first bit of content only.
-       */
+       /* 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() != libdcp::TRAILER) {
-               ContentList cl = content ();
                Ratio const * content_ratio = 0;
-               for (ContentList::const_iterator i = cl.begin(); i != cl.end(); ++i) {
+               ContentList cl = content ();
+               for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
                        shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
-                       if (vc && (content_ratio == 0 || vc->scale().ratio() != content_ratio)) {
-                               content_ratio = vc->scale().ratio();
+                       if (vc) {
+                               /* Here's the first piece of video content */
+                               if (vc->scale().ratio ()) {
+                                       content_ratio = vc->scale().ratio ();
+                               } else {
+                                       content_ratio = Ratio::from_ratio (vc->video_size().ratio ());
+                               }
+                               break;
                        }
                }
                
@@ -793,7 +822,7 @@ Film::info_path (int f, Eyes e) const
        boost::filesystem::path p;
        p /= info_dir ();
 
-       stringstream s;
+       SafeStringStream s;
        s.width (8);
        s << setfill('0') << f;
 
@@ -820,7 +849,7 @@ Film::j2c_path (int f, Eyes e, bool t) const
        p /= "j2c";
        p /= video_identifier ();
 
-       stringstream s;
+       SafeStringStream s;
        s.width (8);
        s << setfill('0') << f;
 
@@ -1062,7 +1091,8 @@ Film::make_kdm (
        shared_ptr<libdcp::Certificate> target,
        boost::filesystem::path cpl_file,
        boost::posix_time::ptime from,
-       boost::posix_time::ptime until
+       boost::posix_time::ptime until,
+       libdcp::KDM::Formulation formulation
        ) const
 {
        shared_ptr<const Signer> signer = make_signer ();
@@ -1071,7 +1101,7 @@ Film::make_kdm (
        struct tm* tm = localtime (&now);
        string const issue_date = libdcp::tm_to_string (tm);
        
-       return libdcp::KDM (cpl_file, signer, target, key (), from, until, "DCP-o-matic", issue_date);
+       return libdcp::KDM (cpl_file, signer, target, key (), from, until, "DCP-o-matic", issue_date, formulation);
 }
 
 list<libdcp::KDM>
@@ -1079,13 +1109,14 @@ Film::make_kdms (
        list<shared_ptr<Screen> > screens,
        boost::filesystem::path dcp,
        boost::posix_time::ptime from,
-       boost::posix_time::ptime until
+       boost::posix_time::ptime until,
+       libdcp::KDM::Formulation formulation
        ) const
 {
        list<libdcp::KDM> kdms;
 
        for (list<shared_ptr<Screen> >::iterator i = screens.begin(); i != screens.end(); ++i) {
-               kdms.push_back (make_kdm ((*i)->certificate, dcp, from, until));
+               kdms.push_back (make_kdm ((*i)->certificate, dcp, from, until, formulation));
        }
 
        return kdms;