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