3b4dc7e46f60c7d5660ca5ca56e087ea5352b438
[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 "multiple_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 MultipleWidget<wxSpinCtrl> (this, new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)));
59         _left_crop->add (grid, wxGBPosition (r, 1));
60         ++r;
61
62         add_label_to_grid_bag_sizer (grid, this, _("Right crop"), true, wxGBPosition (r, 0));
63         _right_crop = new MultipleWidget<wxSpinCtrl> (this, new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)));
64         _right_crop->add (grid, wxGBPosition (r, 1));
65         ++r;
66         
67         add_label_to_grid_bag_sizer (grid, this, _("Top crop"), true, wxGBPosition (r, 0));
68         _top_crop = new MultipleWidget<wxSpinCtrl> (this, new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)));
69         _top_crop->add (grid, wxGBPosition (r,1 ));
70         ++r;
71         
72         add_label_to_grid_bag_sizer (grid, this, _("Bottom crop"), true, wxGBPosition (r, 0));
73         _bottom_crop = new MultipleWidget<wxSpinCtrl> (this, new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)));
74         _bottom_crop->add (grid, wxGBPosition (r, 1));
75         ++r;
76
77         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
78         _ratio = new wxChoice (this, wxID_ANY);
79         grid->Add (_ratio, wxGBPosition (r, 1));
80         ++r;
81
82         {
83                 add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
84                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
85
86                 wxClientDC dc (this);
87                 wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
88                 size.SetHeight (-1);
89                 
90                 _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
91                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
92                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
93                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
94                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
95         }
96         ++r;
97         
98         {
99                 add_label_to_grid_bag_sizer (grid, this, _("Colour conversion"), true, wxGBPosition (r, 0));
100                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
101
102                 wxClientDC dc (this);
103                 wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
104                 size.SetHeight (-1);
105                 
106                 _colour_conversion = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size);
107
108                 s->Add (_colour_conversion, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
109                 _colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
110                 s->Add (_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
111                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
112         }
113         ++r;
114
115         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
116         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 2), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
117         wxFont font = _description->GetFont();
118         font.SetStyle(wxFONTSTYLE_ITALIC);
119         font.SetPointSize(font.GetPointSize() - 1);
120         _description->SetFont(font);
121         ++r;
122
123         _left_crop->wrapped()->SetRange (0, 1024);
124         _top_crop->wrapped()->SetRange (0, 1024);
125         _right_crop->wrapped()->SetRange (0, 1024);
126         _bottom_crop->wrapped()->SetRange (0, 1024);
127
128         vector<Ratio const *> ratios = Ratio::all ();
129         _ratio->Clear ();
130         for (vector<Ratio const *>::iterator i = ratios.begin(); i != ratios.end(); ++i) {
131                 _ratio->Append (std_to_wx ((*i)->nickname ()));
132         }
133         _ratio->Append (_("No stretch"));
134
135         _frame_type->Append (_("2D"));
136         _frame_type->Append (_("3D left/right"));
137
138         _frame_type->Bind               (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::frame_type_changed, this));
139         _left_crop->SetAllSame.connect  (boost::bind (&VideoPanel::set_left_crop_same, this));
140         _left_crop->wrapped()->Bind     (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::left_crop_changed, this));
141         _right_crop->wrapped()->Bind    (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::right_crop_changed, this));
142         _top_crop->wrapped()->Bind      (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::top_crop_changed, this));
143         _bottom_crop->wrapped()->Bind   (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&VideoPanel::bottom_crop_changed, this));
144         _ratio->Bind                    (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::ratio_changed, this));
145         _filters_button->Bind           (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
146         _colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
147 }
148
149
150 /** Called when the left crop widget has been changed */
151 void
152 VideoPanel::left_crop_changed ()
153 {
154         VideoContentList c = _editor->selected_video_content ();
155         for (VideoContentList::iterator i = c.begin(); i != c.end(); ++i) {
156                 (*i)->set_left_crop (_left_crop->wrapped()->GetValue ());
157         }
158 }
159
160 /** Called when the right crop widget has been changed */
161 void
162 VideoPanel::right_crop_changed ()
163 {
164         VideoContentList c = _editor->selected_video_content ();
165         for (VideoContentList::iterator i = c.begin(); i != c.end(); ++i) {
166                 (*i)->set_right_crop (_right_crop->wrapped()->GetValue ());
167         }
168 }
169
170 /** Called when the top crop widget has been changed */
171 void
172 VideoPanel::top_crop_changed ()
173 {
174         VideoContentList c = _editor->selected_video_content ();
175         for (VideoContentList::iterator i = c.begin(); i != c.end(); ++i) {
176                 (*i)->set_top_crop (_top_crop->wrapped()->GetValue ());
177         }
178 }
179
180 /** Called when the bottom crop value has been changed */
181 void
182 VideoPanel::bottom_crop_changed ()
183 {
184         VideoContentList c = _editor->selected_video_content ();
185         for (VideoContentList::iterator i = c.begin(); i != c.end(); ++i) {
186                 (*i)->set_bottom_crop (_bottom_crop->wrapped()->GetValue ());
187         }
188 }
189
190 void
191 VideoPanel::film_changed (Film::Property property)
192 {
193         switch (property) {
194         case Film::CONTAINER:
195         case Film::VIDEO_FRAME_RATE:
196                 setup_description ();
197                 break;
198         default:
199                 break;
200         }
201 }
202
203 void
204 VideoPanel::film_content_changed (int property)
205 {
206         VideoContentList vc = _editor->selected_video_content ();
207         shared_ptr<VideoContent> vcs;
208         shared_ptr<FFmpegContent> fcs;
209         if (!vc.empty ()) {
210                 vcs = vc.front ();
211                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
212         }
213         
214         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
215                 checked_set (_frame_type, vcs ? vcs->video_frame_type () : VIDEO_FRAME_TYPE_2D);
216                 setup_description ();
217         } else if (property == VideoContentProperty::VIDEO_CROP) {
218                 set_multiple<VideoContent> (vc, _left_crop, &VideoContent::left_crop);
219                 set_multiple<VideoContent> (vc, _right_crop, &VideoContent::right_crop);
220                 set_multiple<VideoContent> (vc, _top_crop, &VideoContent::top_crop);
221                 set_multiple<VideoContent> (vc, _bottom_crop, &VideoContent::bottom_crop);
222                 setup_description ();
223         } else if (property == VideoContentProperty::VIDEO_RATIO) {
224                 if (vcs) {
225                         int n = 0;
226                         vector<Ratio const *> ratios = Ratio::all ();
227                         vector<Ratio const *>::iterator i = ratios.begin ();
228                         while (i != ratios.end() && *i != vcs->ratio()) {
229                                 ++i;
230                                 ++n;
231                         }
232
233                         if (i == ratios.end()) {
234                                 checked_set (_ratio, ratios.size ());
235                         } else {
236                                 checked_set (_ratio, n);
237                         }
238                 } else {
239                         checked_set (_ratio, -1);
240                 }
241                 setup_description ();
242         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
243                 setup_description ();
244         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
245                 optional<size_t> preset = vcs ? vcs->colour_conversion().preset () : optional<size_t> ();
246                 vector<PresetColourConversion> cc = Config::instance()->colour_conversions ();
247                 _colour_conversion->SetLabel (preset ? std_to_wx (cc[preset.get()].name) : _("Custom"));
248         } else if (property == FFmpegContentProperty::FILTERS) {
249                 if (fcs) {
250                         pair<string, string> p = Filter::ffmpeg_strings (fcs->filters ());
251                         if (p.first.empty () && p.second.empty ()) {
252                                 _filters->SetLabel (_("None"));
253                         } else {
254                                 string const b = p.first + " " + p.second;
255                                 _filters->SetLabel (std_to_wx (b));
256                         }
257                 }
258         }
259 }
260
261 /** Called when the `Edit filters' button has been clicked */
262 void
263 VideoPanel::edit_filters_clicked ()
264 {
265         FFmpegContentList c = _editor->selected_ffmpeg_content ();
266         if (c.size() != 1) {
267                 return;
268         }
269
270         FilterDialog* d = new FilterDialog (this, c.front()->filters());
271         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
272         d->ShowModal ();
273         d->Destroy ();
274 }
275
276 void
277 VideoPanel::setup_description ()
278 {
279         FFmpegContentList vc = _editor->selected_ffmpeg_content ();
280         if (vc.empty ()) {
281                 _description->SetLabel ("");
282                 return;
283         } else if (vc.size() > 1) {
284                 _description->SetLabel (_("Multiple content selected"));
285                 return;
286         }
287
288         shared_ptr<FFmpegContent> vcs = vc.front ();
289
290         wxString d;
291
292         int lines = 0;
293
294         if (vcs->video_size().width && vcs->video_size().height) {
295                 d << wxString::Format (
296                         _("Content video is %dx%d (%.2f:1)\n"),
297                         vcs->video_size_after_3d_split().width,
298                         vcs->video_size_after_3d_split().height,
299                         vcs->video_size_after_3d_split().ratio ()
300                         );
301                 ++lines;
302         }
303
304         Crop const crop = vcs->crop ();
305         if ((crop.left || crop.right || crop.top || crop.bottom) && vcs->video_size() != libdcp::Size (0, 0)) {
306                 libdcp::Size cropped = vcs->video_size_after_crop ();
307                 d << wxString::Format (
308                         _("Cropped to %dx%d (%.2f:1)\n"),
309                         cropped.width, cropped.height,
310                         cropped.ratio ()
311                         );
312                 ++lines;
313         }
314
315         Ratio const * ratio = vcs->ratio ();
316         libdcp::Size container_size = fit_ratio_within (_editor->film()->container()->ratio (), _editor->film()->full_frame ());
317         float const ratio_value = ratio ? ratio->ratio() : vcs->video_size_after_crop().ratio ();
318
319         /* We have a specified ratio to scale to */
320         libdcp::Size const scaled = fit_ratio_within (ratio_value, container_size);
321         
322         d << wxString::Format (
323                 _("Scaled to %dx%d (%.2f:1)\n"),
324                 scaled.width, scaled.height,
325                 scaled.ratio ()
326                 );
327         ++lines;
328         
329         if (scaled != container_size) {
330                 d << wxString::Format (
331                         _("Padded with black to %dx%d (%.2f:1)\n"),
332                         container_size.width, container_size.height,
333                         container_size.ratio ()
334                         );
335                 ++lines;
336         }
337
338         d << wxString::Format (_("Content frame rate %.4f\n"), vcs->video_frame_rate ());
339         ++lines;
340         FrameRateConversion frc (vcs->video_frame_rate(), _editor->film()->video_frame_rate ());
341         d << frc.description << "\n";
342         ++lines;
343
344         for (int i = lines; i < 6; ++i) {
345                 d << wxT ("\n ");
346         }
347
348         _description->SetLabel (d);
349         _sizer->Layout ();
350 }
351
352
353 void
354 VideoPanel::ratio_changed ()
355 {
356         if (!_editor->film ()) {
357                 return;
358         }
359
360         VideoContentList vc = _editor->selected_video_content ();
361         if (vc.size() != 1) {
362                 return;
363         }
364         
365         int const n = _ratio->GetSelection ();
366         if (n >= 0) {
367                 vector<Ratio const *> ratios = Ratio::all ();
368                 if (n < int (ratios.size ())) {
369                         vc.front()->set_ratio (ratios[n]);
370                 } else {
371                         vc.front()->set_ratio (0);
372                 }
373         }
374 }
375
376 void
377 VideoPanel::frame_type_changed ()
378 {
379         VideoContentList vc = _editor->selected_video_content ();
380         if (vc.size() == 1) {
381                 vc.front()->set_video_frame_type (static_cast<VideoFrameType> (_frame_type->GetSelection ()));
382         }
383 }
384
385 void
386 VideoPanel::edit_colour_conversion_clicked ()
387 {
388         VideoContentList vc = _editor->selected_video_content ();
389         if (vc.size() != 1) {
390                 return;
391         }
392
393         ColourConversion conversion = vc.front()->colour_conversion ();
394         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
395         d->set (conversion);
396         d->ShowModal ();
397
398         vc.front()->set_colour_conversion (d->get ());
399         d->Destroy ();
400 }
401
402 void
403 VideoPanel::content_selection_changed ()
404 {
405         VideoContentList sel = _editor->selected_video_content ();
406         bool const single = sel.size() == 1;
407         bool const multiple = sel.size() > 1;
408
409         /* Things that are allowed with multiple selections */
410         _left_crop->wrapped()->Enable   (single || multiple);
411         _right_crop->wrapped()->Enable  (single || multiple);
412         _top_crop->wrapped()->Enable    (single || multiple);
413         _bottom_crop->wrapped()->Enable (single || multiple);
414
415         /* Things that are only allowed with single selections */
416         _frame_type->Enable (single);
417         _ratio->Enable (single);
418         _filters_button->Enable (single);
419         _colour_conversion_button->Enable (single);
420
421         film_content_changed (VideoContentProperty::VIDEO_FRAME_TYPE);
422         film_content_changed (VideoContentProperty::VIDEO_CROP);
423         film_content_changed (VideoContentProperty::VIDEO_RATIO);
424         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
425         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
426         film_content_changed (FFmpegContentProperty::FILTERS);
427 }
428
429 void
430 VideoPanel::set_left_crop_same ()
431 {
432         VideoContentList sel = _editor->selected_video_content ();
433         for (VideoContentList::iterator i = sel.begin(); i != sel.end(); ++i) {
434                 (*i)->set_left_crop (sel.front()->left_crop ());
435         }
436 }
437