Add basics of colourspace conversion bypass (#266).
[dcpomatic.git] / src / lib / video_content.cc
index 783cddafad958fcb5ee81cc3806bea3dc2dff8e8..3094a70c8ca34a87c01f13c82400cf46702ce139 100644 (file)
@@ -30,6 +30,8 @@
 #include "util.h"
 #include "film.h"
 #include "exceptions.h"
+#include "frame_rate_change.h"
+#include "safe_stringstream.h"
 
 #include "i18n.h"
 
@@ -41,45 +43,47 @@ int const VideoContentProperty::VIDEO_SCALE   = 4;
 int const VideoContentProperty::COLOUR_CONVERSION = 5;
 
 using std::string;
-using std::stringstream;
 using std::setprecision;
 using std::cout;
 using std::vector;
+using std::min;
+using std::max;
 using boost::shared_ptr;
 using boost::optional;
 using boost::dynamic_pointer_cast;
 using libdcp::raw_convert;
 
-vector<VideoContentScale> VideoContentScale::_scales;
-
 VideoContent::VideoContent (shared_ptr<const Film> f)
        : Content (f)
        , _video_length (0)
+       , _original_video_frame_rate (0)
        , _video_frame_rate (0)
        , _video_frame_type (VIDEO_FRAME_TYPE_2D)
-       , _scale (Ratio::from_id ("185"))
+       , _scale (Config::instance()->default_scale ())
 {
-       setup_default_colour_conversion ();
+       set_default_colour_conversion ();
 }
 
 VideoContent::VideoContent (shared_ptr<const Film> f, Time s, VideoContent::Frame len)
        : Content (f, s)
        , _video_length (len)
+       , _original_video_frame_rate (0)
        , _video_frame_rate (0)
        , _video_frame_type (VIDEO_FRAME_TYPE_2D)
-       , _scale (Ratio::from_id ("185"))
+       , _scale (Config::instance()->default_scale ())
 {
-       setup_default_colour_conversion ();
+       set_default_colour_conversion ();
 }
 
 VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
        : Content (f, p)
        , _video_length (0)
+       , _original_video_frame_rate (0)
        , _video_frame_rate (0)
        , _video_frame_type (VIDEO_FRAME_TYPE_2D)
-       , _scale (Ratio::from_id ("185"))
+       , _scale (Config::instance()->default_scale ())
 {
-       setup_default_colour_conversion ();
+       set_default_colour_conversion ();
 }
 
 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node, int version)
@@ -89,6 +93,7 @@ VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Nod
        _video_size.width = node->number_child<int> ("VideoWidth");
        _video_size.height = node->number_child<int> ("VideoHeight");
        _video_frame_rate = node->number_child<float> ("VideoFrameRate");
+       _original_video_frame_rate = node->optional_number_child<float> ("OriginalVideoFrameRate").get_value_or (_video_frame_rate);
        _video_frame_type = static_cast<VideoFrameType> (node->number_child<int> ("VideoFrameType"));
        _crop.left = node->number_child<int> ("LeftCrop");
        _crop.right = node->number_child<int> ("RightCrop");
@@ -103,8 +108,10 @@ VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Nod
        } else {
                _scale = VideoContentScale (node->node_child ("Scale"));
        }
-       
-       _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
+
+       if (node->optional_node_child ("ColourConversion")) {
+               _colour_conversion = ColourConversion (node->node_child ("ColourConversion"));
+       }
 }
 
 VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
@@ -145,6 +152,7 @@ VideoContent::VideoContent (shared_ptr<const Film> f, vector<shared_ptr<Content>
        }
 
        _video_size = ref->video_size ();
+       _original_video_frame_rate = ref->original_video_frame_rate ();
        _video_frame_rate = ref->video_frame_rate ();
        _video_frame_type = ref->video_frame_type ();
        _crop = ref->crop ();
@@ -160,19 +168,19 @@ VideoContent::as_xml (xmlpp::Node* node) const
        node->add_child("VideoWidth")->add_child_text (raw_convert<string> (_video_size.width));
        node->add_child("VideoHeight")->add_child_text (raw_convert<string> (_video_size.height));
        node->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
+       node->add_child("OriginalVideoFrameRate")->add_child_text (raw_convert<string> (_original_video_frame_rate));
        node->add_child("VideoFrameType")->add_child_text (raw_convert<string> (static_cast<int> (_video_frame_type)));
-       node->add_child("LeftCrop")->add_child_text (raw_convert<string> (_crop.left));
-       node->add_child("RightCrop")->add_child_text (raw_convert<string> (_crop.right));
-       node->add_child("TopCrop")->add_child_text (raw_convert<string> (_crop.top));
-       node->add_child("BottomCrop")->add_child_text (raw_convert<string> (_crop.bottom));
+       _crop.as_xml (node);
        _scale.as_xml (node->add_child("Scale"));
-       _colour_conversion.as_xml (node->add_child("ColourConversion"));
+       if (_colour_conversion) {
+               _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
+       }
 }
 
 void
-VideoContent::setup_default_colour_conversion ()
+VideoContent::set_default_colour_conversion ()
 {
-       _colour_conversion = PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion;
+       set_colour_conversion (PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6).conversion);
 }
 
 void
@@ -186,6 +194,7 @@ VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
                boost::mutex::scoped_lock lm (_mutex);
                _video_size = vs;
                _video_frame_rate = vfr;
+               _original_video_frame_rate = vfr;
        }
        
        signal_changed (VideoContentProperty::VIDEO_SIZE);
@@ -200,7 +209,7 @@ VideoContent::information () const
                return "";
        }
        
-       stringstream s;
+       SafeStringStream s;
 
        s << String::compose (
                _("%1x%2 pixels (%3:1)"),
@@ -292,14 +301,17 @@ VideoContent::set_scale (VideoContentScale s)
 string
 VideoContent::identifier () const
 {
-       stringstream s;
+       SafeStringStream s;
        s << Content::identifier()
          << "_" << crop().left
          << "_" << crop().right
          << "_" << crop().top
          << "_" << crop().bottom
-         << "_" << scale().id()
-         << "_" << colour_conversion().identifier ();
+         << "_" << scale().id();
+
+       if (colour_conversion()) {
+               s << "_" << colour_conversion().get().identifier ();
+       }
 
        return s.str ();
 }
@@ -343,6 +355,17 @@ VideoContent::video_size_after_3d_split () const
        assert (false);
 }
 
+void
+VideoContent::unset_colour_conversion ()
+{
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               _colour_conversion = boost::optional<ColourConversion> ();
+       }
+
+       signal_changed (VideoContentProperty::COLOUR_CONVERSION);
+}
+
 void
 VideoContent::set_colour_conversion (ColourConversion c)
 {
@@ -370,7 +393,7 @@ VideoContent::time_to_content_video_frames (Time t) const
        shared_ptr<const Film> film = _film.lock ();
        assert (film);
        
-       FrameRateConversion frc (video_frame_rate(), film->video_frame_rate());
+       FrameRateChange frc (video_frame_rate(), film->video_frame_rate());
 
        /* Here we are converting from time (in the DCP) to a frame number in the content.
           Hence we need to use the DCP's frame rate and the double/skip correction, not
@@ -379,123 +402,44 @@ VideoContent::time_to_content_video_frames (Time t) const
        return t * film->video_frame_rate() / (frc.factor() * TIME_HZ);
 }
 
-VideoContentScale::VideoContentScale (Ratio const * r)
-       : _ratio (r)
-       , _scale (true)
-{
-
-}
-
-VideoContentScale::VideoContentScale ()
-       : _ratio (0)
-       , _scale (false)
-{
-
-}
-
-VideoContentScale::VideoContentScale (bool scale)
-       : _ratio (0)
-       , _scale (scale)
-{
-
-}
-
-VideoContentScale::VideoContentScale (shared_ptr<cxml::Node> node)
-       : _ratio (0)
-       , _scale (true)
-{
-       optional<string> r = node->optional_string_child ("Ratio");
-       if (r) {
-               _ratio = Ratio::from_id (r.get ());
-       } else {
-               _scale = node->bool_child ("Scale");
-       }
-}
-
 void
-VideoContentScale::as_xml (xmlpp::Node* node) const
+VideoContent::scale_and_crop_to_fit_width ()
 {
-       if (_ratio) {
-               node->add_child("Ratio")->add_child_text (_ratio->id ());
-       } else {
-               node->add_child("Scale")->add_child_text (_scale ? "1" : "0");
-       }
-}
-
-string
-VideoContentScale::id () const
-{
-       stringstream s;
-       
-       if (_ratio) {
-               s << _ratio->id () << "_";
-       } else {
-               s << (_scale ? "S1" : "S0");
-       }
-       
-       return s.str ();
-}
-
-string
-VideoContentScale::name () const
-{
-       if (_ratio) {
-               return _ratio->nickname ();
-       }
+       shared_ptr<const Film> film = _film.lock ();
+       assert (film);
 
-       if (_scale) {
-               return _("No stretch");
-       }
+       set_scale (VideoContentScale (film->container ()));
 
-       return _("No scale");
+       int const crop = max (0, int (video_size().height - double (film->frame_size().height) * video_size().width / film->frame_size().width));
+       set_top_crop (crop / 2);
+       set_bottom_crop (crop / 2);
 }
 
-/** @param display_container Size of the container that we are displaying this content in.
- *  @param film_container The size of the film's image.
- */
-libdcp::Size
-VideoContentScale::size (shared_ptr<const VideoContent> c, libdcp::Size display_container, libdcp::Size film_container) const
+void
+VideoContent::scale_and_crop_to_fit_height ()
 {
-       if (_ratio) {
-               return fit_ratio_within (_ratio->ratio (), display_container);
-       }
-
-       libdcp::Size const ac = c->video_size_after_crop ();
+       shared_ptr<const Film> film = _film.lock ();
+       assert (film);
 
-       /* Force scale if the film_container is smaller than the content's image */
-       if (_scale || film_container.width < ac.width || film_container.height < ac.height) {
-               return fit_ratio_within (ac.ratio (), display_container);
-       }
+       set_scale (VideoContentScale (film->container ()));
 
-       /* Scale the image so that it will be in the right place in film_container, even if display_container is a
-          different size.
-       */
-       return libdcp::Size (
-               c->video_size().width  * float(display_container.width)  / film_container.width,
-               c->video_size().height * float(display_container.height) / film_container.height
-               );
+       int const crop = max (0, int (video_size().width - double (film->frame_size().width) * video_size().height / film->frame_size().height));
+       set_left_crop (crop / 2);
+       set_right_crop (crop / 2);
 }
 
 void
-VideoContentScale::setup_scales ()
+VideoContent::set_video_frame_rate (float r)
 {
-       vector<Ratio const *> ratios = Ratio::all ();
-       for (vector<Ratio const *>::const_iterator i = ratios.begin(); i != ratios.end(); ++i) {
-               _scales.push_back (VideoContentScale (*i));
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               if (_video_frame_rate == r) {
+                       return;
+               }
+               
+               _video_frame_rate = r;
        }
-
-       _scales.push_back (VideoContentScale (true));
-       _scales.push_back (VideoContentScale (false));
-}
-
-bool
-operator== (VideoContentScale const & a, VideoContentScale const & b)
-{
-       return (a.ratio() == b.ratio() && a.scale() == b.scale());
+       
+       signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
 }
 
-bool
-operator!= (VideoContentScale const & a, VideoContentScale const & b)
-{
-       return (a.ratio() != b.ratio() || a.scale() != b.scale());
-}