ca1fd1ce8702c3d78154f1cd35ccdeb4eef75e4b
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <wx/spinctrl.h>
21 #include "lib/ratio.h"
22 #include "lib/filter.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/colour_conversion.h"
25 #include "lib/config.h"
26 #include "lib/util.h"
27 #include "filter_dialog.h"
28 #include "video_panel.h"
29 #include "wx_util.h"
30 #include "film_editor.h"
31 #include "content_colour_conversion_dialog.h"
32 #include "content_widget.h"
33
34 using std::vector;
35 using std::string;
36 using std::pair;
37 using std::cout;
38 using std::list;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::bind;
42 using boost::optional;
43
44 VideoPanel::VideoPanel (FilmEditor* e)
45         : FilmEditorPanel (e, _("Video"))
46 {
47         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
48         _sizer->Add (grid, 0, wxALL, 8);
49
50         int r = 0;
51
52         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
53         _frame_type = new wxChoice (this, wxID_ANY);
54         grid->Add (_frame_type, wxGBPosition (r, 1));
55         ++r;
56         
57         add_label_to_grid_bag_sizer (grid, this, _("Left crop"), true, wxGBPosition (r, 0));
58         _left_crop = new ContentWidget<VideoContent, wxSpinCtrl> (
59                 this,
60                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
61                 VideoContentProperty::VIDEO_CROP,
62                 boost::mem_fn (&VideoContent::left_crop),
63                 boost::mem_fn (&VideoContent::set_left_crop)
64                 );
65         _left_crop->add (grid, wxGBPosition (r, 1));
66         ++r;
67
68         add_label_to_grid_bag_sizer (grid, this, _("Right crop"), true, wxGBPosition (r, 0));
69         _right_crop = new ContentWidget<VideoContent, wxSpinCtrl> (
70                 this,
71                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
72                 VideoContentProperty::VIDEO_CROP,
73                 boost::mem_fn (&VideoContent::right_crop),
74                 boost::mem_fn (&VideoContent::set_right_crop)
75                 );
76         _right_crop->add (grid, wxGBPosition (r, 1));
77         ++r;
78         
79         add_label_to_grid_bag_sizer (grid, this, _("Top crop"), true, wxGBPosition (r, 0));
80         _top_crop = new ContentWidget<VideoContent, wxSpinCtrl> (
81                 this,
82                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
83                 VideoContentProperty::VIDEO_CROP,
84                 boost::mem_fn (&VideoContent::top_crop),
85                 boost::mem_fn (&VideoContent::set_top_crop)
86                 );
87         _top_crop->add (grid, wxGBPosition (r,1 ));
88         ++r;
89         
90         add_label_to_grid_bag_sizer (grid, this, _("Bottom crop"), true, wxGBPosition (r, 0));
91         _bottom_crop = new ContentWidget<VideoContent, wxSpinCtrl> (
92                 this,
93                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
94                 VideoContentProperty::VIDEO_CROP,
95                 boost::mem_fn (&VideoContent::bottom_crop),
96                 boost::mem_fn (&VideoContent::set_bottom_crop)
97                 );
98         _bottom_crop->add (grid, wxGBPosition (r, 1));
99         ++r;
100
101         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
102         _ratio = new wxChoice (this, wxID_ANY);
103         grid->Add (_ratio, wxGBPosition (r, 1));
104         ++r;
105
106         {
107                 add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
108                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
109
110                 wxClientDC dc (this);
111                 wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
112                 size.SetHeight (-1);
113                 
114                 _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
115                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
116                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
117                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
118                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
119         }
120         ++r;
121         
122         {
123                 add_label_to_grid_bag_sizer (grid, this, _("Colour conversion"), true, wxGBPosition (r, 0));
124                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
125
126                 wxClientDC dc (this);
127                 wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
128                 size.SetHeight (-1);
129                 
130                 _colour_conversion = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size);
131
132                 s->Add (_colour_conversion, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
133                 _colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
134                 s->Add (_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
135                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
136         }
137         ++r;
138
139         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
140         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
141         wxFont font = _description->GetFont();
142         font.SetStyle(wxFONTSTYLE_ITALIC);
143         font.SetPointSize(font.GetPointSize() - 1);
144         _description->SetFont(font);
145         ++r;
146
147         _left_crop->wrapped()->SetRange (0, 1024);
148         _top_crop->wrapped()->SetRange (0, 1024);
149         _right_crop->wrapped()->SetRange (0, 1024);
150         _bottom_crop->wrapped()->SetRange (0, 1024);
151
152         vector<Ratio const *> ratios = Ratio::all ();
153         _ratio->Clear ();
154         for (vector<Ratio const *>::iterator i = ratios.begin(); i != ratios.end(); ++i) {
155                 _ratio->Append (std_to_wx ((*i)->nickname ()));
156         }
157         _ratio->Append (_("No stretch"));
158
159         _frame_type->Append (_("2D"));
160         _frame_type->Append (_("3D left/right"));
161
162         _frame_type->Bind               (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::frame_type_changed, this));
163         _ratio->Bind                    (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::ratio_changed, this));
164         _filters_button->Bind           (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
165         _colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
166 }
167
168 void
169 VideoPanel::film_changed (Film::Property property)
170 {
171         switch (property) {
172         case Film::CONTAINER:
173         case Film::VIDEO_FRAME_RATE:
174                 setup_description ();
175                 break;
176         default:
177                 break;
178         }
179 }
180
181 void
182 VideoPanel::film_content_changed (int property)
183 {
184         VideoContentList vc = _editor->selected_video_content ();
185         shared_ptr<VideoContent> vcs;
186         shared_ptr<FFmpegContent> fcs;
187         if (!vc.empty ()) {
188                 vcs = vc.front ();
189                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
190         }
191         
192         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
193                 checked_set (_frame_type, vcs ? vcs->video_frame_type () : VIDEO_FRAME_TYPE_2D);
194                 setup_description ();
195         } else if (property == VideoContentProperty::VIDEO_CROP) {
196                 setup_description ();
197         } else if (property == VideoContentProperty::VIDEO_RATIO) {
198                 if (vcs) {
199                         int n = 0;
200                         vector<Ratio const *> ratios = Ratio::all ();
201                         vector<Ratio const *>::iterator i = ratios.begin ();
202                         while (i != ratios.end() && *i != vcs->ratio()) {
203                                 ++i;
204                                 ++n;
205                         }
206
207                         if (i == ratios.end()) {
208                                 checked_set (_ratio, ratios.size ());
209                         } else {
210                                 checked_set (_ratio, n);
211                         }
212                 } else {
213                         checked_set (_ratio, -1);
214                 }
215                 setup_description ();
216         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
217                 setup_description ();
218         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
219                 optional<size_t> preset = vcs ? vcs->colour_conversion().preset () : optional<size_t> ();
220                 vector<PresetColourConversion> cc = Config::instance()->colour_conversions ();
221                 _colour_conversion->SetLabel (preset ? std_to_wx (cc[preset.get()].name) : _("Custom"));
222         } else if (property == FFmpegContentProperty::FILTERS) {
223                 if (fcs) {
224                         pair<string, string> p = Filter::ffmpeg_strings (fcs->filters ());
225                         if (p.first.empty () && p.second.empty ()) {
226                                 _filters->SetLabel (_("None"));
227                         } else {
228                                 string const b = p.first + " " + p.second;
229                                 _filters->SetLabel (std_to_wx (b));
230                         }
231                 }
232         }
233 }
234
235 /** Called when the `Edit filters' button has been clicked */
236 void
237 VideoPanel::edit_filters_clicked ()
238 {
239         FFmpegContentList c = _editor->selected_ffmpeg_content ();
240         if (c.size() != 1) {
241                 return;
242         }
243
244         FilterDialog* d = new FilterDialog (this, c.front()->filters());
245         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
246         d->ShowModal ();
247         d->Destroy ();
248 }
249
250 void
251 VideoPanel::setup_description ()
252 {
253         FFmpegContentList vc = _editor->selected_ffmpeg_content ();
254         if (vc.empty ()) {
255                 _description->SetLabel ("");
256                 return;
257         } else if (vc.size() > 1) {
258                 _description->SetLabel (_("Multiple content selected"));
259                 return;
260         }
261
262         shared_ptr<FFmpegContent> vcs = vc.front ();
263
264         wxString d;
265
266         int lines = 0;
267
268         if (vcs->video_size().width && vcs->video_size().height) {
269                 d << wxString::Format (
270                         _("Content video is %dx%d (%.2f:1)\n"),
271                         vcs->video_size_after_3d_split().width,
272                         vcs->video_size_after_3d_split().height,
273                         vcs->video_size_after_3d_split().ratio ()
274                         );
275                 ++lines;
276         }
277
278         Crop const crop = vcs->crop ();
279         if ((crop.left || crop.right || crop.top || crop.bottom) && vcs->video_size() != libdcp::Size (0, 0)) {
280                 libdcp::Size cropped = vcs->video_size_after_crop ();
281                 d << wxString::Format (
282                         _("Cropped to %dx%d (%.2f:1)\n"),
283                         cropped.width, cropped.height,
284                         cropped.ratio ()
285                         );
286                 ++lines;
287         }
288
289         Ratio const * ratio = vcs->ratio ();
290         libdcp::Size container_size = fit_ratio_within (_editor->film()->container()->ratio (), _editor->film()->full_frame ());
291         float const ratio_value = ratio ? ratio->ratio() : vcs->video_size_after_crop().ratio ();
292
293         /* We have a specified ratio to scale to */
294         libdcp::Size const scaled = fit_ratio_within (ratio_value, container_size);
295         
296         d << wxString::Format (
297                 _("Scaled to %dx%d (%.2f:1)\n"),
298                 scaled.width, scaled.height,
299                 scaled.ratio ()
300                 );
301         ++lines;
302         
303         if (scaled != container_size) {
304                 d << wxString::Format (
305                         _("Padded with black to %dx%d (%.2f:1)\n"),
306                         container_size.width, container_size.height,
307                         container_size.ratio ()
308                         );
309                 ++lines;
310         }
311
312         d << wxString::Format (_("Content frame rate %.4f\n"), vcs->video_frame_rate ());
313         ++lines;
314         FrameRateConversion frc (vcs->video_frame_rate(), _editor->film()->video_frame_rate ());
315         d << frc.description << "\n";
316         ++lines;
317
318         for (int i = lines; i < 6; ++i) {
319                 d << wxT ("\n ");
320         }
321
322         _description->SetLabel (d);
323         _sizer->Layout ();
324 }
325
326
327 void
328 VideoPanel::ratio_changed ()
329 {
330         if (!_editor->film ()) {
331                 return;
332         }
333
334         VideoContentList vc = _editor->selected_video_content ();
335         if (vc.size() != 1) {
336                 return;
337         }
338         
339         int const n = _ratio->GetSelection ();
340         if (n >= 0) {
341                 vector<Ratio const *> ratios = Ratio::all ();
342                 if (n < int (ratios.size ())) {
343                         vc.front()->set_ratio (ratios[n]);
344                 } else {
345                         vc.front()->set_ratio (0);
346                 }
347         }
348 }
349
350 void
351 VideoPanel::frame_type_changed ()
352 {
353         VideoContentList vc = _editor->selected_video_content ();
354         if (vc.size() == 1) {
355                 vc.front()->set_video_frame_type (static_cast<VideoFrameType> (_frame_type->GetSelection ()));
356         }
357 }
358
359 void
360 VideoPanel::edit_colour_conversion_clicked ()
361 {
362         VideoContentList vc = _editor->selected_video_content ();
363         if (vc.size() != 1) {
364                 return;
365         }
366
367         ColourConversion conversion = vc.front()->colour_conversion ();
368         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
369         d->set (conversion);
370         d->ShowModal ();
371
372         vc.front()->set_colour_conversion (d->get ());
373         d->Destroy ();
374 }
375
376 void
377 VideoPanel::content_selection_changed ()
378 {
379         VideoContentList sel = _editor->selected_video_content ();
380         bool const single = sel.size() == 1;
381
382         _left_crop->set_content (sel);
383         _right_crop->set_content (sel);
384         _top_crop->set_content (sel);
385         _bottom_crop->set_content (sel);
386
387         /* Things that are only allowed with single selections */
388         _frame_type->Enable (single);
389         _ratio->Enable (single);
390         _filters_button->Enable (single);
391         _colour_conversion_button->Enable (single);
392
393         film_content_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
394         film_content_changed (VideoContentProperty::VIDEO_CROP);
395         film_content_changed (VideoContentProperty::VIDEO_RATIO);
396         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
397         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
398         film_content_changed (FFmpegContentProperty::FILTERS);
399 }