Merge master.
[dcpomatic.git] / src / wx / film_editor.cc
index ab1fe86ceacea094b2f6b66f8197655b578f4b58..feb49ed78a394b392f93e118ae604fe3e2948ce9 100644 (file)
@@ -40,6 +40,7 @@
 #include "lib/ffmpeg_decoder.h"
 #include "lib/imagemagick_content.h"
 #include "lib/sndfile_content.h"
+#include "lib/dcp_content_type.h"
 #include "filter_dialog.h"
 #include "wx_util.h"
 #include "film_editor.h"
@@ -48,6 +49,8 @@
 #include "dci_metadata_dialog.h"
 #include "scaler.h"
 #include "audio_dialog.h"
+#include "imagemagick_content_dialog.h"
+#include "audio_mapping_view.h"
 
 using std::string;
 using std::cout;
@@ -58,13 +61,13 @@ using std::setprecision;
 using std::list;
 using std::vector;
 using boost::shared_ptr;
+using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
 using boost::lexical_cast;
 
 /** @param f Film to edit */
 FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
        : wxPanel (parent)
-       , _film (f)
        , _generally_sensitive (true)
        , _audio_dialog (0)
 {
@@ -83,7 +86,7 @@ FilmEditor::FilmEditor (shared_ptr<Film> f, wxWindow* parent)
        make_subtitle_panel ();
        _notebook->AddPage (_subtitle_panel, _("Subtitles"), false);
 
-       set_film (_film);
+       set_film (f);
        connect_to_widgets ();
 
        JobManager::instance()->ActiveJobsChanged.connect (
@@ -143,7 +146,7 @@ FilmEditor::make_film_panel ()
        }
        ++r;
 
-       _frame_rate_description = new wxStaticText (_film_panel, wxID_ANY, wxT (" \n "), wxDefaultPosition, wxDefaultSize);
+       _frame_rate_description = new wxStaticText (_film_panel, wxID_ANY, wxT (" \n \n "), wxDefaultPosition, wxDefaultSize);
        grid->Add (_frame_rate_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
        wxFont font = _frame_rate_description->GetFont();
        font.SetStyle(wxFONTSTYLE_ITALIC);
@@ -194,9 +197,12 @@ FilmEditor::connect_to_widgets ()
        _edit_dci_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::edit_dci_button_clicked), 0, this);
        _format->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FilmEditor::format_changed), 0, this);
        _trust_content_headers->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (FilmEditor::trust_content_headers_changed), 0, this);
-       _content->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler (FilmEditor::content_item_selected), 0, this);
+       _content->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler (FilmEditor::content_selection_changed), 0, this);
+       _content->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_DESELECTED, wxListEventHandler (FilmEditor::content_selection_changed), 0, this);
+       _content->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_ACTIVATED, wxListEventHandler (FilmEditor::content_activated), 0, this);
        _content_add->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::content_add_clicked), 0, this);
        _content_remove->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::content_remove_clicked), 0, this);
+       _content_edit->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::content_edit_clicked), 0, this);
        _content_earlier->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::content_earlier_clicked), 0, this);
        _content_later->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (FilmEditor::content_later_clicked), 0, this);
        _left_crop->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (FilmEditor::left_crop_changed), 0, this);
@@ -242,14 +248,6 @@ FilmEditor::make_video_panel ()
        grid->Add (_format, wxGBPosition (r, 1));
        ++r;
 
-       _format_description = new wxStaticText (_video_panel, wxID_ANY, wxT (""), wxDefaultPosition, wxDefaultSize);
-       grid->Add (_format_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
-       wxFont font = _format_description->GetFont();
-       font.SetStyle(wxFONTSTYLE_ITALIC);
-       font.SetPointSize(font.GetPointSize() - 1);
-       _format_description->SetFont(font);
-       ++r;
-
        add_label_to_grid_bag_sizer (grid, _video_panel, _("Left crop"), wxGBPosition (r, 0));
        _left_crop = new wxSpinCtrl (_video_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1));
        grid->Add (_left_crop, wxGBPosition (r, 1));
@@ -270,6 +268,14 @@ FilmEditor::make_video_panel ()
        grid->Add (_bottom_crop, wxGBPosition (r, 1));
        ++r;
 
+       _scaling_description = new wxStaticText (_video_panel, wxID_ANY, wxT ("\n \n \n \n"), wxDefaultPosition, wxDefaultSize);
+       grid->Add (_scaling_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
+       wxFont font = _scaling_description->GetFont();
+       font.SetStyle(wxFONTSTYLE_ITALIC);
+       font.SetPointSize(font.GetPointSize() - 1);
+       _scaling_description->SetFont(font);
+       ++r;
+
        /* VIDEO-only stuff */
        {
                add_label_to_grid_bag_sizer (grid, _video_panel, _("Filters"), wxGBPosition (r, 0));
@@ -341,6 +347,8 @@ FilmEditor::make_content_panel ()
                 b->Add (_content_add);
                 _content_remove = new wxButton (_content_panel, wxID_ANY, _("Remove"));
                 b->Add (_content_remove);
+                _content_edit = new wxButton (_content_panel, wxID_ANY, _("Edit..."));
+                b->Add (_content_edit);
                 _content_earlier = new wxButton (_content_panel, wxID_ANY, _("Earlier"));
                 b->Add (_content_earlier);
                 _content_later = new wxButton (_content_panel, wxID_ANY, _("Later"));
@@ -400,6 +408,9 @@ FilmEditor::make_audio_panel ()
                 grid->Add (s, 1, wxEXPAND);
         }
 
+       _audio_mapping = new AudioMappingView (_audio_panel);
+       _audio_sizer->Add (_audio_mapping, 1, wxEXPAND | wxALL, 6);
+       
        _audio_gain->SetRange (-60, 60);
        _audio_delay->SetRange (-1000, 1000);
 }
@@ -419,9 +430,14 @@ FilmEditor::make_subtitle_panel ()
        _ffmpeg_subtitle_stream = new wxChoice (_subtitle_panel, wxID_ANY);
        grid->Add (_ffmpeg_subtitle_stream);
 
-       add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Offset"));
-       _subtitle_offset = new wxSpinCtrl (_subtitle_panel);
-       grid->Add (_subtitle_offset, 1);
+       {
+               add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Offset"));
+               wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
+               _subtitle_offset = new wxSpinCtrl (_subtitle_panel);
+               s->Add (_subtitle_offset);
+               add_label_to_sizer (s, _subtitle_panel, _("pixels"));
+               grid->Add (s);
+       }
 
        {
                add_label_to_sizer (grid, _subtitle_panel, _("Subtitle Scale"));
@@ -592,7 +608,6 @@ FilmEditor::film_changed (Film::Property p)
                setup_subtitle_control_sensitivity ();
                setup_streams ();
                setup_show_audio_sensitivity ();
-               setup_length ();
                break;
        case Film::TRUST_CONTENT_HEADERS:
                checked_set (_trust_content_headers, _film->trust_content_headers ());
@@ -605,6 +620,7 @@ FilmEditor::film_changed (Film::Property p)
                checked_set (_right_crop, _film->crop().right);
                checked_set (_top_crop, _film->crop().top);
                checked_set (_bottom_crop, _film->crop().bottom);
+               setup_scaling_description ();
                break;
        case Film::FILTERS:
        {
@@ -679,17 +695,20 @@ FilmEditor::film_changed (Film::Property p)
                }
 
                if (_film->video_frame_rate()) {
-                       _frame_rate_description->SetLabel (std_to_wx (FrameRateConversion (_film->video_frame_rate(), _film->dcp_frame_rate()).description));
                        _best_dcp_frame_rate->Enable (best_dcp_frame_rate (_film->video_frame_rate ()) != _film->dcp_frame_rate ());
                } else {
-                       _frame_rate_description->SetLabel (wxT (""));
                        _best_dcp_frame_rate->Disable ();
                }
+               setup_frame_rate_description ();
+               break;
+       case Film::AUDIO_MAPPING:
+               _audio_mapping->set_mapping (_film->audio_mapping ());
+               break;
        }
 }
 
 void
-FilmEditor::film_content_changed (int p)
+FilmEditor::film_content_changed (weak_ptr<Content> content, int property)
 {
        if (!_film) {
                /* We call this method ourselves (as well as using it as a signal handler)
@@ -698,23 +717,26 @@ FilmEditor::film_content_changed (int p)
                return;
        }
                
-       if (p == FFmpegContentProperty::SUBTITLE_STREAMS) {
+       if (property == FFmpegContentProperty::SUBTITLE_STREAMS) {
                setup_subtitle_control_sensitivity ();
                setup_streams ();
-       } else if (p == FFmpegContentProperty::AUDIO_STREAMS) {
+       } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
                setup_streams ();
                setup_show_audio_sensitivity ();
-       } else if (p == VideoContentProperty::VIDEO_LENGTH) {
+       } else if (property == VideoContentProperty::VIDEO_LENGTH) {
                setup_length ();
-               setup_content_information ();
-       } else if (p == FFmpegContentProperty::AUDIO_STREAM) {
+               boost::shared_ptr<Content> c = content.lock ();
+               if (c && c == selected_content()) {
+                       setup_content_information ();
+               }
+       } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
                if (_film->ffmpeg_audio_stream()) {
                        checked_set (_ffmpeg_audio_stream, boost::lexical_cast<string> (_film->ffmpeg_audio_stream()->id));
                }
                setup_dcp_name ();
                setup_audio_details ();
                setup_show_audio_sensitivity ();
-       } else if (p == FFmpegContentProperty::SUBTITLE_STREAM) {
+       } else if (property == FFmpegContentProperty::SUBTITLE_STREAM) {
                if (_film->ffmpeg_subtitle_stream()) {
                        checked_set (_ffmpeg_subtitle_stream, boost::lexical_cast<string> (_film->ffmpeg_subtitle_stream()->id));
                }
@@ -730,18 +752,15 @@ FilmEditor::setup_format ()
                ++i;
                ++n;
        }
+       
        if (i == _formats.end()) {
                checked_set (_format, -1);
        } else {
                checked_set (_format, n);
        }
-       setup_dcp_name ();
        
-       if (_film->format ()) {
-               _format_description->SetLabel (std_to_wx (_film->format()->description ()));
-       } else {
-               _format_description->SetLabel (wxT (""));
-       }
+       setup_dcp_name ();
+       setup_scaling_description ();
 }      
 
 void
@@ -762,6 +781,29 @@ FilmEditor::setup_length ()
        }
 }      
 
+void
+FilmEditor::setup_frame_rate_description ()
+{
+       wxString d;
+       if (_film->video_frame_rate()) {
+               d << std_to_wx (FrameRateConversion (_film->video_frame_rate(), _film->dcp_frame_rate()).description);
+#ifdef HAVE_SWRESAMPLE
+               if (_film->audio_frame_rate() != _film->target_audio_sample_rate ()) {
+                       d << wxString::Format (
+                               _("Audio will be resampled from %dHz to %dHz\n"),
+                               _film->audio_frame_rate(),
+                               _film->target_audio_sample_rate()
+                               );
+               } else {
+                       d << "\n";
+               }
+#else
+               d << "\n";
+#endif         
+       }
+
+       _frame_rate_description->SetLabel (d);
+}
 
 /** Called when the format widget has been changed */
 void
@@ -796,13 +838,17 @@ FilmEditor::dcp_content_type_changed (wxCommandEvent &)
 void
 FilmEditor::set_film (shared_ptr<Film> f)
 {
-       _film = f;
-
        set_things_sensitive (_film != 0);
 
+       if (_film == f) {
+               return;
+       }
+       
+       _film = f;
+
        if (_film) {
                _film->Changed.connect (bind (&FilmEditor::film_changed, this, _1));
-               _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _1));
+               _film->ContentChanged.connect (bind (&FilmEditor::film_content_changed, this, _1, _2));
        }
 
        if (_film) {
@@ -836,11 +882,12 @@ FilmEditor::set_film (shared_ptr<Film> f)
        film_changed (Film::J2K_BANDWIDTH);
        film_changed (Film::DCI_METADATA);
        film_changed (Film::DCP_FRAME_RATE);
+       film_changed (Film::AUDIO_MAPPING);
 
-       film_content_changed (FFmpegContentProperty::SUBTITLE_STREAMS);
-       film_content_changed (FFmpegContentProperty::SUBTITLE_STREAM);
-       film_content_changed (FFmpegContentProperty::AUDIO_STREAMS);
-       film_content_changed (FFmpegContentProperty::AUDIO_STREAM);
+       film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::SUBTITLE_STREAMS);
+       film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::SUBTITLE_STREAM);
+       film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::AUDIO_STREAMS);
+       film_content_changed (boost::shared_ptr<Content> (), FFmpegContentProperty::AUDIO_STREAM);
 }
 
 /** Updates the sensitivity of lots of widgets to a given value.
@@ -926,6 +973,24 @@ FilmEditor::audio_delay_changed (wxCommandEvent &)
        _film->set_audio_delay (_audio_delay->GetValue ());
 }
 
+void
+FilmEditor::setup_notebook_size ()
+{
+       _notebook->InvalidateBestSize ();
+       
+       _film_sizer->Layout ();
+       _film_sizer->SetSizeHints (_film_panel);
+       _video_sizer->Layout ();
+       _video_sizer->SetSizeHints (_video_panel);
+       _audio_sizer->Layout ();
+       _audio_sizer->SetSizeHints (_audio_panel);
+       _subtitle_sizer->Layout ();
+       _subtitle_sizer->SetSizeHints (_subtitle_panel);
+
+       _notebook->Fit ();
+       Fit ();
+}
+
 void
 FilmEditor::trim_start_changed (wxCommandEvent &)
 {
@@ -1113,15 +1178,17 @@ FilmEditor::setup_audio_details ()
        if (!_film->ffmpeg_audio_stream()) {
                _audio->SetLabel (wxT (""));
        } else {
-               stringstream s;
+               wxString s;
                if (_film->audio_channels() == 1) {
-                       s << wx_to_std (_("1 channel"));
+                       s << _("1 channel");
                } else {
-                       s << _film->audio_channels() << " " << wx_to_std (_("channels"));
+                       s << _film->audio_channels() << " " << _("channels");
                }
-               s << ", " << _film->audio_frame_rate() << wx_to_std (_("Hz"));
-               _audio->SetLabel (std_to_wx (s.str ()));
+               s << ", " << _film->audio_frame_rate() << _("Hz");
+               _audio->SetLabel (s);
        }
+
+       setup_notebook_size ();
 }
 
 void
@@ -1223,44 +1290,65 @@ FilmEditor::content_add_clicked (wxCommandEvent &)
 void
 FilmEditor::content_remove_clicked (wxCommandEvent &)
 {
-       int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
-       if (s == -1) {
-               return;
+       shared_ptr<Content> c = selected_content ();
+       if (c) {
+               _film->remove_content (c);
        }
+}
 
+void
+FilmEditor::content_activated (wxListEvent& ev)
+{
        ContentList c = _film->content ();
-       assert (s >= 0 && size_t (s) < c.size ());
-       _film->remove_content (c[s]);
+       assert (ev.GetIndex() >= 0 && size_t (ev.GetIndex()) < c.size ());
+
+       edit_content (c[ev.GetIndex()]);
 }
 
 void
-FilmEditor::content_earlier_clicked (wxCommandEvent &)
+FilmEditor::content_edit_clicked (wxCommandEvent &)
 {
-       int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
-       if (s == -1) {
+       shared_ptr<Content> c = selected_content ();
+       if (!c) {
                return;
        }
 
-       ContentList c = _film->content ();
-       assert (s >= 0 && size_t (s) < c.size ());
-       _film->move_content_earlier (c[s]);
+       edit_content (c);
 }
 
 void
-FilmEditor::content_later_clicked (wxCommandEvent &)
+FilmEditor::edit_content (shared_ptr<Content> c)
 {
-       int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
-       if (s == -1) {
-               return;
+       shared_ptr<ImageMagickContent> im = dynamic_pointer_cast<ImageMagickContent> (c);
+       if (im) {
+               ImageMagickContentDialog* d = new ImageMagickContentDialog (this, im);
+               d->ShowModal ();
+               d->Destroy ();
+
+               im->set_video_length (d->video_length() * 24);
        }
+}
 
-       ContentList c = _film->content ();
-       assert (s >= 0 && size_t (s) < c.size ());
-       _film->move_content_later (c[s]);
+void
+FilmEditor::content_earlier_clicked (wxCommandEvent &)
+{
+       shared_ptr<Content> c = selected_content ();
+       if (c) {
+               _film->move_content_earlier (c);
+       }
+}
+
+void
+FilmEditor::content_later_clicked (wxCommandEvent &)
+{
+       shared_ptr<Content> c = selected_content ();
+       if (c) {
+               _film->move_content_later (c);
+       }
 }
 
 void
-FilmEditor::content_item_selected (wxListEvent &)
+FilmEditor::content_selection_changed (wxListEvent &)
 {
         setup_content_button_sensitivity ();
        setup_content_information ();
@@ -1269,15 +1357,13 @@ FilmEditor::content_item_selected (wxListEvent &)
 void
 FilmEditor::setup_content_information ()
 {
-       int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
-       if (s == -1) {
+       shared_ptr<Content> c = selected_content ();
+       if (!c) {
                _content_information->SetValue (wxT (""));
                return;
        }
 
-       ContentList c = _film->content ();
-       assert (s >= 0 && size_t (s) < c.size ());
-       _content_information->SetValue (std_to_wx (c[s]->information ()));
+       _content_information->SetValue (std_to_wx (c->information ()));
 }
 
 void
@@ -1285,8 +1371,81 @@ FilmEditor::setup_content_button_sensitivity ()
 {
         _content_add->Enable (_generally_sensitive);
 
-       bool const have_selection = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1;
-        _content_remove->Enable (have_selection && _generally_sensitive);
-        _content_earlier->Enable (have_selection && _generally_sensitive);
-        _content_later->Enable (have_selection && _generally_sensitive);
+       shared_ptr<Content> selection = selected_content ();
+
+        _content_edit->Enable (selection && _generally_sensitive && dynamic_pointer_cast<ImageMagickContent> (selection));
+        _content_remove->Enable (selection && _generally_sensitive);
+        _content_earlier->Enable (selection && _generally_sensitive);
+        _content_later->Enable (selection && _generally_sensitive);
+}
+
+shared_ptr<Content>
+FilmEditor::selected_content ()
+{
+       int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+       if (s == -1) {
+               return shared_ptr<Content> ();
+       }
+
+       ContentList c = _film->content ();
+       if (s < 0 || size_t (s) >= c.size ()) {
+               return shared_ptr<Content> ();
+       }
+       
+       return c[s];
+}
+
+void
+FilmEditor::setup_scaling_description ()
+{
+       wxString d;
+
+       int lines = 0;
+
+       d << wxString::Format (
+               _("Original video is %dx%d (%.2f:1)\n"),
+               _film->video_size().width, _film->video_size().height,
+               float (_film->video_size().width) / _film->video_size().height
+               );
+
+       ++lines;
+
+       Crop const crop = _film->crop ();
+       if (crop.left || crop.right || crop.top || crop.bottom) {
+               libdcp::Size const cropped = _film->cropped_size (_film->video_size ());
+               d << wxString::Format (
+                       _("Cropped to %dx%d (%.2f:1)\n"),
+                       cropped.width, cropped.height,
+                       float (cropped.width) / cropped.height
+                       );
+               ++lines;
+       }
+
+       Format const * format = _film->format ();
+       if (format) {
+               int const padding = format->dcp_padding (_film);
+               libdcp::Size scaled = format->dcp_size ();
+               scaled.width -= padding * 2;
+               d << wxString::Format (
+                       _("Scaled to %dx%d (%.2f:1)\n"),
+                       scaled.width, scaled.height,
+                       float (scaled.width) / scaled.height
+                       );
+               ++lines;
+
+               if (padding) {
+                       d << wxString::Format (
+                               _("Padded with black to %dx%d (%.2f:1)\n"),
+                               format->dcp_size().width, format->dcp_size().height,
+                               float (format->dcp_size().width) / format->dcp_size().height
+                               );
+                       ++lines;
+               }
+       }
+
+       for (int i = lines; i < 4; ++i) {
+               d << " \n";
+       }
+
+       _scaling_description->SetLabel (d);
 }