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