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