Allow no-stretch scaling of video content.
authorCarl Hetherington <cth@carlh.net>
Tue, 15 Oct 2013 14:26:04 +0000 (15:26 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 15 Oct 2013 14:26:04 +0000 (15:26 +0100)
14 files changed:
ChangeLog
src/lib/image.cc
src/lib/player.cc
src/lib/ratio.cc
src/lib/ratio.h
src/lib/types.h
src/lib/util.cc
src/lib/util.h
src/lib/video_content.cc
src/lib/video_content.h
src/lib/writer.cc
src/wx/film_viewer.cc
src/wx/video_panel.cc
test/ratio_test.cc

index a066ce19da2d5dc3bc77e1e4fca4cc1b293f7a31..addda9eea18bc3cf5856d1eb2537b00edceb183e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2013-10-15  Carl Hetherington  <cth@carlh.net>
+
+       * Allow no-stretch scaling like in DVD-o-matic.
+
 2013-10-14  Carl Hetherington  <cth@carlh.net>
 
        * Add Rec. 709 colour conversion preset using
index 9a3aa8d45b036557fcb6f0297cc92afbf519aeb1..d56b8763ae5d2df99770ded6105686c27613c611 100644 (file)
@@ -155,10 +155,7 @@ Image::post_process (string pp, bool aligned) const
 shared_ptr<Image>
 Image::crop (Crop crop, bool aligned) const
 {
-       libdcp::Size cropped_size = size ();
-       cropped_size.width -= crop.left + crop.right;
-       cropped_size.height -= crop.top + crop.bottom;
-
+       libdcp::Size cropped_size = crop.apply (size ());
        shared_ptr<Image> out (new Image (pixel_format(), cropped_size, aligned));
 
        for (int c = 0; c < components(); ++c) {
index 70d6fa8773a445977da35edd2d2b286a042221dd..03c5dc3220333fd67ec67c81e3d422a5089b11ca 100644 (file)
@@ -103,7 +103,7 @@ Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
        _playlist_changed_connection = _playlist->Changed.connect (bind (&Player::playlist_changed, this));
        _playlist_content_changed_connection = _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2, _3));
        _film_changed_connection = _film->Changed.connect (bind (&Player::film_changed, this, _1));
-       set_video_container_size (_film->container()->size (_film->full_frame ()));
+       set_video_container_size (fit_ratio_within (_film->container()->ratio (), _film->full_frame ()));
 }
 
 void
@@ -256,7 +256,8 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
 
        work_image = work_image->crop (content->crop(), true);
 
-       libdcp::Size const image_size = content->ratio()->size (_video_container_size);
+       float const ratio = content->ratio() ? content->ratio()->ratio() : content->video_size_after_crop().ratio();
+       libdcp::Size image_size = fit_ratio_within (ratio, _video_container_size);
        
        work_image = work_image->scale (image_size, _film->scaler(), PIX_FMT_RGB24, true);
 
index 5988b34188375eade8e2c8eac218de93b90bf141..41abbb76020667df16d93e24f0aef17f8986ed9e 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <libdcp/types.h>
 #include "ratio.h"
+#include "util.h"
 
 #include "i18n.h"
 
@@ -28,19 +29,6 @@ using std::vector;
 
 vector<Ratio const *> Ratio::_ratios;
 
-libdcp::Size
-Ratio::size (libdcp::Size full_frame) const
-{
-       if (_ratio < static_cast<float>(full_frame.width) / full_frame.height) {
-               return libdcp::Size (full_frame.height * _ratio, full_frame.height);
-       } else {
-               return libdcp::Size (full_frame.width, full_frame.width / _ratio);
-       }
-
-       return libdcp::Size ();
-}
-
-
 void
 Ratio::setup_ratios ()
 {
index c331edabe8104afd54e0ab0a8895aa7cebf5ef91..f3354f1b631c06bad6eda7073c1d75a57dc64866 100644 (file)
@@ -34,8 +34,6 @@ public:
                , _dci_name (d)
        {}
 
-       libdcp::Size size (libdcp::Size) const;
-
        std::string id () const {
                return _id;
        }
index 01560ba81fdedbd9eb643a816adb002b45acaace..d4d66387d0c5c17c08183b1f04aecfe6ce2cebff 100644 (file)
@@ -87,6 +87,21 @@ struct Crop
        int top;
        /** Number of pixels to remove from the bottom */
        int bottom;
+
+       libdcp::Size apply (libdcp::Size s, int minimum = 4) const {
+               s.width -= left + right;
+               s.height -= top + bottom;
+
+               if (s.width < minimum) {
+                       s.width = minimum;
+               }
+
+               if (s.height < minimum) {
+                       s.height = minimum;
+               }
+               
+               return s;
+       }
 };
 
 extern bool operator== (Crop const & a, Crop const & b);
index b13d905bf0983a00bb132b80d7b33b860dd32c6a..1c4347233965279203a494dbde8fde06141b9ed8 100644 (file)
@@ -882,3 +882,12 @@ make_signer ()
        return shared_ptr<const libdcp::Signer> (new libdcp::Signer (chain, signer_key));
 }
 
+libdcp::Size
+fit_ratio_within (float ratio, libdcp::Size full_frame)
+{
+       if (ratio < full_frame.ratio ()) {
+               return libdcp::Size (full_frame.height * ratio, full_frame.height);
+       }
+       
+       return libdcp::Size (full_frame.width, full_frame.width / ratio);
+}
index 377b3b7858cd09d389d72d7e0d8534877666c58c..b8ea6ebec0d6798e2daf07e05b32c5975ee93262 100644 (file)
@@ -75,7 +75,8 @@ extern bool valid_image_file (boost::filesystem::path);
 extern boost::filesystem::path mo_path ();
 #endif
 extern std::string tidy_for_filename (std::string);
-boost::shared_ptr<const libdcp::Signer> make_signer ();
+extern boost::shared_ptr<const libdcp::Signer> make_signer ();
+extern libdcp::Size fit_ratio_within (float ratio, libdcp::Size);
 
 struct FrameRateConversion
 {
index af0c3e12c08022fed68969c97f817ada684f3cc4..23ef2cf89dcce74463abcbadc0bbd8d824b0905d 100644 (file)
@@ -66,6 +66,7 @@ VideoContent::VideoContent (shared_ptr<const Film> f, boost::filesystem::path p)
 
 VideoContent::VideoContent (shared_ptr<const Film> f, shared_ptr<const cxml::Node> node)
        : Content (f, node)
+       , _ratio (0)
 {
        _video_length = node->number_child<VideoContent::Frame> ("VideoLength");
        _video_size.width = node->number_child<int> ("VideoWidth");
@@ -139,7 +140,7 @@ VideoContent::information () const
                _("%1x%2 pixels (%3:1)"),
                video_size().width,
                video_size().height,
-               setprecision (3), float (video_size().width) / video_size().height
+               setprecision (3), video_size().ratio ()
                );
        
        return s.str ();
@@ -281,3 +282,10 @@ VideoContent::set_colour_conversion (ColourConversion c)
 
        signal_changed (VideoContentProperty::COLOUR_CONVERSION);
 }
+
+/** @return Video size after 3D split and crop */
+libdcp::Size
+VideoContent::video_size_after_crop () const
+{
+       return crop().apply (video_size_after_3d_split ());
+}
index 81325516d53ba547ccd2163daad318623e30760c..36920977661169b0ccb1c1fb0d758cc4794a248e 100644 (file)
@@ -87,6 +87,7 @@ public:
 
        void set_ratio (Ratio const *);
 
+       /** @return ratio to scale to, or 0 if the content's own ratio should be preserved. */
        Ratio const * ratio () const {
                boost::mutex::scoped_lock lm (_mutex);
                return _ratio;
@@ -98,6 +99,7 @@ public:
        }
 
        libdcp::Size video_size_after_3d_split () const;
+       libdcp::Size video_size_after_crop () const;
 
 protected:
        void take_from_video_examiner (boost::shared_ptr<VideoExaminer>);
index 5e8fb5b7a020bdf4be553d8bb65a005e80b5ac8f..968cd1505df60ce471eed15a5c7dd8b5f901cec1 100644 (file)
@@ -80,7 +80,7 @@ Writer::Writer (shared_ptr<const Film> f, shared_ptr<Job> j)
        }
 
        _picture_asset->set_edit_rate (_film->video_frame_rate ());
-       _picture_asset->set_size (_film->container()->size (_film->full_frame ()));
+       _picture_asset->set_size (fit_ratio_within (_film->container()->ratio(), _film->full_frame ()));
 
        if (_film->encrypted ()) {
                _picture_asset->set_key (_film->key ());
index 3ba7ee7ce8dfa20ffa05fd7ed6809365e6cc6f29..69cd276e17a9fa6ac64aef63f0bde2b3003ca9d5 100644 (file)
@@ -239,7 +239,7 @@ FilmViewer::calculate_sizes ()
 
        Ratio const * container = _film->container ();
        
-       float const panel_ratio = static_cast<float> (_panel_size.width) / _panel_size.height;
+       float const panel_ratio = _panel_size.ratio ();
        float const film_ratio = container ? container->ratio () : 1.78;
                        
        if (panel_ratio < film_ratio) {
index bb8476d63b7bbdf1d39624ac28f38b9f29ed887b..a643832e89dd53e56aee6c73a260ade51eaa819f 100644 (file)
@@ -23,6 +23,7 @@
 #include "lib/ffmpeg_content.h"
 #include "lib/colour_conversion.h"
 #include "lib/config.h"
+#include "lib/util.h"
 #include "filter_dialog.h"
 #include "video_panel.h"
 #include "wx_util.h"
@@ -128,6 +129,7 @@ VideoPanel::VideoPanel (FilmEditor* e)
        for (vector<Ratio const *>::iterator i = ratios.begin(); i != ratios.end(); ++i) {
                _ratio->Append (std_to_wx ((*i)->nickname ()));
        }
+       _ratio->Append (_("No stretch"));
 
        _frame_type->Append (_("2D"));
        _frame_type->Append (_("3D left/right"));
@@ -230,7 +232,7 @@ VideoPanel::film_content_changed (shared_ptr<Content> c, int property)
                        }
 
                        if (i == ratios.end()) {
-                               checked_set (_ratio, -1);
+                               checked_set (_ratio, ratios.size ());
                        } else {
                                checked_set (_ratio, n);
                        }
@@ -293,45 +295,45 @@ VideoPanel::setup_description ()
        if (vc->video_size().width && vc->video_size().height) {
                d << wxString::Format (
                        _("Content video is %dx%d (%.2f:1)\n"),
-                       vc->video_size_after_3d_split().width, vc->video_size_after_3d_split().height,
-                       float (vc->video_size_after_3d_split().width) / vc->video_size_after_3d_split().height
+                       vc->video_size_after_3d_split().width,
+                       vc->video_size_after_3d_split().height,
+                       vc->video_size_after_3d_split().ratio ()
                        );
                ++lines;
        }
 
        Crop const crop = vc->crop ();
        if ((crop.left || crop.right || crop.top || crop.bottom) && vc->video_size() != libdcp::Size (0, 0)) {
-               libdcp::Size cropped = vc->video_size_after_3d_split ();
-               cropped.width -= crop.left + crop.right;
-               cropped.height -= crop.top + crop.bottom;
+               libdcp::Size cropped = vc->video_size_after_crop ();
                d << wxString::Format (
                        _("Cropped to %dx%d (%.2f:1)\n"),
                        cropped.width, cropped.height,
-                       float (cropped.width) / cropped.height
+                       cropped.ratio ()
                        );
                ++lines;
        }
 
        Ratio const * ratio = vc->ratio ();
-       if (ratio) {
-               libdcp::Size container_size = _editor->film()->container()->size (_editor->film()->full_frame ());
-               
-               libdcp::Size const scaled = ratio->size (container_size);
+       libdcp::Size container_size = fit_ratio_within (_editor->film()->container()->ratio (), _editor->film()->full_frame ());
+       float const ratio_value = ratio ? ratio->ratio() : vc->video_size_after_crop().ratio ();
+
+       /* We have a specified ratio to scale to */
+       libdcp::Size const scaled = fit_ratio_within (ratio_value, container_size);
+       
+       d << wxString::Format (
+               _("Scaled to %dx%d (%.2f:1)\n"),
+               scaled.width, scaled.height,
+               scaled.ratio ()
+               );
+       ++lines;
+       
+       if (scaled != container_size) {
                d << wxString::Format (
-                       _("Scaled to %dx%d (%.2f:1)\n"),
-                       scaled.width, scaled.height,
-                       float (scaled.width) / scaled.height
+                       _("Padded with black to %dx%d (%.2f:1)\n"),
+                       container_size.width, container_size.height,
+                       container_size.ratio ()
                        );
                ++lines;
-
-               if (scaled != container_size) {
-                       d << wxString::Format (
-                               _("Padded with black to %dx%d (%.2f:1)\n"),
-                               container_size.width, container_size.height,
-                               float (container_size.width) / container_size.height
-                               );
-                       ++lines;
-               }
        }
 
        d << wxString::Format (_("Content frame rate %.4f\n"), vc->video_frame_rate ());
@@ -361,8 +363,11 @@ VideoPanel::ratio_changed ()
        int const n = _ratio->GetSelection ();
        if (n >= 0) {
                vector<Ratio const *> ratios = Ratio::all ();
-               assert (n < int (ratios.size()));
-               vc->set_ratio (ratios[n]);
+               if (n < int (ratios.size ())) {
+                       vc->set_ratio (ratios[n]);
+               } else {
+                       vc->set_ratio (0);
+               }
        }
 }
 
index 11517ed1189786f7688dcbfd7a5491c71bbf9f17..1a39040fbe2079bf61def7282b0c9d1b8f9ee8cb 100644 (file)
@@ -21,6 +21,7 @@
 #include <boost/test/unit_test.hpp>
 #include <libdcp/util.h>
 #include "lib/ratio.h"
+#include "lib/util.h"
 
 using std::ostream;
 
@@ -41,38 +42,38 @@ BOOST_AUTO_TEST_CASE (ratio_test)
 
        Ratio const * r = Ratio::from_id ("119");
        BOOST_CHECK (r);
-       BOOST_CHECK_EQUAL (r->size(libdcp::Size (2048, 1080)), libdcp::Size (1285, 1080));
+       BOOST_CHECK_EQUAL (fit_ratio_within (r->ratio(), libdcp::Size (2048, 1080)), libdcp::Size (1285, 1080));
 
        r = Ratio::from_id ("133");
        BOOST_CHECK (r);
-       BOOST_CHECK_EQUAL (r->size(libdcp::Size (2048, 1080)), libdcp::Size (1436, 1080));
+       BOOST_CHECK_EQUAL (fit_ratio_within (r->ratio(), libdcp::Size (2048, 1080)), libdcp::Size (1436, 1080));
 
        r = Ratio::from_id ("137");
        BOOST_CHECK (r);
-       BOOST_CHECK_EQUAL (r->size(libdcp::Size (2048, 1080)), libdcp::Size (1480, 1080));
+       BOOST_CHECK_EQUAL (fit_ratio_within (r->ratio(), libdcp::Size (2048, 1080)), libdcp::Size (1480, 1080));
 
        r = Ratio::from_id ("138");
        BOOST_CHECK (r);
-       BOOST_CHECK_EQUAL (r->size(libdcp::Size (2048, 1080)), libdcp::Size (1485, 1080));
+       BOOST_CHECK_EQUAL (fit_ratio_within (r->ratio(), libdcp::Size (2048, 1080)), libdcp::Size (1485, 1080));
 
        r = Ratio::from_id ("166");
        BOOST_CHECK (r);
-       BOOST_CHECK_EQUAL (r->size(libdcp::Size (2048, 1080)), libdcp::Size (1793, 1080));
+       BOOST_CHECK_EQUAL (fit_ratio_within (r->ratio(), libdcp::Size (2048, 1080)), libdcp::Size (1793, 1080));
 
        r = Ratio::from_id ("178");
        BOOST_CHECK (r);
-       BOOST_CHECK_EQUAL (r->size(libdcp::Size (2048, 1080)), libdcp::Size (1920, 1080));
+       BOOST_CHECK_EQUAL (fit_ratio_within (r->ratio(), libdcp::Size (2048, 1080)), libdcp::Size (1920, 1080));
 
        r = Ratio::from_id ("185");
        BOOST_CHECK (r);
-       BOOST_CHECK_EQUAL (r->size(libdcp::Size (2048, 1080)), libdcp::Size (1998, 1080));
+       BOOST_CHECK_EQUAL (fit_ratio_within (r->ratio(), libdcp::Size (2048, 1080)), libdcp::Size (1998, 1080));
 
        r = Ratio::from_id ("239");
        BOOST_CHECK (r);
-       BOOST_CHECK_EQUAL (r->size(libdcp::Size (2048, 1080)), libdcp::Size (2048, 858));
+       BOOST_CHECK_EQUAL (fit_ratio_within (r->ratio(), libdcp::Size (2048, 1080)), libdcp::Size (2048, 858));
 
        r = Ratio::from_id ("full-frame");
        BOOST_CHECK (r);
-       BOOST_CHECK_EQUAL (r->size(libdcp::Size (2048, 1080)), libdcp::Size (2048, 1080));
+       BOOST_CHECK_EQUAL (fit_ratio_within (r->ratio(), libdcp::Size (2048, 1080)), libdcp::Size (2048, 1080));
 }