Use drop-frame sound hack for 30 fps drop as well.
[dcpomatic.git] / src / lib / film_state.cc
index 343e9ebb4f4c4305e9f26e60b393c618d9fd973f..d7d9a1462a7896e2581838f855b575e4d232bbb7 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";
        }
@@ -114,13 +115,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") {
@@ -165,11 +166,6 @@ FilmState::read_metadata (string k, 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 (content_path ());
-       }
 }
 
 
@@ -214,8 +210,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 +252,50 @@ 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
+{
+       double t = dcp_audio_sample_rate (audio_sample_rate);
+       if (rint (frames_per_second) != frames_per_second) {
+               if (fabs (frames_per_second - 23.976) < 1e-6 || (fabs (frames_per_second - 29.97) < 1e-6)) {
+                       /* 24fps or 30fps drop-frame ie {24,30} * 1000 / 1001 frames per second;
+                          hence we need to resample the audio to dcp_audio_sample_rate * 1000 / 1001
+                          so that when we play it back at dcp_audio_sample_rate it is sped up
+                          by the same amount that the video is
+                       */
+                       t *= double(1000) / 1001;
+               } else {
+                       throw EncodeError ("unknown fractional frame rate");
+               }
+       }
+
+       return rint (t);
+}