Merge branch 'master' of ssh://git.carlh.net/home/carl/git/dcpomatic
[dcpomatic.git] / src / lib / video_content.cc
index 498d5b9db7f3215dd870bb40db3fc2c4ff29e646..fcf7e322966c6ec96ffe0de158a765ec59251516 100644 (file)
@@ -55,7 +55,6 @@ using std::cout;
 using std::vector;
 using std::min;
 using std::max;
-using std::stringstream;
 using std::fixed;
 using std::setprecision;
 using std::list;
@@ -69,6 +68,7 @@ VideoContent::VideoContent (shared_ptr<const Film> film)
        , _video_length (0)
        , _video_frame_type (VIDEO_FRAME_TYPE_2D)
        , _scale (VideoContentScale (Ratio::from_id ("178")))
+       , _yuv (true)
        , _fade_in (0)
        , _fade_out (0)
 {
@@ -80,6 +80,7 @@ VideoContent::VideoContent (shared_ptr<const Film> film, DCPTime s, Frame len)
        , _video_length (len)
        , _video_frame_type (VIDEO_FRAME_TYPE_2D)
        , _scale (VideoContentScale (Ratio::from_id ("178")))
+       , _yuv (true)
        , _fade_in (0)
        , _fade_out (0)
 {
@@ -91,6 +92,7 @@ VideoContent::VideoContent (shared_ptr<const Film> film, boost::filesystem::path
        , _video_length (0)
        , _video_frame_type (VIDEO_FRAME_TYPE_2D)
        , _scale (VideoContentScale (Ratio::from_id ("178")))
+       , _yuv (true)
        , _fade_in (0)
        , _fade_out (0)
 {
@@ -124,6 +126,9 @@ VideoContent::VideoContent (shared_ptr<const Film> film, cxml::ConstNodePtr node
        if (node->optional_node_child ("ColourConversion")) {
                _colour_conversion = ColourConversion (node->node_child ("ColourConversion"), version);
        }
+
+       _yuv = node->optional_bool_child("YUV").get_value_or (true);
+
        if (version >= 32) {
                _fade_in = node->number_child<Frame> ("FadeIn");
                _fade_out = node->number_child<Frame> ("FadeOut");
@@ -135,6 +140,7 @@ VideoContent::VideoContent (shared_ptr<const Film> film, cxml::ConstNodePtr node
 VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
        : Content (film, c)
        , _video_length (0)
+       , _yuv (false)
 {
        shared_ptr<VideoContent> ref = dynamic_pointer_cast<VideoContent> (c[0]);
        DCPOMATIC_ASSERT (ref);
@@ -171,6 +177,10 @@ VideoContent::VideoContent (shared_ptr<const Film> film, vector<shared_ptr<Conte
                }
 
                _video_length += vc->video_length ();
+
+               if (vc->yuv ()) {
+                       _yuv = true;
+               }
        }
 
        _video_size = ref->video_size ();
@@ -202,6 +212,7 @@ VideoContent::as_xml (xmlpp::Node* node) const
        if (_colour_conversion) {
                _colour_conversion.get().as_xml (node->add_child("ColourConversion"));
        }
+       node->add_child("YUV")->add_child_text (_yuv ? "1" : "0");
        node->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in));
        node->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out));
 }
@@ -222,6 +233,7 @@ VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
        optional<double> const vfr = d->video_frame_rate ();
        Frame vl = d->video_length ();
        optional<double> const ar = d->sample_aspect_ratio ();
+       bool const yuv = d->yuv ();
 
        {
                boost::mutex::scoped_lock lm (_mutex);
@@ -229,6 +241,7 @@ VideoContent::take_from_video_examiner (shared_ptr<VideoExaminer> d)
                _video_frame_rate = vfr;
                _video_length = vl;
                _sample_aspect_ratio = ar;
+               _yuv = yuv;
 
                /* Guess correct scale from size and sample aspect ratio */
                _scale = VideoContentScale (
@@ -448,6 +461,8 @@ VideoContent::scale_and_crop_to_fit_width ()
        set_scale (VideoContentScale (film()->container ()));
 
        int const crop = max (0, int (video_size().height - double (film()->frame_size().height) * video_size().width / film()->frame_size().width));
+       set_left_crop (0);
+       set_right_crop (0);
        set_top_crop (crop / 2);
        set_bottom_crop (crop / 2);
 }
@@ -460,6 +475,8 @@ VideoContent::scale_and_crop_to_fit_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);
+       set_top_crop (0);
+       set_bottom_crop (0);
 }
 
 void
@@ -477,16 +494,18 @@ VideoContent::set_video_frame_rate (double r)
        signal_changed (VideoContentProperty::VIDEO_FRAME_RATE);
 }
 
+/** @param f Frame index within the whole (untrimmed) content */
 optional<double>
 VideoContent::fade (Frame f) const
 {
        DCPOMATIC_ASSERT (f >= 0);
 
-       if (f < fade_in()) {
-               return double (f) / fade_in();
+       Frame const ts = trim_start().frames_round(video_frame_rate());
+       if ((f - ts) < fade_in()) {
+               return double (f - ts) / fade_in();
        }
 
-       Frame fade_out_start = video_length() - fade_out();
+       Frame fade_out_start = video_length() - trim_end().frames_round(video_frame_rate()) - fade_out();
        if (f >= fade_out_start) {
                return 1 - double (f - fade_out_start) / fade_out();
        }
@@ -498,7 +517,7 @@ string
 VideoContent::processing_description () const
 {
        /* stringstream is OK here as this string is just for presentation to the user */
-       stringstream d;
+       SafeStringStream d;
 
        if (video_size().width && video_size().height) {
                d << String::compose (