Rest of DCP panel.
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "filter_dialog.h"
22 #include "video_panel.h"
23 #include "wx_util.h"
24 #include "content_colour_conversion_dialog.h"
25 #include "content_widget.h"
26 #include "content_panel.h"
27 #include "lib/filter.h"
28 #include "lib/ffmpeg_content.h"
29 #include "lib/colour_conversion.h"
30 #include "lib/config.h"
31 #include "lib/util.h"
32 #include "lib/ratio.h"
33 #include "lib/frame_rate_change.h"
34 #include "lib/dcp_content.h"
35 #include "lib/video_content.h"
36 #include <wx/spinctrl.h>
37 #include <boost/foreach.hpp>
38 #include <set>
39 #include <iostream>
40
41 using std::vector;
42 using std::string;
43 using std::pair;
44 using std::cout;
45 using std::list;
46 using std::set;
47 using boost::shared_ptr;
48 using boost::dynamic_pointer_cast;
49 using boost::bind;
50 using boost::optional;
51
52 static VideoContentScale
53 index_to_scale (int n)
54 {
55         vector<VideoContentScale> scales = VideoContentScale::all ();
56         DCPOMATIC_ASSERT (n >= 0);
57         DCPOMATIC_ASSERT (n < int (scales.size ()));
58         return scales[n];
59 }
60
61 static int
62 scale_to_index (VideoContentScale scale)
63 {
64         vector<VideoContentScale> scales = VideoContentScale::all ();
65         for (size_t i = 0; i < scales.size(); ++i) {
66                 if (scales[i] == scale) {
67                         return i;
68                 }
69         }
70
71         DCPOMATIC_ASSERT (false);
72 }
73
74 VideoPanel::VideoPanel (ContentPanel* p)
75         : ContentSubPanel (p, _("Video"))
76 {
77         _reference = new wxCheckBox (this, wxID_ANY, _("Use this DCP's video as OV and make VF"));
78         _reference_note = new wxStaticText (this, wxID_ANY, _(""));
79         _reference_note->Wrap (200);
80         wxFont font = _reference_note->GetFont();
81         font.SetStyle(wxFONTSTYLE_ITALIC);
82         font.SetPointSize(font.GetPointSize() - 1);
83         _reference_note->SetFont(font);
84
85         _type_label = create_label (this, _("Type"), true);
86         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
87                 this,
88                 new wxChoice (this, wxID_ANY),
89                 VideoContentProperty::FRAME_TYPE,
90                 &Content::video,
91                 boost::mem_fn (&VideoContent::frame_type),
92                 boost::mem_fn (&VideoContent::set_frame_type),
93                 &caster<int, VideoFrameType>,
94                 &caster<VideoFrameType, int>
95                 );
96
97         _left_crop_label = create_label (this, _("Left crop"), true);
98         _left_crop = new ContentSpinCtrl<VideoContent> (
99                 this,
100                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
101                 VideoContentProperty::CROP,
102                 &Content::video,
103                 boost::mem_fn (&VideoContent::left_crop),
104                 boost::mem_fn (&VideoContent::set_left_crop)
105                 );
106
107         _right_crop_label = create_label (this, _("Right crop"), true);
108         _right_crop = new ContentSpinCtrl<VideoContent> (
109                 this,
110                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
111                 VideoContentProperty::CROP,
112                 &Content::video,
113                 boost::mem_fn (&VideoContent::right_crop),
114                 boost::mem_fn (&VideoContent::set_right_crop)
115                 );
116
117         _top_crop_label = create_label (this, _("Top crop"), true);
118         _top_crop = new ContentSpinCtrl<VideoContent> (
119                 this,
120                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
121                 VideoContentProperty::CROP,
122                 &Content::video,
123                 boost::mem_fn (&VideoContent::top_crop),
124                 boost::mem_fn (&VideoContent::set_top_crop)
125                 );
126
127         _bottom_crop_label = create_label (this, _("Bottom crop"), true);
128         _bottom_crop = new ContentSpinCtrl<VideoContent> (
129                 this,
130                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
131                 VideoContentProperty::CROP,
132                 &Content::video,
133                 boost::mem_fn (&VideoContent::bottom_crop),
134                 boost::mem_fn (&VideoContent::set_bottom_crop)
135                 );
136
137         _fade_in_label = create_label (this, _("Fade in"), true);
138         _fade_in = new Timecode<ContentTime> (this);
139
140         _fade_out_label = create_label (this, _("Fade out"), true);
141         _fade_out = new Timecode<ContentTime> (this);
142
143         _scale_to_label = create_label (this, _("Scale to"), true);
144         _scale = new ContentChoice<VideoContent, VideoContentScale> (
145                 this,
146                 new wxChoice (this, wxID_ANY),
147                 VideoContentProperty::SCALE,
148                 &Content::video,
149                 boost::mem_fn (&VideoContent::scale),
150                 boost::mem_fn (&VideoContent::set_scale),
151                 &index_to_scale,
152                 &scale_to_index
153                 );
154
155         wxClientDC dc (this);
156         wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
157         size.SetHeight (-1);
158
159         _filters_label = create_label (this, _("Filters"), true);
160         _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
161         _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
162
163         _colour_conversion_label = create_label (this, _("Colour conversion"), true);
164         _colour_conversion = new wxChoice (this, wxID_ANY, wxDefaultPosition, size);
165         _colour_conversion->Append (_("None"));
166         BOOST_FOREACH (PresetColourConversion const & i, PresetColourConversion::all()) {
167                 _colour_conversion->Append (std_to_wx (i.name));
168         }
169
170         /// TRANSLATORS: translate the word "Custom" here; do not include the "Colour|" prefix
171         _colour_conversion->Append (S_("Colour|Custom"));
172         _edit_colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
173
174         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
175         _description->SetFont(font);
176
177         _left_crop->wrapped()->SetRange (0, 1024);
178         _top_crop->wrapped()->SetRange (0, 1024);
179         _right_crop->wrapped()->SetRange (0, 1024);
180         _bottom_crop->wrapped()->SetRange (0, 1024);
181
182         _scale->wrapped()->Clear ();
183         BOOST_FOREACH (VideoContentScale const & i, VideoContentScale::all ()) {
184                 _scale->wrapped()->Append (std_to_wx (i.name ()));
185         }
186
187         _frame_type->wrapped()->Append (_("2D"));
188         _frame_type->wrapped()->Append (_("3D"));
189         _frame_type->wrapped()->Append (_("3D left/right"));
190         _frame_type->wrapped()->Append (_("3D top/bottom"));
191         _frame_type->wrapped()->Append (_("3D alternate"));
192         _frame_type->wrapped()->Append (_("3D left only"));
193         _frame_type->wrapped()->Append (_("3D right only"));
194
195         content_selection_changed ();
196
197         _fade_in->Changed.connect (boost::bind (&VideoPanel::fade_in_changed, this));
198         _fade_out->Changed.connect (boost::bind (&VideoPanel::fade_out_changed, this));
199
200         _reference->Bind                     (wxEVT_CHECKBOX, boost::bind (&VideoPanel::reference_clicked, this));
201         _filters_button->Bind                (wxEVT_BUTTON,   boost::bind (&VideoPanel::edit_filters_clicked, this));
202         _colour_conversion->Bind             (wxEVT_CHOICE,   boost::bind (&VideoPanel::colour_conversion_changed, this));
203         _edit_colour_conversion_button->Bind (wxEVT_BUTTON,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
204
205         add_to_grid ();
206 }
207
208 void
209 VideoPanel::add_to_grid ()
210 {
211         bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
212
213         int r = 0;
214
215         _reference->Show (full);
216         _reference_note->Show (full);
217
218         if (full) {
219                 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
220                 reference_sizer->Add (_reference, 0);
221                 reference_sizer->Add (_reference_note, 0);
222                 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 3));
223                 ++r;
224         }
225
226         add_label_to_sizer (_grid, _type_label, true, wxGBPosition(r, 0));
227         _frame_type->add (_grid, wxGBPosition(r, 1), wxGBSpan(1, 2));
228         ++r;
229
230         int flags = wxTOP;
231 #ifdef __WXOSX__
232         flags |= wxALIGN_RIGHT;
233 #endif
234         int cr = 0;
235         wxGridBagSizer* crop = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
236         add_label_to_sizer (crop, _left_crop_label, true, wxGBPosition (cr, 0));
237         _left_crop->add (crop, wxGBPosition (cr, 1));
238         add_label_to_sizer (crop, _right_crop_label, true, wxGBPosition (cr, 2));
239         _right_crop->add (crop, wxGBPosition (cr, 3));
240         ++cr;
241         add_label_to_sizer (crop, _top_crop_label, true, wxGBPosition (cr, 0));
242         _top_crop->add (crop, wxGBPosition (cr, 1));
243         add_label_to_sizer (crop, _bottom_crop_label, true, wxGBPosition (cr, 2));
244         _bottom_crop->add (crop, wxGBPosition (cr, 3));
245         _grid->Add (crop, wxGBPosition (r, 0), wxGBSpan (2, 4));
246         r += 2;
247
248         _fade_in_label->Show (full);
249         _fade_in->Show (full);
250         _fade_out_label->Show (full);
251         _fade_out->Show (full);
252         _scale_to_label->Show (full);
253         _scale->show (full);
254         _filters_label->Show (full);
255         _filters->Show (full);
256         _filters_button->Show (full);
257         _colour_conversion_label->Show (full);
258         _colour_conversion->Show (full);
259         _edit_colour_conversion_button->Show (full);
260
261         if (full) {
262                 add_label_to_sizer (_grid, _fade_in_label, true, wxGBPosition (r, 0));
263                 _grid->Add (_fade_in, wxGBPosition (r, 1), wxGBSpan (1, 3));
264                 ++r;
265
266                 add_label_to_sizer (_grid, _fade_out_label, true, wxGBPosition (r, 0));
267                 _grid->Add (_fade_out, wxGBPosition (r, 1), wxGBSpan (1, 3));
268                 ++r;
269
270                 add_label_to_sizer (_grid, _scale_to_label, true, wxGBPosition (r, 0));
271                 _scale->add (_grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
272                 ++r;
273
274                 add_label_to_sizer (_grid, _filters_label, true, wxGBPosition (r, 0));
275                 {
276                         wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
277                         s->Add (_filters, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
278                         s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
279                         _grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
280                 }
281                 ++r;
282
283                 add_label_to_sizer (_grid, _colour_conversion_label, true, wxGBPosition(r, 0));
284                 {
285                         wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
286                         s->Add (_colour_conversion, 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
287                         s->Add (_edit_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
288                         _grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
289                 }
290                 ++r;
291         }
292
293         _grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 4), wxEXPAND | wxALIGN_CENTER_VERTICAL, 6);
294         ++r;
295 }
296
297
298 void
299 VideoPanel::film_changed (Film::Property property)
300 {
301         switch (property) {
302         case Film::VIDEO_FRAME_RATE:
303         case Film::CONTAINER:
304                 setup_description ();
305                 setup_sensitivity ();
306                 break;
307         case Film::RESOLUTION:
308                 setup_description ();
309                 break;
310         case Film::REEL_TYPE:
311         case Film::INTEROP:
312                 setup_sensitivity ();
313                 break;
314         default:
315                 break;
316         }
317 }
318
319 void
320 VideoPanel::film_content_changed (int property)
321 {
322         ContentList vc = _parent->selected_video ();
323         shared_ptr<Content> vcs;
324         shared_ptr<FFmpegContent> fcs;
325         if (!vc.empty ()) {
326                 vcs = vc.front ();
327                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
328         }
329
330         if (property == ContentProperty::VIDEO_FRAME_RATE ||
331             property == VideoContentProperty::FRAME_TYPE ||
332             property == VideoContentProperty::CROP ||
333             property == VideoContentProperty::SCALE) {
334                 setup_description ();
335         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
336                 if (vcs && vcs->video->colour_conversion ()) {
337                         optional<size_t> preset = vcs->video->colour_conversion().get().preset ();
338                         vector<PresetColourConversion> cc = PresetColourConversion::all ();
339                         if (preset) {
340                                 checked_set (_colour_conversion, preset.get() + 1);
341                         } else {
342                                 checked_set (_colour_conversion, cc.size() + 1);
343                         }
344                 } else {
345                         checked_set (_colour_conversion, 0);
346                 }
347
348                 setup_sensitivity ();
349
350         } else if (property == FFmpegContentProperty::FILTERS) {
351                 if (fcs) {
352                         string p = Filter::ffmpeg_string (fcs->filters ());
353                         if (p.empty ()) {
354                                 checked_set (_filters, _("None"));
355                         } else {
356                                 if (p.length() > 25) {
357                                         p = p.substr (0, 25) + "...";
358                                 }
359                                 checked_set (_filters, p);
360                         }
361                 }
362         } else if (property == VideoContentProperty::FADE_IN) {
363                 set<Frame> check;
364                 BOOST_FOREACH (shared_ptr<const Content> i, vc) {
365                         check.insert (i->video->fade_in ());
366                 }
367
368                 if (check.size() == 1) {
369                         _fade_in->set (
370                                 ContentTime::from_frames (vc.front()->video->fade_in (), vc.front()->active_video_frame_rate ()),
371                                 vc.front()->active_video_frame_rate ()
372                                 );
373                 } else {
374                         _fade_in->clear ();
375                 }
376         } else if (property == VideoContentProperty::FADE_OUT) {
377                 set<Frame> check;
378                 BOOST_FOREACH (shared_ptr<const Content> i, vc) {
379                         check.insert (i->video->fade_out ());
380                 }
381
382                 if (check.size() == 1) {
383                         _fade_out->set (
384                                 ContentTime::from_frames (vc.front()->video->fade_out (), vc.front()->active_video_frame_rate ()),
385                                 vc.front()->active_video_frame_rate ()
386                                 );
387                 } else {
388                         _fade_out->clear ();
389                 }
390         } else if (property == DCPContentProperty::REFERENCE_VIDEO) {
391                 if (vc.size() == 1) {
392                         shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (vc.front ());
393                         checked_set (_reference, dcp ? dcp->reference_video () : false);
394                 } else {
395                         checked_set (_reference, false);
396                 }
397
398                 setup_sensitivity ();
399         }
400 }
401
402 /** Called when the `Edit filters' button has been clicked */
403 void
404 VideoPanel::edit_filters_clicked ()
405 {
406         FFmpegContentList c = _parent->selected_ffmpeg ();
407         if (c.size() != 1) {
408                 return;
409         }
410
411         FilterDialog* d = new FilterDialog (this, c.front()->filters());
412         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
413         d->ShowModal ();
414         d->Destroy ();
415 }
416
417 void
418 VideoPanel::setup_description ()
419 {
420         ContentList vc = _parent->selected_video ();
421         if (vc.empty ()) {
422                 checked_set (_description, wxT (""));
423                 return;
424         } else if (vc.size() > 1) {
425                 checked_set (_description, _("Multiple content selected"));
426                 return;
427         }
428
429         string d = vc.front()->video->processing_description ();
430         size_t lines = count (d.begin(), d.end(), '\n');
431
432         for (int i = lines; i < 6; ++i) {
433                 d += "\n ";
434         }
435
436         checked_set (_description, d);
437         _sizer->Layout ();
438 }
439
440 void
441 VideoPanel::colour_conversion_changed ()
442 {
443         ContentList vc = _parent->selected_video ();
444         if (vc.size() != 1) {
445                 return;
446         }
447
448         int const s = _colour_conversion->GetSelection ();
449         vector<PresetColourConversion> all = PresetColourConversion::all ();
450
451         if (s == 0) {
452                 vc.front()->video->unset_colour_conversion ();
453         } else if (s == int (all.size() + 1)) {
454                 edit_colour_conversion_clicked ();
455         } else {
456                 vc.front()->video->set_colour_conversion (all[s - 1].conversion);
457         }
458 }
459
460 void
461 VideoPanel::edit_colour_conversion_clicked ()
462 {
463         ContentList vc = _parent->selected_video ();
464         if (vc.size() != 1) {
465                 return;
466         }
467
468         ContentColourConversionDialog* d = new ContentColourConversionDialog (this, vc.front()->video->yuv ());
469         d->set (vc.front()->video->colour_conversion().get_value_or (PresetColourConversion::all().front().conversion));
470         if (d->ShowModal() == wxID_OK) {
471                 vc.front()->video->set_colour_conversion (d->get ());
472         } else {
473                 /* Reset the colour conversion choice */
474                 film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
475         }
476         d->Destroy ();
477 }
478
479 void
480 VideoPanel::content_selection_changed ()
481 {
482         ContentList video_sel = _parent->selected_video ();
483
484         _frame_type->set_content (video_sel);
485         _left_crop->set_content (video_sel);
486         _right_crop->set_content (video_sel);
487         _top_crop->set_content (video_sel);
488         _bottom_crop->set_content (video_sel);
489         _scale->set_content (video_sel);
490
491         film_content_changed (ContentProperty::VIDEO_FRAME_RATE);
492         film_content_changed (VideoContentProperty::CROP);
493         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
494         film_content_changed (VideoContentProperty::FADE_IN);
495         film_content_changed (VideoContentProperty::FADE_OUT);
496         film_content_changed (FFmpegContentProperty::FILTERS);
497         film_content_changed (DCPContentProperty::REFERENCE_VIDEO);
498
499         setup_sensitivity ();
500 }
501
502 void
503 VideoPanel::setup_sensitivity ()
504 {
505         ContentList sel = _parent->selected ();
506
507         shared_ptr<DCPContent> dcp;
508         if (sel.size() == 1) {
509                 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
510         }
511
512         string why_not;
513         bool const can_reference = dcp && dcp->can_reference_video (why_not);
514         setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
515
516         if (_reference->GetValue ()) {
517                 _frame_type->wrapped()->Enable (false);
518                 _left_crop->wrapped()->Enable (false);
519                 _right_crop->wrapped()->Enable (false);
520                 _top_crop->wrapped()->Enable (false);
521                 _bottom_crop->wrapped()->Enable (false);
522                 _fade_in->Enable (false);
523                 _fade_out->Enable (false);
524                 _scale->wrapped()->Enable (false);
525                 _description->Enable (false);
526                 _filters->Enable (false);
527                 _filters_button->Enable (false);
528                 _colour_conversion->Enable (false);
529         } else {
530                 ContentList video_sel = _parent->selected_video ();
531                 FFmpegContentList ffmpeg_sel = _parent->selected_ffmpeg ();
532                 bool const single = video_sel.size() == 1;
533
534                 _frame_type->wrapped()->Enable (true);
535                 _left_crop->wrapped()->Enable (true);
536                 _right_crop->wrapped()->Enable (true);
537                 _top_crop->wrapped()->Enable (true);
538                 _bottom_crop->wrapped()->Enable (true);
539                 _fade_in->Enable (!video_sel.empty ());
540                 _fade_out->Enable (!video_sel.empty ());
541                 _scale->wrapped()->Enable (true);
542                 _description->Enable (true);
543                 _filters->Enable (true);
544                 _filters_button->Enable (single && !ffmpeg_sel.empty ());
545                 _colour_conversion->Enable (single && !video_sel.empty ());
546         }
547
548         ContentList vc = _parent->selected_video ();
549         shared_ptr<Content> vcs;
550         if (!vc.empty ()) {
551                 vcs = vc.front ();
552         }
553
554         if (vcs && vcs->video->colour_conversion ()) {
555                 _edit_colour_conversion_button->Enable (!vcs->video->colour_conversion().get().preset());
556         } else {
557                 _edit_colour_conversion_button->Enable (false);
558         }
559 }
560
561 void
562 VideoPanel::fade_in_changed ()
563 {
564         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video ()) {
565                 int const vfr = _parent->film()->video_frame_rate ();
566                 i->video->set_fade_in (_fade_in->get (vfr).frames_round (vfr));
567         }
568 }
569
570 void
571 VideoPanel::fade_out_changed ()
572 {
573         BOOST_FOREACH (shared_ptr<Content> i, _parent->selected_video ()) {
574                 int const vfr = _parent->film()->video_frame_rate ();
575                 i->video->set_fade_out (_fade_out->get (vfr).frames_round (vfr));
576         }
577 }
578
579 void
580 VideoPanel::reference_clicked ()
581 {
582         ContentList c = _parent->selected ();
583         if (c.size() != 1) {
584                 return;
585         }
586
587         shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
588         if (!d) {
589                 return;
590         }
591
592         d->set_reference_video (_reference->GetValue ());
593 }