Lots of #include <iostream>s for Arch.
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/filter.h"
21 #include "lib/ffmpeg_content.h"
22 #include "lib/colour_conversion.h"
23 #include "lib/config.h"
24 #include "lib/util.h"
25 #include "lib/ratio.h"
26 #include "lib/frame_rate_change.h"
27 #include "filter_dialog.h"
28 #include "video_panel.h"
29 #include "wx_util.h"
30 #include "content_colour_conversion_dialog.h"
31 #include "content_widget.h"
32 #include "content_panel.h"
33 #include <wx/spinctrl.h>
34 #include <boost/foreach.hpp>
35 #include <set>
36 #include <iostream>
37
38 using std::vector;
39 using std::string;
40 using std::pair;
41 using std::cout;
42 using std::list;
43 using std::set;
44 using boost::shared_ptr;
45 using boost::dynamic_pointer_cast;
46 using boost::bind;
47 using boost::optional;
48
49 static VideoContentScale
50 index_to_scale (int n)
51 {
52         vector<VideoContentScale> scales = VideoContentScale::all ();
53         DCPOMATIC_ASSERT (n >= 0);
54         DCPOMATIC_ASSERT (n < int (scales.size ()));
55         return scales[n];
56 }
57
58 static int
59 scale_to_index (VideoContentScale scale)
60 {
61         vector<VideoContentScale> scales = VideoContentScale::all ();
62         for (size_t i = 0; i < scales.size(); ++i) {
63                 if (scales[i] == scale) {
64                         return i;
65                 }
66         }
67
68         DCPOMATIC_ASSERT (false);
69 }
70
71 VideoPanel::VideoPanel (ContentPanel* p)
72         : ContentSubPanel (p, _("Video"))
73 {
74         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
75         _sizer->Add (grid, 0, wxALL, 8);
76
77         int r = 0;
78
79         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
80         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
81                 this,
82                 new wxChoice (this, wxID_ANY),
83                 VideoContentProperty::VIDEO_FRAME_TYPE,
84                 boost::mem_fn (&VideoContent::video_frame_type),
85                 boost::mem_fn (&VideoContent::set_video_frame_type),
86                 &caster<int, VideoFrameType>,
87                 &caster<VideoFrameType, int>
88                 );
89         _frame_type->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
90         ++r;
91
92         add_label_to_grid_bag_sizer (grid, this, _("Crop"), true, wxGBPosition (r, 0));
93
94         int cr = 0;
95         wxGridBagSizer* crop = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
96
97         add_label_to_grid_bag_sizer (crop, this, _("Left"), true, wxGBPosition (cr, 0));
98         _left_crop = new ContentSpinCtrl<VideoContent> (
99                 this,
100                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
101                 VideoContentProperty::VIDEO_CROP,
102                 boost::mem_fn (&VideoContent::left_crop),
103                 boost::mem_fn (&VideoContent::set_left_crop)
104                 );
105         _left_crop->add (crop, wxGBPosition (cr, 1));
106
107         add_label_to_grid_bag_sizer (crop, this, _("Right"), true, wxGBPosition (cr, 2));
108         _right_crop = new ContentSpinCtrl<VideoContent> (
109                 this,
110                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
111                 VideoContentProperty::VIDEO_CROP,
112                 boost::mem_fn (&VideoContent::right_crop),
113                 boost::mem_fn (&VideoContent::set_right_crop)
114                 );
115         _right_crop->add (crop, wxGBPosition (cr, 3));
116
117         ++cr;
118
119         add_label_to_grid_bag_sizer (crop, this, _("Top"), true, wxGBPosition (cr, 0));
120         _top_crop = new ContentSpinCtrl<VideoContent> (
121                 this,
122                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
123                 VideoContentProperty::VIDEO_CROP,
124                 boost::mem_fn (&VideoContent::top_crop),
125                 boost::mem_fn (&VideoContent::set_top_crop)
126                 );
127         _top_crop->add (crop, wxGBPosition (cr, 1));
128
129         add_label_to_grid_bag_sizer (crop, this, _("Bottom"), true, wxGBPosition (cr, 2));
130         _bottom_crop = new ContentSpinCtrl<VideoContent> (
131                 this,
132                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
133                 VideoContentProperty::VIDEO_CROP,
134                 boost::mem_fn (&VideoContent::bottom_crop),
135                 boost::mem_fn (&VideoContent::set_bottom_crop)
136                 );
137         _bottom_crop->add (crop, wxGBPosition (cr, 3));
138
139         grid->Add (crop, wxGBPosition (r, 1), wxGBSpan (2, 3));
140         r += 2;
141
142         add_label_to_grid_bag_sizer (grid, this, _("Fade in"), true, wxGBPosition (r, 0));
143         _fade_in = new Timecode<ContentTime> (this);
144         grid->Add (_fade_in, wxGBPosition (r, 1), wxGBSpan (1, 3));
145         ++r;
146
147         add_label_to_grid_bag_sizer (grid, this, _("Fade out"), true, wxGBPosition (r, 0));
148         _fade_out = new Timecode<ContentTime> (this);
149         grid->Add (_fade_out, wxGBPosition (r, 1), wxGBSpan (1, 3));
150         ++r;
151
152         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
153         _scale = new ContentChoice<VideoContent, VideoContentScale> (
154                 this,
155                 new wxChoice (this, wxID_ANY),
156                 VideoContentProperty::VIDEO_SCALE,
157                 boost::mem_fn (&VideoContent::scale),
158                 boost::mem_fn (&VideoContent::set_scale),
159                 &index_to_scale,
160                 &scale_to_index
161                 );
162         _scale->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
163         ++r;
164
165         wxClientDC dc (this);
166         wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
167         size.SetHeight (-1);
168
169         add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
170         {
171                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
172
173                 _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
174                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
175                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
176                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
177
178                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
179         }
180         ++r;
181
182         add_label_to_grid_bag_sizer (grid, this, _("Colour conversion"), true, wxGBPosition (r, 0));
183         {
184                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
185
186                 _colour_conversion = new wxChoice (this, wxID_ANY, wxDefaultPosition, size);
187                 _colour_conversion->Append (_("None"));
188                 BOOST_FOREACH (PresetColourConversion const & i, PresetColourConversion::all()) {
189                         _colour_conversion->Append (std_to_wx (i.name));
190                 }
191                 _colour_conversion->Append (_("Custom"));
192                 s->Add (_colour_conversion, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
193
194                 _edit_colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
195                 s->Add (_edit_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
196
197                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
198         }
199         ++r;
200
201         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
202         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 4), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
203         wxFont font = _description->GetFont();
204         font.SetStyle(wxFONTSTYLE_ITALIC);
205         font.SetPointSize(font.GetPointSize() - 1);
206         _description->SetFont(font);
207         ++r;
208
209         _left_crop->wrapped()->SetRange (0, 1024);
210         _top_crop->wrapped()->SetRange (0, 1024);
211         _right_crop->wrapped()->SetRange (0, 1024);
212         _bottom_crop->wrapped()->SetRange (0, 1024);
213
214         _scale->wrapped()->Clear ();
215         BOOST_FOREACH (VideoContentScale const & i, VideoContentScale::all ()) {
216                 _scale->wrapped()->Append (std_to_wx (i.name ()));
217         }
218
219         _frame_type->wrapped()->Append (_("2D"));
220         _frame_type->wrapped()->Append (_("3D left/right"));
221         _frame_type->wrapped()->Append (_("3D top/bottom"));
222         _frame_type->wrapped()->Append (_("3D alternate"));
223         _frame_type->wrapped()->Append (_("3D left only"));
224         _frame_type->wrapped()->Append (_("3D right only"));
225
226         _fade_in->Changed.connect (boost::bind (&VideoPanel::fade_in_changed, this));
227         _fade_out->Changed.connect (boost::bind (&VideoPanel::fade_out_changed, this));
228
229         _filters_button->Bind                (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&VideoPanel::edit_filters_clicked, this));
230         _colour_conversion->Bind             (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&VideoPanel::colour_conversion_changed, this));
231         _edit_colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
232 }
233
234 void
235 VideoPanel::film_changed (Film::Property property)
236 {
237         switch (property) {
238         case Film::CONTAINER:
239         case Film::VIDEO_FRAME_RATE:
240         case Film::RESOLUTION:
241                 setup_description ();
242                 break;
243         default:
244                 break;
245         }
246 }
247
248 void
249 VideoPanel::film_content_changed (int property)
250 {
251         VideoContentList vc = _parent->selected_video ();
252         shared_ptr<VideoContent> vcs;
253         shared_ptr<FFmpegContent> fcs;
254         if (!vc.empty ()) {
255                 vcs = vc.front ();
256                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
257         }
258
259         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
260                 setup_description ();
261         } else if (property == VideoContentProperty::VIDEO_CROP) {
262                 setup_description ();
263         } else if (property == VideoContentProperty::VIDEO_SCALE) {
264                 setup_description ();
265         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
266                 setup_description ();
267         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
268                 if (!vcs) {
269                         checked_set (_colour_conversion, 0);
270                         _edit_colour_conversion_button->Enable (false);
271                 } else if (vcs->colour_conversion ()) {
272                         optional<size_t> preset = vcs->colour_conversion().get().preset ();
273                         vector<PresetColourConversion> cc = PresetColourConversion::all ();
274                         if (preset) {
275                                 checked_set (_colour_conversion, preset.get() + 1);
276                                 _edit_colour_conversion_button->Enable (false);
277                         } else {
278                                 checked_set (_colour_conversion, cc.size() + 1);
279                                 _edit_colour_conversion_button->Enable (true);
280                         }
281                 } else {
282                         checked_set (_colour_conversion, 0);
283                         _edit_colour_conversion_button->Enable (false);
284                 }
285         } else if (property == FFmpegContentProperty::FILTERS) {
286                 if (fcs) {
287                         string p = Filter::ffmpeg_string (fcs->filters ());
288                         if (p.empty ()) {
289                                 checked_set (_filters, _("None"));
290                         } else {
291                                 if (p.length() > 25) {
292                                         p = p.substr (0, 25) + "...";
293                                 }
294                                 checked_set (_filters, p);
295                         }
296                 }
297         } else if (property == VideoContentProperty::VIDEO_FADE_IN) {
298                 set<Frame> check;
299                 BOOST_FOREACH (shared_ptr<const VideoContent> i, vc) {
300                         check.insert (i->fade_in ());
301                 }
302
303                 if (check.size() == 1) {
304                         _fade_in->set (ContentTime::from_frames (vc.front()->fade_in (), vc.front()->video_frame_rate ()), vc.front()->video_frame_rate ());
305                 } else {
306                         _fade_in->clear ();
307                 }
308         } else if (property == VideoContentProperty::VIDEO_FADE_OUT) {
309                 set<Frame> check;
310                 BOOST_FOREACH (shared_ptr<const VideoContent> i, vc) {
311                         check.insert (i->fade_out ());
312                 }
313
314                 if (check.size() == 1) {
315                         _fade_out->set (ContentTime::from_frames (vc.front()->fade_out (), vc.front()->video_frame_rate ()), vc.front()->video_frame_rate ());
316                 } else {
317                         _fade_out->clear ();
318                 }
319         }
320 }
321
322 /** Called when the `Edit filters' button has been clicked */
323 void
324 VideoPanel::edit_filters_clicked ()
325 {
326         FFmpegContentList c = _parent->selected_ffmpeg ();
327         if (c.size() != 1) {
328                 return;
329         }
330
331         FilterDialog* d = new FilterDialog (this, c.front()->filters());
332         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
333         d->ShowModal ();
334         d->Destroy ();
335 }
336
337 void
338 VideoPanel::setup_description ()
339 {
340         VideoContentList vc = _parent->selected_video ();
341         if (vc.empty ()) {
342                 checked_set (_description, wxT (""));
343                 return;
344         } else if (vc.size() > 1) {
345                 checked_set (_description, _("Multiple content selected"));
346                 return;
347         }
348
349         string d = vc.front()->processing_description ();
350         size_t lines = count (d.begin(), d.end(), '\n');
351
352         for (int i = lines; i < 6; ++i) {
353                 d += "\n ";
354         }
355
356         checked_set (_description, d);
357         _sizer->Layout ();
358 }
359
360 void
361 VideoPanel::colour_conversion_changed ()
362 {
363         VideoContentList vc = _parent->selected_video ();
364         if (vc.size() != 1) {
365                 return;
366         }
367
368         int const s = _colour_conversion->GetSelection ();
369         vector<PresetColourConversion> all = PresetColourConversion::all ();
370
371         if (s == 0) {
372                 vc.front()->unset_colour_conversion ();
373         } else if (s == int (all.size() + 1)) {
374                 edit_colour_conversion_clicked ();
375         } else {
376                 vc.front()->set_colour_conversion (all[s - 1].conversion);
377         }
378 }
379
380 void
381 VideoPanel::edit_colour_conversion_clicked ()
382 {
383         VideoContentList vc = _parent->selected_video ();
384         if (vc.size() != 1) {
385                 return;
386         }
387
388         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
389         d->set (vc.front()->colour_conversion().get_value_or (PresetColourConversion::all().front ().conversion));
390         d->ShowModal ();
391         vc.front()->set_colour_conversion (d->get ());
392         d->Destroy ();
393 }
394
395 void
396 VideoPanel::content_selection_changed ()
397 {
398         VideoContentList video_sel = _parent->selected_video ();
399         FFmpegContentList ffmpeg_sel = _parent->selected_ffmpeg ();
400
401         bool const single = video_sel.size() == 1;
402
403         _frame_type->set_content (video_sel);
404         _left_crop->set_content (video_sel);
405         _right_crop->set_content (video_sel);
406         _top_crop->set_content (video_sel);
407         _bottom_crop->set_content (video_sel);
408         _fade_in->Enable (!video_sel.empty ());
409         _fade_out->Enable (!video_sel.empty ());
410         _scale->set_content (video_sel);
411
412         _colour_conversion->Enable (single && !video_sel.empty ());
413         _filters_button->Enable (single && !ffmpeg_sel.empty ());
414
415         film_content_changed (VideoContentProperty::VIDEO_CROP);
416         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
417         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
418         film_content_changed (VideoContentProperty::VIDEO_FADE_IN);
419         film_content_changed (VideoContentProperty::VIDEO_FADE_OUT);
420         film_content_changed (FFmpegContentProperty::FILTERS);
421 }
422
423 void
424 VideoPanel::fade_in_changed ()
425 {
426         BOOST_FOREACH (shared_ptr<VideoContent> i, _parent->selected_video ()) {
427                 int const vfr = _parent->film()->video_frame_rate ();
428                 i->set_fade_in (_fade_in->get (vfr).frames_round (vfr));
429         }
430 }
431
432 void
433 VideoPanel::fade_out_changed ()
434 {
435         BOOST_FOREACH (shared_ptr<VideoContent> i, _parent->selected_video ()) {
436                 int const vfr = _parent->film()->video_frame_rate ();
437                 i->set_fade_out (_fade_out->get (vfr).frames_round (vfr));
438         }
439 }