Merge master.
authorCarl Hetherington <cth@carlh.net>
Fri, 12 Sep 2014 22:16:31 +0000 (23:16 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 12 Sep 2014 22:16:31 +0000 (23:16 +0100)
ChangeLog
src/lib/film.cc
src/lib/player.cc
src/lib/subtitle.cc [new file with mode: 0644]
src/lib/subtitle_content.cc
src/lib/subtitle_content.h
src/wx/subtitle_panel.cc
src/wx/subtitle_panel.h

index 183339f61c1b83ecbc57ae5ffb8d42dc0fa66502..222a0eea15acdcf95e8a3407f9fbac80f21af7b8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -13,6 +13,8 @@
        * Add option to re-make signing chain with specified organisation,
        common names etc. (#354)
 
+       * Allow separate X and Y scale for subtitles (#337).
+
 2014-09-10  Carl Hetherington  <cth@carlh.net>
 
        * Allow DCP names to be created using the ISDCF template and then
index 475dd68448628afba155ac59981b133f1b1e84f7..26810992175d8da91065dfc4052d7da424afa0a2 100644 (file)
@@ -92,6 +92,8 @@ using dcp::raw_convert;
  * Use <Scale> tag in <VideoContent> rather than <Ratio>.
  * 8 -> 9
  * DCI -> ISDCF
+ * 9 -> 10
+ * Subtitle X and Y scale.
  *
  * Bumped to 32 for 2.0 branch; some times are expressed in Times rather
  * than frames now.
index e46d539f872c90fb1e130cdcd45049f77ddc169b..f83c9563b29ce80cab262ad02c22c913219cf835 100644 (file)
@@ -201,7 +201,8 @@ Player::content_changed (weak_ptr<Content> w, int property, bool frequent)
                property == SubtitleContentProperty::USE_SUBTITLES ||
                property == SubtitleContentProperty::SUBTITLE_X_OFFSET ||
                property == SubtitleContentProperty::SUBTITLE_Y_OFFSET ||
-               property == SubtitleContentProperty::SUBTITLE_SCALE ||
+               property == SubtitleContentProperty::SUBTITLE_X_SCALE ||
+               property == SubtitleContentProperty::SUBTITLE_Y_SCALE ||
                property == VideoContentProperty::VIDEO_CROP ||
                property == VideoContentProperty::VIDEO_SCALE ||
                property == VideoContentProperty::VIDEO_FRAME_RATE
@@ -260,8 +261,8 @@ Player::transform_image_subtitles (list<ImageSubtitle> subs) const
                 *     rect.x * _video_container_size.width and rect.y * _video_container_size.height.
                 *
                 * 2.  that to shift the origin of the scale by subtitle_scale to the centre of the subtitle; this will be
-                *     (width_before_subtitle_scale * (1 - subtitle_scale) / 2) and
-                *     (height_before_subtitle_scale * (1 - subtitle_scale) / 2).
+                *     (width_before_subtitle_scale * (1 - subtitle_x_scale) / 2) and
+                *     (height_before_subtitle_scale * (1 - subtitle_y_scale) / 2).
                 *
                 * Combining these two translations gives these expressions.
                 */
@@ -558,12 +559,12 @@ Player::get_subtitles (DCPTime time, DCPTime length, bool starting)
                        i->sub.rectangle.y += subtitle_content->subtitle_y_offset ();
 
                        /* Apply content's subtitle scale */
-                       i->sub.rectangle.width *= subtitle_content->subtitle_scale ();
-                       i->sub.rectangle.height *= subtitle_content->subtitle_scale ();
+                       i->sub.rectangle.width *= subtitle_content->subtitle_x_scale ();
+                       i->sub.rectangle.height *= subtitle_content->subtitle_y_scale ();
 
                        /* Apply a corrective translation to keep the subtitle centred after that scale */
-                       i->sub.rectangle.x -= i->sub.rectangle.width * (subtitle_content->subtitle_scale() - 1);
-                       i->sub.rectangle.y -= i->sub.rectangle.height * (subtitle_content->subtitle_scale() - 1);
+                       i->sub.rectangle.x -= i->sub.rectangle.width * (subtitle_content->subtitle_x_scale() - 1);
+                       i->sub.rectangle.y -= i->sub.rectangle.height * (subtitle_content->subtitle_y_scale() - 1);
                        
                        ps.image.push_back (i->sub);
                }
diff --git a/src/lib/subtitle.cc b/src/lib/subtitle.cc
new file mode 100644 (file)
index 0000000..03d4ccf
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+    Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "subtitle.h"
+#include "subtitle_content.h"
+#include "piece.h"
+#include "image.h"
+#include "scaler.h"
+#include "film.h"
+
+using boost::shared_ptr;
+using boost::dynamic_pointer_cast;
+using boost::weak_ptr;
+
+Subtitle::Subtitle (shared_ptr<const Film> film, libdcp::Size video_container_size, weak_ptr<Piece> weak_piece, shared_ptr<Image> image, dcpomatic::Rect<double> rect, Time from, Time to)
+       : _piece (weak_piece)
+       , _in_image (image)
+       , _in_rect (rect)
+       , _in_from (from)
+       , _in_to (to)
+{
+       update (film, video_container_size);
+}
+
+void
+Subtitle::update (shared_ptr<const Film> film, libdcp::Size video_container_size)
+{
+       shared_ptr<Piece> piece = _piece.lock ();
+       if (!piece) {
+               return;
+       }
+
+       if (!_in_image) {
+               _out_image.reset ();
+               return;
+       }
+
+       shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (piece->content);
+       assert (sc);
+
+       dcpomatic::Rect<double> in_rect = _in_rect;
+       libdcp::Size scaled_size;
+
+       in_rect.x += sc->subtitle_x_offset ();
+       in_rect.y += sc->subtitle_y_offset ();
+
+       /* We will scale the subtitle up to fit _video_container_size, and also by the additional subtitle scale */
+       scaled_size.width = in_rect.width * video_container_size.width * sc->subtitle_x_scale ();
+       scaled_size.height = in_rect.height * video_container_size.height * sc->subtitle_y_scale ();
+
+       /* Then we need a corrective translation, consisting of two parts:
+        *
+        * 1.  that which is the result of the scaling of the subtitle by _video_container_size; this will be
+        *     rect.x * _video_container_size.width and rect.y * _video_container_size.height.
+        *
+        * 2.  that to shift the origin of the scale by subtitle_scale to the centre of the subtitle; this will be
+        *     (width_before_subtitle_scale * (1 - subtitle_x_scale) / 2) and
+        *     (height_before_subtitle_scale * (1 - subtitle_y_scale) / 2).
+        *
+        * Combining these two translations gives these expressions.
+        */
+       
+       _out_position.x = rint (video_container_size.width * (in_rect.x + (in_rect.width * (1 - sc->subtitle_x_scale ()) / 2)));
+       _out_position.y = rint (video_container_size.height * (in_rect.y + (in_rect.height * (1 - sc->subtitle_y_scale ()) / 2)));
+       
+       _out_image = _in_image->scale (
+               scaled_size,
+               Scaler::from_id ("bicubic"),
+               _in_image->pixel_format (),
+               true
+               );
+
+       /* XXX: hack */
+       Time from = _in_from;
+       Time to = _in_to;
+       shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (piece->content);
+       if (vc) {
+               from = rint (from * vc->video_frame_rate() / film->video_frame_rate());
+               to = rint (to * vc->video_frame_rate() / film->video_frame_rate());
+       }
+       
+       _out_from = from + piece->content->position ();
+       _out_to = to + piece->content->position ();
+
+       check_out_to ();
+}
+
+bool
+Subtitle::covers (Time t) const
+{
+       return _out_from <= t && t <= _out_to;
+}
+
+void
+Subtitle::check_out_to ()
+{
+       if (_stop && _out_to > _stop.get ()) {
+               _out_to = _stop.get ();
+       }
+}
index 0e56619455b09613a7ada5229d0b932a2fe72e11..5b370847ba08d2f2f156301f5864982f7820efae 100644 (file)
@@ -35,15 +35,17 @@ using dcp::raw_convert;
 
 int const SubtitleContentProperty::SUBTITLE_X_OFFSET = 500;
 int const SubtitleContentProperty::SUBTITLE_Y_OFFSET = 501;
-int const SubtitleContentProperty::SUBTITLE_SCALE = 502;
-int const SubtitleContentProperty::USE_SUBTITLES = 503;
+int const SubtitleContentProperty::SUBTITLE_X_SCALE = 502;
+int const SubtitleContentProperty::SUBTITLE_Y_SCALE = 503;
+int const SubtitleContentProperty::USE_SUBTITLES = 504;
 
 SubtitleContent::SubtitleContent (shared_ptr<const Film> f)
        : Content (f)
        , _use_subtitles (false)
        , _subtitle_x_offset (0)
        , _subtitle_y_offset (0)
-       , _subtitle_scale (1)
+       , _subtitle_x_scale (1)
+       , _subtitle_y_scale (1)
 {
 
 }
@@ -53,7 +55,8 @@ SubtitleContent::SubtitleContent (shared_ptr<const Film> f, boost::filesystem::p
        , _use_subtitles (false)
        , _subtitle_x_offset (0)
        , _subtitle_y_offset (0)
-       , _subtitle_scale (1)
+       , _subtitle_x_scale (1)
+       , _subtitle_y_scale (1)
 {
 
 }
@@ -63,7 +66,8 @@ SubtitleContent::SubtitleContent (shared_ptr<const Film> f, cxml::ConstNodePtr n
        , _use_subtitles (false)
        , _subtitle_x_offset (0)
        , _subtitle_y_offset (0)
-       , _subtitle_scale (1)
+       , _subtitle_x_scale (1)
+       , _subtitle_y_scale (1)
 {
        if (version >= 32) {
                _use_subtitles = node->bool_child ("UseSubtitles");
@@ -77,8 +81,13 @@ SubtitleContent::SubtitleContent (shared_ptr<const Film> f, cxml::ConstNodePtr n
        } else {
                _subtitle_y_offset = node->number_child<float> ("SubtitleOffset");
        }
-       
-       _subtitle_scale = node->number_child<float> ("SubtitleScale");
+
+       if (version >= 10) {
+               _subtitle_x_scale = node->number_child<float> ("SubtitleXScale");
+               _subtitle_y_scale = node->number_child<float> ("SubtitleYScale");
+       } else {
+               _subtitle_x_scale = _subtitle_y_scale = node->number_child<float> ("SubtitleScale");
+       }
 }
 
 SubtitleContent::SubtitleContent (shared_ptr<const Film> f, vector<shared_ptr<Content> > c)
@@ -102,15 +111,20 @@ SubtitleContent::SubtitleContent (shared_ptr<const Film> f, vector<shared_ptr<Co
                        throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
                }
 
-               if (sc->subtitle_scale() != ref->subtitle_scale()) {
-                       throw JoinError (_("Content to be joined must have the same subtitle scale."));
+               if (sc->subtitle_x_scale() != ref->subtitle_x_scale()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle X scale."));
+               }
+
+               if (sc->subtitle_y_scale() != ref->subtitle_y_scale()) {
+                       throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
                }
        }
 
        _use_subtitles = ref->use_subtitles ();
        _subtitle_x_offset = ref->subtitle_x_offset ();
        _subtitle_y_offset = ref->subtitle_y_offset ();
-       _subtitle_scale = ref->subtitle_scale ();
+       _subtitle_x_scale = ref->subtitle_x_scale ();
+       _subtitle_y_scale = ref->subtitle_y_scale ();
 }
 
 void
@@ -119,7 +133,8 @@ SubtitleContent::as_xml (xmlpp::Node* root) const
        root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use_subtitles));
        root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_subtitle_x_offset));
        root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_subtitle_y_offset));
-       root->add_child("SubtitleScale")->add_child_text (raw_convert<string> (_subtitle_scale));
+       root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_subtitle_x_scale));
+       root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_subtitle_y_scale));
 }
 
 void
@@ -153,13 +168,23 @@ SubtitleContent::set_subtitle_y_offset (double o)
 }
 
 void
-SubtitleContent::set_subtitle_scale (double s)
+SubtitleContent::set_subtitle_x_scale (double s)
+{
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               _subtitle_x_scale = s;
+       }
+       signal_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
+}
+
+void
+SubtitleContent::set_subtitle_y_scale (double s)
 {
        {
                boost::mutex::scoped_lock lm (_mutex);
-               _subtitle_scale = s;
+               _subtitle_y_scale = s;
        }
-       signal_changed (SubtitleContentProperty::SUBTITLE_SCALE);
+       signal_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
 }
 
 string
@@ -167,7 +192,8 @@ SubtitleContent::identifier () const
 {
        SafeStringStream s;
        s << Content::identifier()
-         << "_" << raw_convert<string> (subtitle_scale())
+         << "_" << raw_convert<string> (subtitle_x_scale())
+         << "_" << raw_convert<string> (subtitle_y_scale())
          << "_" << raw_convert<string> (subtitle_x_offset())
          << "_" << raw_convert<string> (subtitle_y_offset());
 
index 97649f4d525590a2ad1fdb333a6b789bad9087e1..c3c25232f4893ec59776d42c20f483ec5f72acc1 100644 (file)
@@ -27,7 +27,8 @@ class SubtitleContentProperty
 public:
        static int const SUBTITLE_X_OFFSET;
        static int const SUBTITLE_Y_OFFSET;
-       static int const SUBTITLE_SCALE;
+       static int const SUBTITLE_X_SCALE;
+       static int const SUBTITLE_Y_SCALE;
        static int const USE_SUBTITLES;
 };
 
@@ -53,7 +54,8 @@ public:
        void set_use_subtitles (bool);
        void set_subtitle_x_offset (double);
        void set_subtitle_y_offset (double);
-       void set_subtitle_scale (double);
+       void set_subtitle_x_scale (double);
+       void set_subtitle_y_scale (double);
 
        bool use_subtitles () const {
                boost::mutex::scoped_lock lm (_mutex);
@@ -70,9 +72,14 @@ public:
                return _subtitle_y_offset;
        }
 
-       double subtitle_scale () const {
+       double subtitle_x_scale () const {
                boost::mutex::scoped_lock lm (_mutex);
-               return _subtitle_scale;
+               return _subtitle_x_scale;
+       }
+
+       double subtitle_y_scale () const {
+               boost::mutex::scoped_lock lm (_mutex);
+               return _subtitle_y_scale;
        }
 
 private:
@@ -87,8 +94,10 @@ private:
         *  +ve is further down the frame, -ve is further up.
         */
        double _subtitle_y_offset;
-       /** scale factor to apply to subtitles */
-       double _subtitle_scale;
+       /** x scale factor to apply to subtitles */
+       double _subtitle_x_scale;
+       /** y scale factor to apply to subtitles */
+       double _subtitle_y_scale;
 };
 
 #endif
index fecc85106f6f889d5a1bd725ac10978022bae7ac..21d6f8e5ba6c46a48a57004fa625e3a584af4641 100644 (file)
@@ -67,14 +67,23 @@ SubtitlePanel::SubtitlePanel (ContentPanel* p)
        }
        
        {
-               add_label_to_sizer (grid, this, _("Scale"), true);
+               add_label_to_sizer (grid, this, _("Scale"), true);
                wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
-               _scale = new wxSpinCtrl (this);
-               s->Add (_scale);
+               _x_scale = new wxSpinCtrl (this);
+               s->Add (_x_scale);
                add_label_to_sizer (s, this, _("%"), false);
                grid->Add (s);
        }
 
+       {
+               add_label_to_sizer (grid, this, _("Y Scale"), true);
+               wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
+               _y_scale = new wxSpinCtrl (this);
+               s->Add (_y_scale);
+               add_label_to_sizer (s, this, _("%"), false);
+               grid->Add (s);
+       }
+       
        add_label_to_sizer (grid, this, _("Stream"), true);
        _stream = new wxChoice (this, wxID_ANY);
        grid->Add (_stream, 1, wxEXPAND);
@@ -84,13 +93,14 @@ SubtitlePanel::SubtitlePanel (ContentPanel* p)
        
        _x_offset->SetRange (-100, 100);
        _y_offset->SetRange (-100, 100);
-       _scale->SetRange (1, 1000);
-       _scale->SetValue (100);
+       _x_scale->SetRange (10, 1000);
+       _y_scale->SetRange (10, 1000);
 
        _use->Bind         (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&SubtitlePanel::use_toggled, this));
        _x_offset->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_offset_changed, this));
        _y_offset->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_offset_changed, this));
-       _scale->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::scale_changed, this));
+       _x_scale->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::x_scale_changed, this));
+       _y_scale->Bind       (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&SubtitlePanel::y_scale_changed, this));
        _stream->Bind      (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&SubtitlePanel::stream_changed, this));
        _view_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&SubtitlePanel::view_clicked, this));
 }
@@ -141,8 +151,10 @@ SubtitlePanel::film_content_changed (int property)
                checked_set (_x_offset, scs ? (scs->subtitle_x_offset() * 100) : 0);
        } else if (property == SubtitleContentProperty::SUBTITLE_Y_OFFSET) {
                checked_set (_y_offset, scs ? (scs->subtitle_y_offset() * 100) : 0);
-       } else if (property == SubtitleContentProperty::SUBTITLE_SCALE) {
-               checked_set (_scale, scs ? (scs->subtitle_scale() * 100) : 100);
+       } else if (property == SubtitleContentProperty::SUBTITLE_X_SCALE) {
+               checked_set (_x_scale, scs ? int (rint (scs->subtitle_x_scale() * 100)) : 100);
+       } else if (property == SubtitleContentProperty::SUBTITLE_Y_SCALE) {
+               checked_set (_y_scale, scs ? int (rint (scs->subtitle_y_scale() * 100)) : 100);
        }
 }
 
@@ -184,7 +196,8 @@ SubtitlePanel::setup_sensitivity ()
        
        _x_offset->Enable (any_subs > 0 && use);
        _y_offset->Enable (any_subs > 0 && use);
-       _scale->Enable (any_subs > 0 && use);
+       _x_scale->Enable (any_subs > 0 && use);
+       _y_scale->Enable (any_subs > 0 && use);
        _stream->Enable (ffmpeg_subs == 1);
        _view_button->Enable (subrip_or_dcp_subs == 1);
 }
@@ -230,11 +243,20 @@ SubtitlePanel::y_offset_changed ()
 }
 
 void
-SubtitlePanel::scale_changed ()
+SubtitlePanel::x_scale_changed ()
+{
+       SubtitleContentList c = _parent->selected_subtitle ();
+       if (c.size() == 1) {
+               c.front()->set_subtitle_x_scale (_x_scale->GetValue() / 100.0);
+       }
+}
+
+void
+SubtitlePanel::y_scale_changed ()
 {
        SubtitleContentList c = _parent->selected_subtitle ();
        for (SubtitleContentList::iterator i = c.begin(); i != c.end(); ++i) {
-               (*i)->set_subtitle_scale (_scale->GetValue() / 100.0);
+               (*i)->set_subtitle_y_scale (_y_scale->GetValue() / 100.0);
        }
 }
 
@@ -245,7 +267,8 @@ SubtitlePanel::content_selection_changed ()
        film_content_changed (SubtitleContentProperty::USE_SUBTITLES);
        film_content_changed (SubtitleContentProperty::SUBTITLE_X_OFFSET);
        film_content_changed (SubtitleContentProperty::SUBTITLE_Y_OFFSET);
-       film_content_changed (SubtitleContentProperty::SUBTITLE_SCALE);
+       film_content_changed (SubtitleContentProperty::SUBTITLE_X_SCALE);
+       film_content_changed (SubtitleContentProperty::SUBTITLE_Y_SCALE);
 }
 
 void
index 9e60db34b23de055ff7208f797622de16ea70fa2..bcff995a0029a5ef223d5381113648aaef893fc0 100644 (file)
@@ -36,7 +36,8 @@ private:
        void use_toggled ();
        void x_offset_changed ();
        void y_offset_changed ();
-       void scale_changed ();
+       void x_scale_changed ();
+       void y_scale_changed ();
        void stream_changed ();
        void view_clicked ();
 
@@ -45,7 +46,8 @@ private:
        wxCheckBox* _use;
        wxSpinCtrl* _x_offset;
        wxSpinCtrl* _y_offset;
-       wxSpinCtrl* _scale;
+       wxSpinCtrl* _x_scale;
+       wxSpinCtrl* _y_scale;
        wxChoice* _stream;
        wxButton* _view_button;
        SubtitleView* _view;