Add basics of colourspace conversion bypass (#266).
[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 "film_editor.h"
32 #include "content_colour_conversion_dialog.h"
33 #include "content_widget.h"
34
35 using std::vector;
36 using std::string;
37 using std::pair;
38 using std::cout;
39 using std::list;
40 using boost::shared_ptr;
41 using boost::dynamic_pointer_cast;
42 using boost::bind;
43 using boost::optional;
44
45 static VideoContentScale
46 index_to_scale (int n)
47 {
48         vector<VideoContentScale> scales = VideoContentScale::all ();
49         assert (n >= 0);
50         assert (n < int (scales.size ()));
51         return scales[n];
52 }
53
54 static int
55 scale_to_index (VideoContentScale scale)
56 {
57         vector<VideoContentScale> scales = VideoContentScale::all ();
58         for (size_t i = 0; i < scales.size(); ++i) {
59                 if (scales[i] == scale) {
60                         return i;
61                 }
62         }
63
64         assert (false);
65 }
66
67 VideoPanel::VideoPanel (FilmEditor* e)
68         : FilmEditorPanel (e, _("Video"))
69 {
70         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
71         _sizer->Add (grid, 0, wxALL, 8);
72
73         int r = 0;
74
75         add_label_to_grid_bag_sizer (grid, this, _("Type"), true, wxGBPosition (r, 0));
76         _frame_type = new ContentChoice<VideoContent, VideoFrameType> (
77                 this,
78                 new wxChoice (this, wxID_ANY),
79                 VideoContentProperty::VIDEO_FRAME_TYPE,
80                 boost::mem_fn (&VideoContent::video_frame_type),
81                 boost::mem_fn (&VideoContent::set_video_frame_type),
82                 &caster<int, VideoFrameType>,
83                 &caster<VideoFrameType, int>
84                 );
85         _frame_type->add (grid, wxGBPosition (r, 1));
86         ++r;
87         
88         add_label_to_grid_bag_sizer (grid, this, _("Left crop"), true, wxGBPosition (r, 0));
89         _left_crop = new ContentSpinCtrl<VideoContent> (
90                 this,
91                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
92                 VideoContentProperty::VIDEO_CROP,
93                 boost::mem_fn (&VideoContent::left_crop),
94                 boost::mem_fn (&VideoContent::set_left_crop)
95                 );
96         _left_crop->add (grid, wxGBPosition (r, 1));
97         ++r;
98
99         add_label_to_grid_bag_sizer (grid, this, _("Right crop"), true, wxGBPosition (r, 0));
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, 1));
108         ++r;
109         
110         add_label_to_grid_bag_sizer (grid, this, _("Top crop"), true, wxGBPosition (r, 0));
111         _top_crop = new ContentSpinCtrl<VideoContent> (
112                 this,
113                 new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (64, -1)),
114                 VideoContentProperty::VIDEO_CROP,
115                 boost::mem_fn (&VideoContent::top_crop),
116                 boost::mem_fn (&VideoContent::set_top_crop)
117                 );
118         _top_crop->add (grid, wxGBPosition (r,1 ));
119         ++r;
120         
121         add_label_to_grid_bag_sizer (grid, this, _("Bottom crop"), true, wxGBPosition (r, 0));
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, 1));
130         ++r;
131
132         add_label_to_grid_bag_sizer (grid, this, _("Scale to"), true, wxGBPosition (r, 0));
133         _scale = new ContentChoice<VideoContent, VideoContentScale> (
134                 this,
135                 new wxChoice (this, wxID_ANY),
136                 VideoContentProperty::VIDEO_SCALE,
137                 boost::mem_fn (&VideoContent::scale),
138                 boost::mem_fn (&VideoContent::set_scale),
139                 &index_to_scale,
140                 &scale_to_index
141                 );
142         _scale->add (grid, wxGBPosition (r, 1));
143         ++r;
144
145         {
146                 add_label_to_grid_bag_sizer (grid, this, _("Filters"), true, wxGBPosition (r, 0));
147                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
148
149                 wxClientDC dc (this);
150                 wxSize size = dc.GetTextExtent (wxT ("A quite long-ish name"));
151                 size.SetHeight (-1);
152                 
153                 _filters = new wxStaticText (this, wxID_ANY, _("None"), wxDefaultPosition, size);
154                 s->Add (_filters, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
155                 _filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
156                 s->Add (_filters_button, 0, wxALIGN_CENTER_VERTICAL);
157                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
158         }
159         ++r;
160         
161         {
162                 _enable_colour_conversion = new wxCheckBox (this, wxID_ANY, _("Colour conversion"));
163                 grid->Add (_enable_colour_conversion, wxGBPosition (r, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
164
165                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
166
167                 wxClientDC dc (this);
168                 wxSize size = dc.GetTextExtent (wxT ("A quite long-ish name"));
169                 size.SetHeight (-1);
170                 
171                 _colour_conversion = new wxStaticText (this, wxID_ANY, wxT (""), wxDefaultPosition, size);
172
173                 s->Add (_colour_conversion, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
174                 _colour_conversion_button = new wxButton (this, wxID_ANY, _("Edit..."));
175                 s->Add (_colour_conversion_button, 0, wxALIGN_CENTER_VERTICAL);
176                 grid->Add (s, wxGBPosition (r, 1), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
177         }
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, 2), 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         _filters_button->Bind           (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_filters_clicked, this));
207         _enable_colour_conversion->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&VideoPanel::enable_colour_conversion_clicked, this));
208         _colour_conversion_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&VideoPanel::edit_colour_conversion_clicked, this));
209 }
210
211 void
212 VideoPanel::film_changed (Film::Property property)
213 {
214         switch (property) {
215         case Film::CONTAINER:
216         case Film::VIDEO_FRAME_RATE:
217         case Film::RESOLUTION:
218                 setup_description ();
219                 break;
220         default:
221                 break;
222         }
223 }
224
225 void
226 VideoPanel::film_content_changed (int property)
227 {
228         VideoContentList vc = _editor->selected_video_content ();
229         shared_ptr<VideoContent> vcs;
230         shared_ptr<FFmpegContent> fcs;
231         if (!vc.empty ()) {
232                 vcs = vc.front ();
233                 fcs = dynamic_pointer_cast<FFmpegContent> (vcs);
234         }
235         
236         if (property == VideoContentProperty::VIDEO_FRAME_TYPE) {
237                 setup_description ();
238         } else if (property == VideoContentProperty::VIDEO_CROP) {
239                 setup_description ();
240         } else if (property == VideoContentProperty::VIDEO_SCALE) {
241                 setup_description ();
242         } else if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
243                 setup_description ();
244         } else if (property == VideoContentProperty::COLOUR_CONVERSION) {
245                 if (!vcs) {
246                         _colour_conversion->SetLabel (wxT (""));
247                 } else if (vcs->colour_conversion ()) {
248                         optional<size_t> preset = vcs->colour_conversion().get().preset ();
249                         vector<PresetColourConversion> cc = Config::instance()->colour_conversions ();
250                         _colour_conversion->SetLabel (preset ? std_to_wx (cc[preset.get()].name) : _("Custom"));
251                         _enable_colour_conversion->SetValue (true);
252                         _colour_conversion->Enable (true);
253                         _colour_conversion_button->Enable (true);
254                 } else {
255                         _colour_conversion->SetLabel (_("None"));
256                         _enable_colour_conversion->SetValue (false);
257                         _colour_conversion->Enable (false);
258                         _colour_conversion_button->Enable (false);
259                 }
260         } else if (property == FFmpegContentProperty::FILTERS) {
261                 if (fcs) {
262                         string const p = Filter::ffmpeg_string (fcs->filters ());
263                         if (p.empty ()) {
264                                 _filters->SetLabel (_("None"));
265                         } else {
266                                 _filters->SetLabel (std_to_wx (p));
267                         }
268                 }
269         }
270 }
271
272 /** Called when the `Edit filters' button has been clicked */
273 void
274 VideoPanel::edit_filters_clicked ()
275 {
276         FFmpegContentList c = _editor->selected_ffmpeg_content ();
277         if (c.size() != 1) {
278                 return;
279         }
280
281         FilterDialog* d = new FilterDialog (this, c.front()->filters());
282         d->ActiveChanged.connect (bind (&FFmpegContent::set_filters, c.front(), _1));
283         d->ShowModal ();
284         d->Destroy ();
285 }
286
287 void
288 VideoPanel::setup_description ()
289 {
290         VideoContentList vc = _editor->selected_video_content ();
291         if (vc.empty ()) {
292                 _description->SetLabel ("");
293                 return;
294         } else if (vc.size() > 1) {
295                 _description->SetLabel (_("Multiple content selected"));
296                 return;
297         }
298
299         shared_ptr<VideoContent> vcs = vc.front ();
300
301         wxString d;
302
303         int lines = 0;
304
305         if (vcs->video_size().width && vcs->video_size().height) {
306                 d << wxString::Format (
307                         _("Content video is %dx%d (%.2f:1)\n"),
308                         vcs->video_size_after_3d_split().width,
309                         vcs->video_size_after_3d_split().height,
310                         vcs->video_size_after_3d_split().ratio ()
311                         );
312                 ++lines;
313         }
314
315         Crop const crop = vcs->crop ();
316         if ((crop.left || crop.right || crop.top || crop.bottom) && vcs->video_size() != libdcp::Size (0, 0)) {
317                 libdcp::Size cropped = vcs->video_size_after_crop ();
318                 d << wxString::Format (
319                         _("Cropped to %dx%d (%.2f:1)\n"),
320                         cropped.width, cropped.height,
321                         cropped.ratio ()
322                         );
323                 ++lines;
324         }
325
326         libdcp::Size const container_size = _editor->film()->frame_size ();
327         libdcp::Size const scaled = vcs->scale().size (vcs, container_size, container_size);
328
329         if (scaled != vcs->video_size_after_crop ()) {
330                 d << wxString::Format (
331                         _("Scaled to %dx%d (%.2f:1)\n"),
332                         scaled.width, scaled.height,
333                         scaled.ratio ()
334                         );
335                 ++lines;
336         }
337         
338         if (scaled != container_size) {
339                 d << wxString::Format (
340                         _("Padded with black to %dx%d (%.2f:1)\n"),
341                         container_size.width, container_size.height,
342                         container_size.ratio ()
343                         );
344                 ++lines;
345         }
346
347         d << wxString::Format (_("Content frame rate %.4f\n"), vcs->video_frame_rate ());
348         ++lines;
349         FrameRateChange frc (vcs->video_frame_rate(), _editor->film()->video_frame_rate ());
350         d << std_to_wx (frc.description ()) << "\n";
351         ++lines;
352
353         for (int i = lines; i < 6; ++i) {
354                 d << wxT ("\n ");
355         }
356
357         _description->SetLabel (d);
358         _sizer->Layout ();
359 }
360
361 void
362 VideoPanel::edit_colour_conversion_clicked ()
363 {
364         VideoContentList vc = _editor->selected_video_content ();
365         if (vc.size() != 1) {
366                 return;
367         }
368
369         if (!vc.front()->colour_conversion ()) {
370                 return;
371         }
372
373         ColourConversion conversion = vc.front()->colour_conversion().get ();
374         ContentColourConversionDialog* d = new ContentColourConversionDialog (this);
375         d->set (conversion);
376         d->ShowModal ();
377
378         vc.front()->set_colour_conversion (d->get ());
379         d->Destroy ();
380 }
381
382 void
383 VideoPanel::content_selection_changed ()
384 {
385         VideoContentList video_sel = _editor->selected_video_content ();
386         FFmpegContentList ffmpeg_sel = _editor->selected_ffmpeg_content ();
387         
388         bool const single = video_sel.size() == 1;
389
390         _left_crop->set_content (video_sel);
391         _right_crop->set_content (video_sel);
392         _top_crop->set_content (video_sel);
393         _bottom_crop->set_content (video_sel);
394         _frame_type->set_content (video_sel);
395         _scale->set_content (video_sel);
396
397         _filters_button->Enable (single && !ffmpeg_sel.empty ());
398         _colour_conversion_button->Enable (single);
399
400         film_content_changed (VideoContentProperty::VIDEO_CROP);
401         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
402         film_content_changed (VideoContentProperty::COLOUR_CONVERSION);
403         film_content_changed (FFmpegContentProperty::FILTERS);
404 }
405
406 void
407 VideoPanel::enable_colour_conversion_clicked ()
408 {
409         VideoContentList vc = _editor->selected_video_content ();
410         if (vc.size() != 1) {
411                 return;
412         }
413
414         if (_enable_colour_conversion->GetValue()) {
415                 vc.front()->set_default_colour_conversion ();
416         } else {
417                 vc.front()->unset_colour_conversion ();
418         }
419 }