Hand-apply 077d2abb480a883119783db3f957f175e22e543b from master; stop more flickering...
[dcpomatic.git] / src / wx / video_panel.cc
1 /*
2     Copyright (C) 2012-2014 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 <set>
35
36 using std::vector;
37 using std::string;
38 using std::pair;
39 using std::cout;
40 using std::list;
41 using std::set;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44 using boost::bind;
45 using boost::optional;
46
47 static VideoContentScale
48 index_to_scale (int n)
49 {
50         vector<VideoContentScale> scales = VideoContentScale::all ();
51         DCPOMATIC_ASSERT (n >= 0);
52         DCPOMATIC_ASSERT (n < int (scales.size ()));
53         return scales[n];
54 }
55
56 static int
57 scale_to_index (VideoContentScale scale)
58 {
59         vector<VideoContentScale> scales = VideoContentScale::all ();
60         for (size_t i = 0; i < scales.size(); ++i) {
61                 if (scales[i] == scale) {
62                         return i;
63                 }
64         }
65
66         DCPOMATIC_ASSERT (false);
67 }
68
69 VideoPanel::VideoPanel (ContentPanel* p)
70         : ContentSubPanel (p, _("Video"))
71 {
72         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
73         _sizer->Add (grid, 0, wxALL, 8);
74
75         int r = 0;
76
77         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
78         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
79                 this,
80                 new wxChoice (this, wxID_ANY),
81                 VideoContentProperty::VIDEO_FRAME_TYPE,
82                 boost::mem_fn (&VideoContent::video_frame_type),
83                 boost::mem_fn (&VideoContent::set_video_frame_type),
84                 &caster<int, VideoFrameType>,
85                 &caster<VideoFrameType, int>
86                 );
87         _frame_type->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
88         ++r;
89
90         int cr = 0;
91         wxGridBagSizer* crop = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
92
93         add_label_to_grid_bag_sizer (crop, this, _("Left crop"), true, wxGBPosition (cr, 0));
94         _left_crop = new ContentSpinCtrl<VideoContent> (
95                 this,
96                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
97                 VideoContentProperty::VIDEO_CROP,
98                 boost::mem_fn (&VideoContent::left_crop),
99                 boost::mem_fn (&VideoContent::set_left_crop)
100                 );
101         _left_crop->add (crop, wxGBPosition (cr, 1));
102
103         add_label_to_grid_bag_sizer (crop, this, _("Right crop"), true, wxGBPosition (cr, 2));
104         _right_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::right_crop),
109                 boost::mem_fn (&VideoContent::set_right_crop)
110                 );
111         _right_crop->add (crop, wxGBPosition (cr, 3));
112
113         ++cr;
114         
115         add_label_to_grid_bag_sizer (crop, this, _("Top crop"), true, wxGBPosition (cr, 0));
116         _top_crop = new ContentSpinCtrl<VideoContent> (
117                 this,
118                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
119                 VideoContentProperty::VIDEO_CROP,
120                 boost::mem_fn (&VideoContent::top_crop),
121                 boost::mem_fn (&VideoContent::set_top_crop)
122                 );
123         _top_crop->add (crop, wxGBPosition (cr, 1));
124
125         add_label_to_grid_bag_sizer (crop, this, _("Bottom crop"), true, wxGBPosition (cr, 2));
126         _bottom_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::bottom_crop),
131                 boost::mem_fn (&VideoContent::set_bottom_crop)
132                 );
133         _bottom_crop->add (crop, wxGBPosition (cr, 3));
134
135         grid->Add (crop, wxGBPosition (r, 0), wxGBSpan (2, 3));
136         r += 2;
137
138         add_label_to_grid_bag_sizer (grid, this, _("Fade in"), true, wxGBPosition (r, 0));
139         _fade_in = new Timecode<ContentTime> (this);
140         grid->Add (_fade_in, wxGBPosition (r, 1), wxGBSpan (1, 3));
141         ++r;
142
143         add_label_to_grid_bag_sizer (grid, this, _("Fade out"), true, wxGBPosition (r, 0));
144         _fade_out = new Timecode<ContentTime> (this);
145         grid->Add (_fade_out, wxGBPosition (r, 1), wxGBSpan (1, 3));
146         ++r;
147         
148         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
149         _scale = new ContentChoice<VideoContent, VideoContentScale> (
150                 this,
151                 new wxChoice (this, wxID_ANY),
152                 VideoContentProperty::VIDEO_SCALE,
153                 boost::mem_fn (&VideoContent::scale),
154                 boost::mem_fn (&VideoContent::set_scale),
155                 &index_to_scale,
156                 &scale_to_index
157                 );
158         _scale->add (grid, wxGBPosition (r, 1), wxGBSpan (1, 2));
159         ++r;
160
161         wxClientDC dc (this);
162         wxSize size = dc.GetTextExtent (wxT ("A quite long name"));
163         size.SetHeight (-1);
164         
165         add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
166         _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
167         grid->Add (_filters, wxGBPosition (r, 1), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
168         _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
169         grid->Add (_filters_button, wxGBPosition (r, 3), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
170         ++r;
171         
172         _enable_colour_conversion = new wxCheckBox (this, wxID_ANY, _("Colour conversion"));
173         grid->Add (_enable_colour_conversion, wxGBPosition (r, 0), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
174         _colour_conversion = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size);
175         grid->Add (_colour_conversion, wxGBPosition (r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
176         _colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
177         grid->Add (_colour_conversion_button, wxGBPosition (r, 3), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
178         ++r;
179
180         _description = new wxStaticText (this, wxID_ANY, wxT ("\n \n \n \n \n"), wxDefaultPosition, wxDefaultSize);
181         grid->Add (_description, wxGBPosition (r, 0), wxGBSpan (1, 4), wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
182         wxFont font = _description->GetFont();
183         font.SetStyle(wxFONTSTYLE_ITALIC);
184         font.SetPointSize(font.GetPointSize() - 1);
185         _description->SetFont(font);
186         ++r;
187
188         _left_crop->wrapped()->SetRange (0, 1024);
189         _top_crop->wrapped()->SetRange (0, 1024);
190         _right_crop->wrapped()->SetRange (0, 1024);
191         _bottom_crop->wrapped()->SetRange (0, 1024);
192
193         vector<VideoContentScale> scales = VideoContentScale::all ();
194         _scale->wrapped()->Clear ();
195         for (vector<VideoContentScale>::iterator i = scales.begin(); i != scales.end(); ++i) {
196                 _scale->wrapped()->Append (std_to_wx (i->name ()));
197         }
198
199         _frame_type->wrapped()->Append (_("2D"));
200         _frame_type->wrapped()->Append (_("3D left/right"));
201         _frame_type->wrapped()->Append (_("3D top/bottom"));
202         _frame_type->wrapped()->Append (_("3D alternate"));
203         _frame_type->wrapped()->Append (_("3D left only"));
204         _frame_type->wrapped()->Append (_("3D right only"));
205
206         _fade_in->Changed.connect (boost::bind (&VideoPanel::fade_in_changed, this));
207         _fade_out->Changed.connect (boost::bind (&VideoPanel::fade_out_changed, this));
208         
209         _filters_button->Bind           (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
210         _enable_colour_conversion->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&VideoPanel::enable_colour_conversion_clicked, this));
211         _colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
212 }
213
214 void
215 VideoPanel::film_changed (Film::Property property)
216 {
217         switch (property) {
218         case Film::CONTAINER:
219         case Film::VIDEO_FRAME_RATE:
220         case Film::RESOLUTION:
221                 setup_description ();
222                 break;
223         default:
224                 break;
225         }
226 }
227
228 void
229 VideoPanel::film_content_changed (int property)
230 {
231         VideoContentList vc = _parent->selected_video ();
232         shared_ptr<VideoContent> vcs;
233         shared_ptr<FFmpegContent> fcs;
234         if (!vc.empty ()) {
235                 vcs = vc.front ();
236                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
237         }
238         
239         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
240                 setup_description ();
241         } else if (property == VideoContentProperty::VIDEO_CROP) {
242                 setup_description ();
243         } else if (property == VideoContentProperty::VIDEO_SCALE) {
244                 setup_description ();
245         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
246                 setup_description ();
247         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
248                 if (!vcs) {
249                         checked_set (_colour_conversion, wxT (""));
250                 } else if (vcs->colour_conversion ()) {
251                         optional<size_t> preset = vcs->colour_conversion().get().preset ();
252                         vector<PresetColourConversion> cc = Config::instance()->colour_conversions ();
253                         if (preset) {
254                                 checked_set (_colour_conversion, std_to_wx (cc[preset.get()].name));
255                         } else {
256                                 checked_set (_colour_conversion, _("Custom"));
257                         }
258                         _enable_colour_conversion->SetValue (true);
259                         _colour_conversion->Enable (true);
260                         _colour_conversion_button->Enable (true);
261                 } else {
262                         checked_set (_colour_conversion, _("None"));
263                         _enable_colour_conversion->SetValue (false);
264                         _colour_conversion->Enable (false);
265                         _colour_conversion_button->Enable (false);
266                 }
267         } else if (property == FFmpegContentProperty::FILTERS) {
268                 if (fcs) {
269                         string p = Filter::ffmpeg_string (fcs->filters ());
270                         if (p.empty ()) {
271                                 checked_set (_filters, _("None"));
272                         } else {
273                                 if (p.length() > 25) {
274                                         p = p.substr (0, 25) + "...";
275                                 }
276                                 checked_set (_filters, p);
277                         }
278                 }
279         } else if (property == VideoContentProperty::VIDEO_FADE_IN) {
280                 set<ContentTime> check;
281                 for (VideoContentList::const_iterator i = vc.begin (); i != vc.end(); ++i) {
282                         check.insert ((*i)->fade_in ());
283                 }
284                 
285                 if (check.size() == 1) {
286                         _fade_in->set (vc.front()->fade_in (), vc.front()->video_frame_rate ());
287                 } else {
288                         _fade_in->clear ();
289                 }
290         } else if (property == VideoContentProperty::VIDEO_FADE_OUT) {
291                 set<ContentTime> check;
292                 for (VideoContentList::const_iterator i = vc.begin (); i != vc.end(); ++i) {
293                         check.insert ((*i)->fade_out ());
294                 }
295                 
296                 if (check.size() == 1) {
297                         _fade_out->set (vc.front()->fade_out (), vc.front()->video_frame_rate ());
298                 } else {
299                         _fade_out->clear ();
300                 }
301         }
302 }
303
304 /** Called when the `Edit filters' button has been clicked */
305 void
306 VideoPanel::edit_filters_clicked ()
307 {
308         FFmpegContentList c = _parent->selected_ffmpeg ();
309         if (c.size() != 1) {
310                 return;
311         }
312
313         FilterDialog* d = new FilterDialog (this, c.front()->filters());
314         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
315         d->ShowModal ();
316         d->Destroy ();
317 }
318
319 void
320 VideoPanel::setup_description ()
321 {
322         VideoContentList vc = _parent->selected_video ();
323         if (vc.empty ()) {
324                 checked_set (_description, wxT (""));
325                 return;
326         } else if (vc.size() > 1) {
327                 checked_set (_description, _("Multiple content selected"));
328                 return;
329         }
330
331         string d = vc.front()->processing_description ();
332         size_t lines = count (d.begin(), d.end(), '\n');
333
334         for (int i = lines; i < 6; ++i) {
335                 d += "\n ";
336         }
337
338         checked_set (_description, d);
339         _sizer->Layout ();
340 }
341
342 void
343 VideoPanel::edit_colour_conversion_clicked ()
344 {
345         VideoContentList vc = _parent->selected_video ();
346         if (vc.size() != 1) {
347                 return;
348         }
349
350         if (!vc.front()->colour_conversion ()) {
351                 return;
352         }
353
354         ColourConversion conversion = vc.front()->colour_conversion().get ();
355         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
356         d->set (conversion);
357         d->ShowModal ();
358
359         vc.front()->set_colour_conversion (d->get ());
360         d->Destroy ();
361 }
362
363 void
364 VideoPanel::content_selection_changed ()
365 {
366         VideoContentList video_sel = _parent->selected_video ();
367         FFmpegContentList ffmpeg_sel = _parent->selected_ffmpeg ();
368         
369         bool const single = video_sel.size() == 1;
370
371         _left_crop->set_content (video_sel);
372         _right_crop->set_content (video_sel);
373         _top_crop->set_content (video_sel);
374         _bottom_crop->set_content (video_sel);
375         _frame_type->set_content (video_sel);
376         _scale->set_content (video_sel);
377
378         _filters_button->Enable (single && !ffmpeg_sel.empty ());
379         _colour_conversion_button->Enable (single);
380
381         film_content_changed (VideoContentProperty::VIDEO_CROP);
382         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
383         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
384         film_content_changed (VideoContentProperty::VIDEO_FADE_IN);
385         film_content_changed (VideoContentProperty::VIDEO_FADE_OUT);
386         film_content_changed (FFmpegContentProperty::FILTERS);
387 }
388
389 void
390 VideoPanel::fade_in_changed ()
391 {
392         VideoContentList vc = _parent->selected_video ();
393         for (VideoContentList::const_iterator i = vc.begin(); i != vc.end(); ++i) {
394                 (*i)->set_fade_in (_fade_in->get (_parent->film()->video_frame_rate ()));
395         }
396 }
397
398 void
399 VideoPanel::fade_out_changed ()
400 {
401         VideoContentList vc = _parent->selected_video ();
402         for (VideoContentList::const_iterator i = vc.begin(); i != vc.end(); ++i) {
403                 (*i)->set_fade_out (_fade_out->get (_parent->film()->video_frame_rate ()));
404         }
405 }
406
407 void
408 VideoPanel::enable_colour_conversion_clicked ()
409 {
410         VideoContentList vc = _parent->selected_video ();
411         if (vc.size() != 1) {
412                 return;
413         }
414
415         if (_enable_colour_conversion->GetValue()) {
416                 vc.front()->set_default_colour_conversion ();
417         } else {
418                 vc.front()->unset_colour_conversion ();
419         }
420 }