Front-end to set up referencing of DCPs.
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2015 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 "filter_dialog.h"
21 #include "video_panel.h"
22 #include "wx_util.h"
23 #include "content_colour_conversion_dialog.h"
24 #include "content_widget.h"
25 #include "content_panel.h"
26 #include "lib/filter.h"
27 #include "lib/ffmpeg_content.h"
28 #include "lib/colour_conversion.h"
29 #include "lib/config.h"
30 #include "lib/util.h"
31 #include "lib/ratio.h"
32 #include "lib/frame_rate_change.h"
33 #include "lib/dcp_content.h"
34 #include <wx/spinctrl.h>
35 #include <boost/foreach.hpp>
36 #include <set>
37 #include <iostream>
38
39 using std::vector;
40 using std::string;
41 using std::pair;
42 using std::cout;
43 using std::list;
44 using std::set;
45 using boost::shared_ptr;
46 using boost::dynamic_pointer_cast;
47 using boost::bind;
48 using boost::optional;
49
50 static VideoContentScale
51 index_to_scale (int n)
52 {
53         vector<VideoContentScale> scales = VideoContentScale::all ();
54         DCPOMATIC_ASSERT (n >= 0);
55         DCPOMATIC_ASSERT (n < int (scales.size ()));
56         return scales[n];
57 }
58
59 static int
60 scale_to_index (VideoContentScale scale)
61 {
62         vector<VideoContentScale> scales = VideoContentScale::all ();
63         for (size_t i = 0; i < scales.size(); ++i) {
64                 if (scales[i] == scale) {
65                         return i;
66                 }
67         }
68
69         DCPOMATIC_ASSERT (false);
70 }
71
72 VideoPanel::VideoPanel (ContentPanel* p)
73         : ContentSubPanel (p, _("Video"))
74 {
75         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
76         _sizer->Add (grid, 0, wxALL, 8);
77
78         int r = 0;
79
80         _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
81         grid->Add (_reference, wxGBPosition (r, 0), wxGBSpan (1, 2));
82         ++r;
83
84         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
85         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
86                 this,
87                 new wxChoice (this, wxID_ANY),
88                 VideoContentProperty::VIDEO_FRAME_TYPE,
89                 boost::mem_fn (&VideoContent::video_frame_type),
90                 boost::mem_fn (&VideoContent::set_video_frame_type),
91                 &caster<int, VideoFrameType>,
92                 &caster<VideoFrameType, int>
93                 );
94         _frame_type->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
95         ++r;
96
97         add_label_to_grid_bag_sizer (grid, this, _("Crop"), true, wxGBPosition (r, 0));
98
99         int cr = 0;
100         wxGridBagSizer* crop = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
101
102         add_label_to_grid_bag_sizer (crop, this, _("Left"), true, wxGBPosition (cr, 0));
103         _left_crop = new ContentSpinCtrl<VideoContent> (
104                 this,
105                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
106                 VideoContentProperty::VIDEO_CROP,
107                 boost::mem_fn (&VideoContent::left_crop),
108                 boost::mem_fn (&VideoContent::set_left_crop)
109                 );
110         _left_crop->add (crop, wxGBPosition (cr, 1));
111
112         add_label_to_grid_bag_sizer (crop, this, _("Right"), true, wxGBPosition (cr, 2));
113         _right_crop = new ContentSpinCtrl<VideoContent> (
114                 this,
115                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
116                 VideoContentProperty::VIDEO_CROP,
117                 boost::mem_fn (&VideoContent::right_crop),
118                 boost::mem_fn (&VideoContent::set_right_crop)
119                 );
120         _right_crop->add (crop, wxGBPosition (cr, 3));
121
122         ++cr;
123
124         add_label_to_grid_bag_sizer (crop, this, _("Top"), true, wxGBPosition (cr, 0));
125         _top_crop = new ContentSpinCtrl<VideoContent> (
126                 this,
127                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
128                 VideoContentProperty::VIDEO_CROP,
129                 boost::mem_fn (&VideoContent::top_crop),
130                 boost::mem_fn (&VideoContent::set_top_crop)
131                 );
132         _top_crop->add (crop, wxGBPosition (cr, 1));
133
134         add_label_to_grid_bag_sizer (crop, this, _("Bottom"), true, wxGBPosition (cr, 2));
135         _bottom_crop = new ContentSpinCtrl<VideoContent> (
136                 this,
137                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
138                 VideoContentProperty::VIDEO_CROP,
139                 boost::mem_fn (&VideoContent::bottom_crop),
140                 boost::mem_fn (&VideoContent::set_bottom_crop)
141                 );
142         _bottom_crop->add (crop, wxGBPosition (cr, 3));
143
144         grid->Add (crop, wxGBPosition (r, 1), wxGBSpan (2, 3));
145         r += 2;
146
147         add_label_to_grid_bag_sizer (grid, this, _("Fade in"), true, wxGBPosition (r, 0));
148         _fade_in = new Timecode<ContentTime> (this);
149         grid->Add (_fade_in, wxGBPosition (r, 1), wxGBSpan (1, 3));
150         ++r;
151
152         add_label_to_grid_bag_sizer (grid, this, _("Fade out"), true, wxGBPosition (r, 0));
153         _fade_out = new Timecode<ContentTime> (this);
154         grid->Add (_fade_out, wxGBPosition (r, 1), wxGBSpan (1, 3));
155         ++r;
156
157         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
158         _scale = new ContentChoice<VideoContent, VideoContentScale> (
159                 this,
160                 new wxChoice (this, wxID_ANY),
161                 VideoContentProperty::VIDEO_SCALE,
162                 boost::mem_fn (&VideoContent::scale),
163                 boost::mem_fn (&VideoContent::set_scale),
164                 &index_to_scale,
165                 &scale_to_index
166                 );
167         _scale->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
168         ++r;
169
170         wxClientDC dc (this);
171         wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
172         size.SetHeight (-1);
173
174         add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
175         {
176                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
177
178                 _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
179                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
180                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
181                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
182
183                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
184         }
185         ++r;
186
187         add_label_to_grid_bag_sizer (grid, this, _("Colour conversion"), true, wxGBPosition (r, 0));
188         {
189                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
190
191                 _colour_conversion = new wxChoice (this, wxID_ANY, wxDefaultPosition, size);
192                 _colour_conversion->Append (_("None"));
193                 BOOST_FOREACH (PresetColourConversion const & i, PresetColourConversion::all()) {
194                         _colour_conversion->Append (std_to_wx (i.name));
195                 }
196                 _colour_conversion->Append (_("Custom"));
197                 s->Add (_colour_conversion, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
198
199                 _edit_colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
200                 s->Add (_edit_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
201
202                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
203         }
204         ++r;
205
206         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
207         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 4), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
208         wxFont font = _description->GetFont();
209         font.SetStyle(wxFONTSTYLE_ITALIC);
210         font.SetPointSize(font.GetPointSize() - 1);
211         _description->SetFont(font);
212         ++r;
213
214         _left_crop->wrapped()->SetRange (0, 1024);
215         _top_crop->wrapped()->SetRange (0, 1024);
216         _right_crop->wrapped()->SetRange (0, 1024);
217         _bottom_crop->wrapped()->SetRange (0, 1024);
218
219         _scale->wrapped()->Clear ();
220         BOOST_FOREACH (VideoContentScale const & i, VideoContentScale::all ()) {
221                 _scale->wrapped()->Append (std_to_wx (i.name ()));
222         }
223
224         _frame_type->wrapped()->Append (_("2D"));
225         _frame_type->wrapped()->Append (_("3D left/right"));
226         _frame_type->wrapped()->Append (_("3D top/bottom"));
227         _frame_type->wrapped()->Append (_("3D alternate"));
228         _frame_type->wrapped()->Append (_("3D left only"));
229         _frame_type->wrapped()->Append (_("3D right only"));
230
231         _fade_in->Changed.connect (boost::bind (&VideoPanel::fade_in_changed, this));
232         _fade_out->Changed.connect (boost::bind (&VideoPanel::fade_out_changed, this));
233
234         _reference->Bind                     (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&VideoPanel::reference_clicked, this));
235         _filters_button->Bind                (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
236         _colour_conversion->Bind             (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&VideoPanel::colour_conversion_changed, this));
237         _edit_colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
238 }
239
240 void
241 VideoPanel::film_changed (Film::Property property)
242 {
243         switch (property) {
244         case Film::CONTAINER:
245         case Film::VIDEO_FRAME_RATE:
246         case Film::RESOLUTION:
247                 setup_description ();
248                 break;
249         default:
250                 break;
251         }
252 }
253
254 void
255 VideoPanel::film_content_changed (int property)
256 {
257         VideoContentList vc = _parent->selected_video ();
258         shared_ptr<VideoContent> vcs;
259         shared_ptr<FFmpegContent> fcs;
260         if (!vc.empty ()) {
261                 vcs = vc.front ();
262                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
263         }
264
265         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
266                 setup_description ();
267         } else if (property == VideoContentProperty::VIDEO_CROP) {
268                 setup_description ();
269         } else if (property == VideoContentProperty::VIDEO_SCALE) {
270                 setup_description ();
271         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
272                 setup_description ();
273         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
274                 if (!vcs) {
275                         checked_set (_colour_conversion, 0);
276                         _edit_colour_conversion_button->Enable (false);
277                 } else if (vcs->colour_conversion ()) {
278                         optional<size_t> preset = vcs->colour_conversion().get().preset ();
279                         vector<PresetColourConversion> cc = PresetColourConversion::all ();
280                         if (preset) {
281                                 checked_set (_colour_conversion, preset.get() + 1);
282                                 _edit_colour_conversion_button->Enable (false);
283                         } else {
284                                 checked_set (_colour_conversion, cc.size() + 1);
285                                 _edit_colour_conversion_button->Enable (true);
286                         }
287                 } else {
288                         checked_set (_colour_conversion, 0);
289                         _edit_colour_conversion_button->Enable (false);
290                 }
291         } else if (property == FFmpegContentProperty::FILTERS) {
292                 if (fcs) {
293                         string p = Filter::ffmpeg_string (fcs->filters ());
294                         if (p.empty ()) {
295                                 checked_set (_filters, _("None"));
296                         } else {
297                                 if (p.length() > 25) {
298                                         p = p.substr (0, 25) + "...";
299                                 }
300                                 checked_set (_filters, p);
301                         }
302                 }
303         } else if (property == VideoContentProperty::VIDEO_FADE_IN) {
304                 set<Frame> check;
305                 BOOST_FOREACH (shared_ptr<const VideoContent> i, vc) {
306                         check.insert (i->fade_in ());
307                 }
308
309                 if (check.size() == 1) {
310                         _fade_in->set (ContentTime::from_frames (vc.front()->fade_in (), vc.front()->video_frame_rate ()), vc.front()->video_frame_rate ());
311                 } else {
312                         _fade_in->clear ();
313                 }
314         } else if (property == VideoContentProperty::VIDEO_FADE_OUT) {
315                 set<Frame> check;
316                 BOOST_FOREACH (shared_ptr<const VideoContent> i, vc) {
317                         check.insert (i->fade_out ());
318                 }
319
320                 if (check.size() == 1) {
321                         _fade_out->set (ContentTime::from_frames (vc.front()->fade_out (), vc.front()->video_frame_rate ()), vc.front()->video_frame_rate ());
322                 } else {
323                         _fade_out->clear ();
324                 }
325         } else if (property == DCPContentProperty::REFERENCE_VIDEO) {
326                 if (vc.size() == 1) {
327                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (vc.front ());
328                         checked_set (_reference, dcp ? dcp->reference_video () : false);
329                 } else {
330                         checked_set (_reference, false);
331                 }
332
333                 setup_sensitivity ();
334         }
335 }
336
337 /** Called when the `Edit filters' button has been clicked */
338 void
339 VideoPanel::edit_filters_clicked ()
340 {
341         FFmpegContentList c = _parent->selected_ffmpeg ();
342         if (c.size() != 1) {
343                 return;
344         }
345
346         FilterDialog* d = new FilterDialog (this, c.front()->filters());
347         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
348         d->ShowModal ();
349         d->Destroy ();
350 }
351
352 void
353 VideoPanel::setup_description ()
354 {
355         VideoContentList vc = _parent->selected_video ();
356         if (vc.empty ()) {
357                 checked_set (_description, wxT (""));
358                 return;
359         } else if (vc.size() > 1) {
360                 checked_set (_description, _("Multiple content selected"));
361                 return;
362         }
363
364         string d = vc.front()->processing_description ();
365         size_t lines = count (d.begin(), d.end(), '\n');
366
367         for (int i = lines; i < 6; ++i) {
368                 d += "\n ";
369         }
370
371         checked_set (_description, d);
372         _sizer->Layout ();
373 }
374
375 void
376 VideoPanel::colour_conversion_changed ()
377 {
378         VideoContentList vc = _parent->selected_video ();
379         if (vc.size() != 1) {
380                 return;
381         }
382
383         int const s = _colour_conversion->GetSelection ();
384         vector<PresetColourConversion> all = PresetColourConversion::all ();
385
386         if (s == 0) {
387                 vc.front()->unset_colour_conversion ();
388         } else if (s == int (all.size() + 1)) {
389                 edit_colour_conversion_clicked ();
390         } else {
391                 vc.front()->set_colour_conversion (all[s - 1].conversion);
392         }
393 }
394
395 void
396 VideoPanel::edit_colour_conversion_clicked ()
397 {
398         VideoContentList vc = _parent->selected_video ();
399         if (vc.size() != 1) {
400                 return;
401         }
402
403         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
404         d->set (vc.front()->colour_conversion().get_value_or (PresetColourConversion::all().front ().conversion));
405         d->ShowModal ();
406         vc.front()->set_colour_conversion (d->get ());
407         d->Destroy ();
408 }
409
410 void
411 VideoPanel::content_selection_changed ()
412 {
413         VideoContentList video_sel = _parent->selected_video ();
414
415         _frame_type->set_content (video_sel);
416         _left_crop->set_content (video_sel);
417         _right_crop->set_content (video_sel);
418         _top_crop->set_content (video_sel);
419         _bottom_crop->set_content (video_sel);
420         _scale->set_content (video_sel);
421
422         film_content_changed (VideoContentProperty::VIDEO_CROP);
423         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
424         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
425         film_content_changed (VideoContentProperty::VIDEO_FADE_IN);
426         film_content_changed (VideoContentProperty::VIDEO_FADE_OUT);
427         film_content_changed (FFmpegContentProperty::FILTERS);
428         film_content_changed (DCPContentProperty::REFERENCE_VIDEO);
429
430         setup_sensitivity ();
431 }
432
433 void
434 VideoPanel::setup_sensitivity ()
435 {
436         ContentList sel = _parent->selected ();
437         _reference->Enable (sel.size() == 1 && dynamic_pointer_cast<DCPContent> (sel.front ()));
438
439         if (_reference->GetValue ()) {
440                 _frame_type->wrapped()->Enable (false);
441                 _left_crop->wrapped()->Enable (false);
442                 _right_crop->wrapped()->Enable (false);
443                 _top_crop->wrapped()->Enable (false);
444                 _bottom_crop->wrapped()->Enable (false);
445                 _fade_in->Enable (false);
446                 _fade_out->Enable (false);
447                 _scale->wrapped()->Enable (false);
448                 _description->Enable (false);
449                 _filters->Enable (false);
450                 _filters_button->Enable (false);
451                 _colour_conversion->Enable (false);
452                 _edit_colour_conversion_button->Enable (false);
453         } else {
454                 VideoContentList video_sel = _parent->selected_video ();
455                 FFmpegContentList ffmpeg_sel = _parent->selected_ffmpeg ();
456                 bool const single = video_sel.size() == 1;
457
458                 _frame_type->wrapped()->Enable (true);
459                 _left_crop->wrapped()->Enable (true);
460                 _right_crop->wrapped()->Enable (true);
461                 _top_crop->wrapped()->Enable (true);
462                 _bottom_crop->wrapped()->Enable (true);
463                 _fade_in->Enable (!video_sel.empty ());
464                 _fade_out->Enable (!video_sel.empty ());
465                 _scale->wrapped()->Enable (true);
466                 _description->Enable (true);
467                 _filters->Enable (true);
468                 _filters_button->Enable (single && !ffmpeg_sel.empty ());
469                 _colour_conversion->Enable (single && !video_sel.empty ());
470                 _edit_colour_conversion_button->Enable (true);
471         }
472 }
473
474 void
475 VideoPanel::fade_in_changed ()
476 {
477         BOOST_FOREACH (shared_ptr<VideoContent> i, _parent->selected_video ()) {
478                 int const vfr = _parent->film()->video_frame_rate ();
479                 i->set_fade_in (_fade_in->get (vfr).frames_round (vfr));
480         }
481 }
482
483 void
484 VideoPanel::fade_out_changed ()
485 {
486         BOOST_FOREACH (shared_ptr<VideoContent> i, _parent->selected_video ()) {
487                 int const vfr = _parent->film()->video_frame_rate ();
488                 i->set_fade_out (_fade_out->get (vfr).frames_round (vfr));
489         }
490 }
491
492 void
493 VideoPanel::reference_clicked ()
494 {
495         ContentList c = _parent->selected ();
496         if (c.size() != 1) {
497                 return;
498         }
499
500         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
501         if (!d) {
502                 return;
503         }
504
505         d->set_reference_video (_reference->GetValue ());
506 }