Round image line sizes up to 32. Seems to help with LHS image corruption with both...
[dcpomatic.git] / src / lib / film_state.cc
index ae70b53bdc4626776a1f055541726e8c56cc30b7..a4d88d0e05a295cffe096231dd9956a4335fa687 100644 (file)
@@ -35,6 +35,7 @@
 #include "format.h"
 #include "dcp_content_type.h"
 #include "util.h"
+#include "exceptions.h"
 
 using namespace std;
 using namespace boost;
@@ -55,10 +56,10 @@ FilmState::write_metadata (ofstream& f) const
        if (format) {
                f << "format " << format->as_metadata () << "\n";
        }
-       f << "left_crop " << left_crop << "\n";
-       f << "right_crop " << right_crop << "\n";
-       f << "top_crop " << top_crop << "\n";
-       f << "bottom_crop " << bottom_crop << "\n";
+       f << "left_crop " << crop.left << "\n";
+       f << "right_crop " << crop.right << "\n";
+       f << "top_crop " << crop.top << "\n";
+       f << "bottom_crop " << crop.bottom << "\n";
        for (vector<Filter const *>::const_iterator i = filters.begin(); i != filters.end(); ++i) {
                f << "filter " << (*i)->id () << "\n";
        }
@@ -79,6 +80,9 @@ FilmState::write_metadata (ofstream& f) const
        f << "audio_gain " << audio_gain << "\n";
        f << "audio_delay " << audio_delay << "\n";
        f << "still_duration " << still_duration << "\n";
+       f << "with_subtitles " << with_subtitles << "\n";
+       f << "subtitle_offset " << subtitle_offset << "\n";
+       f << "subtitle_scale " << subtitle_scale << "\n";
 
        /* Cached stuff; this is information about our content; we could
           look it up each time, but that's slow.
@@ -93,6 +97,7 @@ FilmState::write_metadata (ofstream& f) const
        f << "audio_sample_rate " << audio_sample_rate << "\n";
        f << "audio_sample_format " << audio_sample_format_to_string (audio_sample_format) << "\n";
        f << "content_digest " << content_digest << "\n";
+       f << "has_subtitles " << has_subtitles << "\n";
 }
 
 /** Read state from a key / value pair.
@@ -114,13 +119,13 @@ FilmState::read_metadata (string k, string v)
        } else if (k == "format") {
                format = Format::from_metadata (v);
        } else if (k == "left_crop") {
-               left_crop = atoi (v.c_str ());
+               crop.left = atoi (v.c_str ());
        } else if (k == "right_crop") {
-               right_crop = atoi (v.c_str ());
+               crop.right = atoi (v.c_str ());
        } else if (k == "top_crop") {
-               top_crop = atoi (v.c_str ());
+               crop.top = atoi (v.c_str ());
        } else if (k == "bottom_crop") {
-               bottom_crop = atoi (v.c_str ());
+               crop.bottom = atoi (v.c_str ());
        } else if (k == "filter") {
                filters.push_back (Filter::from_id (v));
        } else if (k == "scaler") {
@@ -141,6 +146,12 @@ FilmState::read_metadata (string k, string v)
                audio_delay = atoi (v.c_str ());
        } else if (k == "still_duration") {
                still_duration = atoi (v.c_str ());
+       } else if (k == "with_subtitles") {
+               with_subtitles = (v == "1");
+       } else if (k == "subtitle_offset") {
+               subtitle_offset = atoi (v.c_str ());
+       } else if (k == "subtitle_scale") {
+               subtitle_scale = atof (v.c_str ());
        }
        
        /* Cached stuff */
@@ -164,11 +175,8 @@ FilmState::read_metadata (string k, string v)
                audio_sample_format = audio_sample_format_from_string (v);
        } else if (k == "content_digest") {
                content_digest = v;
-       }
-       
-       /* Itsy bitsy hack: compute digest here if don't have one (for backwards compatibility) */
-       if (content_digest.empty() && !content.empty()) {
-               content_digest = md5_digest (file (content));
+       } else if (k == "has_subtitles") {
+               has_subtitles = (v == "1");
        }
 }
 
@@ -188,10 +196,22 @@ FilmState::thumb_file (int n) const
  */
 string
 FilmState::thumb_file_for_frame (int n) const
+{
+       return thumb_base_for_frame(n) + ".png";
+}
+
+string
+FilmState::thumb_base (int n) const
+{
+       return thumb_base_for_frame (thumb_frame (n));
+}
+
+string
+FilmState::thumb_base_for_frame (int n) const
 {
        stringstream s;
        s.width (8);
-       s << setfill('0') << n << ".tiff";
+       s << setfill('0') << n;
        
        filesystem::path p;
        p /= dir ("thumbs");
@@ -214,8 +234,8 @@ FilmState::thumb_frame (int n) const
 Size
 FilmState::cropped_size (Size s) const
 {
-       s.width -= left_crop + right_crop;
-       s.height -= top_crop + bottom_crop;
+       s.width -= crop.left + crop.right;
+       s.height -= crop.top + crop.bottom;
        return s;
 }
 
@@ -256,13 +276,58 @@ ContentType
 FilmState::content_type () const
 {
 #if BOOST_FILESYSTEM_VERSION == 3
-       string const ext = filesystem::path(content).extension().string();
+       string ext = filesystem::path(content).extension().string();
 #else
-       string const ext = filesystem::path(content).extension();
+       string ext = filesystem::path(content).extension();
 #endif
+
+       transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
+       
        if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
                return STILL;
        }
 
        return VIDEO;
 }
+
+/** @return Number of bytes per sample of a single channel */
+int
+FilmState::bytes_per_sample () const
+{
+       switch (audio_sample_format) {
+       case AV_SAMPLE_FMT_S16:
+               return 2;
+       default:
+               return 0;
+       }
+
+       return 0;
+}
+
+int
+FilmState::target_sample_rate () const
+{
+       /* Resample to a DCI-approved sample rate */
+       double t = dcp_audio_sample_rate (audio_sample_rate);
+
+       /* Compensate for the fact that video will be rounded to the
+          nearest integer number of frames per second.
+       */
+       if (rint (frames_per_second) != frames_per_second) {
+               t *= frames_per_second / rint (frames_per_second);
+       }
+
+       return rint (t);
+}
+
+int
+FilmState::dcp_length () const
+{
+       if (dcp_frames) {
+               return dcp_frames;
+       }
+
+       return length;
+}
+
+