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